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 )); 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; + } +}