Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order on document id #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/InMemoryDocumentStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use EventEngine\DocumentStore\OrderBy\AndOrder;
use EventEngine\DocumentStore\OrderBy\Asc;
use EventEngine\DocumentStore\OrderBy\Desc;
use EventEngine\DocumentStore\OrderBy\DocId;
use EventEngine\DocumentStore\OrderBy\OrderBy;
use EventEngine\Persistence\InMemoryConnection;
use function array_key_exists;
Expand Down Expand Up @@ -644,15 +645,20 @@ private function sort(&$docs, OrderBy $orderBy)
};

$getField = function (array $doc, OrderBy $orderBy) {
if ($orderBy instanceof DocId) {
return $doc['docId'];
}

if ($orderBy instanceof Asc || $orderBy instanceof Desc) {
$field = $orderBy->prop();

return (new ArrayReader($doc['doc']))->mixedValue($field);
}

throw new \RuntimeException(\sprintf(
'Unable to get field from doc: %s. Given OrderBy is neither an instance of %s nor %s',
\json_encode($doc['doc']),
'Unable to get field from doc: %s. Given OrderBy is neither an instance of %s, %s nor %s',
\json_encode($doc),
DocId::class,
Asc::class,
Desc::class
));
Expand Down
36 changes: 36 additions & 0 deletions src/OrderBy/DocId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of event-engine/php-document-sore.
* (c) 2018-2021 prooph software GmbH <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace EventEngine\DocumentStore\OrderBy;

final class DocId implements OrderBy
{
private $direction;

public function __construct($direction = 'ASC') {
$this->direction = $direction;
}

public static function fromArray(array $data): OrderBy
{
return new self($data['direction'] ?? OrderBy::ASC);
}

public function toArray(): array
{
return ['direction' => $this->direction];
}

public function direction()
{
return $this->direction;
}
}