@@ -10,27 +10,27 @@ |
||
| 10 | 10 | class CollectionManager |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - protected Database $db; |
|
| 14 | - /** @var Collection[] */ |
|
| 15 | - protected array $store = []; |
|
| 16 | - |
|
| 17 | - public function __construct(array $params) |
|
| 18 | - { |
|
| 19 | - $client = new Client( |
|
| 20 | - $params['dsn'] ?? 'mongodb://127.0.0.1/', |
|
| 21 | - $params['uriOptions'] ?? [], |
|
| 22 | - $params['driverOptions'] ?? [] |
|
| 23 | - ); |
|
| 24 | - |
|
| 25 | - $this->db = $client->selectDatabase($params['db']); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public function getCollection(string $name): Collection |
|
| 29 | - { |
|
| 30 | - if (!array_key_exists($name, $this->store)) { |
|
| 31 | - $this->store[$name] = $this->db->selectCollection($name); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - return $this->store[$name]; |
|
| 35 | - } |
|
| 13 | + protected Database $db; |
|
| 14 | + /** @var Collection[] */ |
|
| 15 | + protected array $store = []; |
|
| 16 | + |
|
| 17 | + public function __construct(array $params) |
|
| 18 | + { |
|
| 19 | + $client = new Client( |
|
| 20 | + $params['dsn'] ?? 'mongodb://127.0.0.1/', |
|
| 21 | + $params['uriOptions'] ?? [], |
|
| 22 | + $params['driverOptions'] ?? [] |
|
| 23 | + ); |
|
| 24 | + |
|
| 25 | + $this->db = $client->selectDatabase($params['db']); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function getCollection(string $name): Collection |
|
| 29 | + { |
|
| 30 | + if (!array_key_exists($name, $this->store)) { |
|
| 31 | + $this->store[$name] = $this->db->selectCollection($name); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + return $this->store[$name]; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -16,98 +16,98 @@ |
||
| 16 | 16 | abstract class MongoRepo |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - public const SORT_DESC = -1; |
|
| 20 | - public const SORT_ASC = 1; |
|
| 21 | - |
|
| 22 | - protected string $tableName; |
|
| 23 | - protected string $classModel; |
|
| 24 | - |
|
| 25 | - protected string $map = 'db'; |
|
| 26 | - protected string $pk = '_id'; |
|
| 27 | - |
|
| 28 | - protected DataTransformerInterface $mapper; |
|
| 29 | - protected CollectionManager $collectionManager; |
|
| 30 | - |
|
| 31 | - public function __construct(CollectionManager $collectionManager, DataTransformerInterface $mapper) |
|
| 32 | - { |
|
| 33 | - $this->collectionManager = $collectionManager; |
|
| 34 | - $this->mapper = $mapper; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function __call(string $name, array $arguments = []) |
|
| 38 | - { |
|
| 39 | - $collection = $this->getCollection(); |
|
| 40 | - if (method_exists($collection, $name)) { |
|
| 41 | - return $collection->$name(...$arguments); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - throw new RuntimeException("Unknown method {$name}"); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - public function getCollection(string $table = null): Collection |
|
| 48 | - { |
|
| 49 | - return $this->collectionManager->getCollection($table ?? $this->tableName); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param object $model |
|
| 54 | - * @param array $options |
|
| 55 | - * |
|
| 56 | - * @return InsertOneResult |
|
| 57 | - */ |
|
| 58 | - public function insertOneModel(object $model, array $options = []): InsertOneResult |
|
| 59 | - { |
|
| 60 | - $dto = $this->modelToDoc($model); |
|
| 61 | - $result = $this->getCollection()->insertOne($dto, $options); |
|
| 62 | - $this->fillId($result->getInsertedId(), $model); |
|
| 63 | - |
|
| 64 | - return $result; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function replaceOneModel(object $model, array $filter, array $options = []): UpdateResult |
|
| 68 | - { |
|
| 69 | - $dto = $this->modelToDoc($model); |
|
| 70 | - return $this->getCollection()->replaceOne($filter, $dto, $options); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - public function replaceOneModelById(object $model, ObjectId $id, array $options = []): UpdateResult |
|
| 74 | - { |
|
| 75 | - return $this->replaceOneModel($model, [$this->pk => $id], $options); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - public function findOneModel(array $filter = [], array $options = []): ?object |
|
| 79 | - { |
|
| 80 | - $doc = $this->getCollection()->findOne($filter, $options); |
|
| 81 | - if ($doc === null) { |
|
| 82 | - return null; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return $this->docsToModels($this->classModel, [$doc], $this->getMap())[0]; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function findModels(array $filter = [], array $options = []): array |
|
| 89 | - { |
|
| 90 | - $cursor = $this->getCollection()->find($filter, $options); |
|
| 91 | - return $this->docsToModels($this->classModel, $cursor, $this->getMap()); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - protected function modelToDoc(object $model): array |
|
| 95 | - { |
|
| 96 | - return $this->mapper->toDTO($model, $this->getMap()); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - protected function docsToModels(string $class, iterable $docs, string $map): array |
|
| 100 | - { |
|
| 101 | - return $this->mapper->toModelsCollection($class, $docs, $map); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - protected function fillId(ObjectId $id, object $model): void |
|
| 105 | - { |
|
| 106 | - $this->mapper->fillModel($model, ['_id' => $id], $this->getMap()); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - protected function getMap(): string |
|
| 110 | - { |
|
| 111 | - return $this->map; |
|
| 112 | - } |
|
| 19 | + public const SORT_DESC = -1; |
|
| 20 | + public const SORT_ASC = 1; |
|
| 21 | + |
|
| 22 | + protected string $tableName; |
|
| 23 | + protected string $classModel; |
|
| 24 | + |
|
| 25 | + protected string $map = 'db'; |
|
| 26 | + protected string $pk = '_id'; |
|
| 27 | + |
|
| 28 | + protected DataTransformerInterface $mapper; |
|
| 29 | + protected CollectionManager $collectionManager; |
|
| 30 | + |
|
| 31 | + public function __construct(CollectionManager $collectionManager, DataTransformerInterface $mapper) |
|
| 32 | + { |
|
| 33 | + $this->collectionManager = $collectionManager; |
|
| 34 | + $this->mapper = $mapper; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function __call(string $name, array $arguments = []) |
|
| 38 | + { |
|
| 39 | + $collection = $this->getCollection(); |
|
| 40 | + if (method_exists($collection, $name)) { |
|
| 41 | + return $collection->$name(...$arguments); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + throw new RuntimeException("Unknown method {$name}"); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + public function getCollection(string $table = null): Collection |
|
| 48 | + { |
|
| 49 | + return $this->collectionManager->getCollection($table ?? $this->tableName); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param object $model |
|
| 54 | + * @param array $options |
|
| 55 | + * |
|
| 56 | + * @return InsertOneResult |
|
| 57 | + */ |
|
| 58 | + public function insertOneModel(object $model, array $options = []): InsertOneResult |
|
| 59 | + { |
|
| 60 | + $dto = $this->modelToDoc($model); |
|
| 61 | + $result = $this->getCollection()->insertOne($dto, $options); |
|
| 62 | + $this->fillId($result->getInsertedId(), $model); |
|
| 63 | + |
|
| 64 | + return $result; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function replaceOneModel(object $model, array $filter, array $options = []): UpdateResult |
|
| 68 | + { |
|
| 69 | + $dto = $this->modelToDoc($model); |
|
| 70 | + return $this->getCollection()->replaceOne($filter, $dto, $options); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + public function replaceOneModelById(object $model, ObjectId $id, array $options = []): UpdateResult |
|
| 74 | + { |
|
| 75 | + return $this->replaceOneModel($model, [$this->pk => $id], $options); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + public function findOneModel(array $filter = [], array $options = []): ?object |
|
| 79 | + { |
|
| 80 | + $doc = $this->getCollection()->findOne($filter, $options); |
|
| 81 | + if ($doc === null) { |
|
| 82 | + return null; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return $this->docsToModels($this->classModel, [$doc], $this->getMap())[0]; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function findModels(array $filter = [], array $options = []): array |
|
| 89 | + { |
|
| 90 | + $cursor = $this->getCollection()->find($filter, $options); |
|
| 91 | + return $this->docsToModels($this->classModel, $cursor, $this->getMap()); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + protected function modelToDoc(object $model): array |
|
| 95 | + { |
|
| 96 | + return $this->mapper->toDTO($model, $this->getMap()); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + protected function docsToModels(string $class, iterable $docs, string $map): array |
|
| 100 | + { |
|
| 101 | + return $this->mapper->toModelsCollection($class, $docs, $map); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + protected function fillId(ObjectId $id, object $model): void |
|
| 105 | + { |
|
| 106 | + $this->mapper->fillModel($model, ['_id' => $id], $this->getMap()); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + protected function getMap(): string |
|
| 110 | + { |
|
| 111 | + return $this->map; |
|
| 112 | + } |
|
| 113 | 113 | } |