From 42b2638c7e529aaa84ac04e7c07a5697aa1b1626 Mon Sep 17 00:00:00 2001 From: Arne De Smedt Date: Thu, 31 Aug 2023 17:20:01 +0200 Subject: [PATCH 1/2] Order on document id --- src/OrderBy/DocId.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/OrderBy/DocId.php diff --git a/src/OrderBy/DocId.php b/src/OrderBy/DocId.php new file mode 100644 index 0000000..24584f2 --- /dev/null +++ b/src/OrderBy/DocId.php @@ -0,0 +1,36 @@ + + * + * 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; + } +} From 08650c70df1f602cd2f627e6bf7122a16fdd43a1 Mon Sep 17 00:00:00 2001 From: Arne De Smedt Date: Thu, 31 Aug 2023 17:28:54 +0200 Subject: [PATCH 2/2] Adapt InMemoryDocumentStore --- src/InMemoryDocumentStore.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/InMemoryDocumentStore.php b/src/InMemoryDocumentStore.php index 51c4472..09131c1 100644 --- a/src/InMemoryDocumentStore.php +++ b/src/InMemoryDocumentStore.php @@ -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; @@ -644,6 +645,10 @@ 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(); @@ -651,8 +656,9 @@ private function sort(&$docs, OrderBy $orderBy) } 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 ));