1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace PTS\MongoRepo; |
5
|
|
|
|
6
|
|
|
use MongoDB\BSON\ObjectId; |
7
|
|
|
use MongoDB\Collection; |
8
|
|
|
use MongoDB\InsertOneResult; |
9
|
|
|
use MongoDB\UpdateResult; |
10
|
|
|
use PTS\DataTransformer\DataTransformerInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @mixin Collection |
15
|
|
|
*/ |
16
|
|
|
abstract class MongoRepo |
17
|
|
|
{ |
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
|
7 |
|
public function __construct(CollectionManager $collectionManager, DataTransformerInterface $mapper) |
32
|
|
|
{ |
33
|
7 |
|
$this->collectionManager = $collectionManager; |
34
|
7 |
|
$this->mapper = $mapper; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
7 |
|
public function __call(string $name, array $arguments = []) |
38
|
|
|
{ |
39
|
7 |
|
$collection = $this->getCollection(); |
40
|
7 |
|
if (method_exists($collection, $name)) { |
41
|
7 |
|
return $collection->$name(...$arguments); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
throw new RuntimeException("Unknown method {$name}"); |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
public function getCollection(string $table = null): Collection |
48
|
|
|
{ |
49
|
7 |
|
return $this->collectionManager->getCollection($table ?? $this->tableName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param object $model |
54
|
|
|
* @param array $options |
55
|
|
|
* |
56
|
|
|
* @return InsertOneResult |
57
|
|
|
*/ |
58
|
4 |
|
public function insertOneModel(object $model, array $options = []): InsertOneResult |
59
|
|
|
{ |
60
|
4 |
|
$dto = $this->modelToDoc($model); |
61
|
4 |
|
$result = $this->getCollection()->insertOne($dto, $options); |
62
|
4 |
|
$this->fillId($result->getInsertedId(), $model); |
63
|
|
|
|
64
|
4 |
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function replaceOneModel(object $model, array $filter, array $options = []): UpdateResult |
68
|
|
|
{ |
69
|
1 |
|
$dto = $this->modelToDoc($model); |
70
|
1 |
|
return $this->getCollection()->replaceOne($filter, $dto, $options); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function replaceOneModelById(object $model, ObjectId $id, array $options = []): UpdateResult |
74
|
|
|
{ |
75
|
1 |
|
return $this->replaceOneModel($model, [$this->pk => $id], $options); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function findOneModel(array $filter = [], array $options = []): ?object |
79
|
|
|
{ |
80
|
1 |
|
$doc = $this->getCollection()->findOne($filter, $options); |
81
|
1 |
|
if ($doc === null) { |
82
|
1 |
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $this->docsToModels($this->classModel, [$doc], $this->getMap())[0]; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function findModels(array $filter = [], array $options = []): array |
89
|
|
|
{ |
90
|
1 |
|
$cursor = $this->getCollection()->find($filter, $options); |
91
|
1 |
|
return $this->docsToModels($this->classModel, $cursor, $this->getMap()); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
protected function modelToDoc(object $model): array |
95
|
|
|
{ |
96
|
4 |
|
return $this->mapper->toDTO($model, $this->getMap()); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
protected function docsToModels(string $class, iterable $docs, string $map): array |
100
|
|
|
{ |
101
|
2 |
|
return $this->mapper->toModelsCollection($class, $docs, $map); |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
protected function fillId(ObjectId $id, object $model): void |
105
|
|
|
{ |
106
|
4 |
|
$this->mapper->fillModel($model, ['_id' => $id], $this->getMap()); |
107
|
4 |
|
} |
108
|
|
|
|
109
|
4 |
|
protected function getMap(): string |
110
|
|
|
{ |
111
|
4 |
|
return $this->map; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|