1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model; |
6
|
|
|
|
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
|
9
|
|
|
final class Resolver implements ResolverInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var ContainerInterface|RepositoryInterface[] |
13
|
|
|
*/ |
14
|
|
|
private $container; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param ContainerInterface $container |
18
|
|
|
*/ |
19
|
|
|
public function __construct(ContainerInterface $container) |
20
|
|
|
{ |
21
|
|
|
$this->container = $container; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $modelClass |
26
|
|
|
* @param string $id |
27
|
|
|
* |
28
|
|
|
* @return ModelInterface|null |
29
|
|
|
*/ |
30
|
|
|
public function find(string $modelClass, string $id) |
31
|
|
|
{ |
32
|
|
|
return $this->getRepositoryByClass($modelClass)->find($id); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $modelClass |
37
|
|
|
* @param array $criteria |
38
|
|
|
* |
39
|
|
|
* @return ModelInterface|null |
40
|
|
|
*/ |
41
|
|
|
public function findOneBy(string $modelClass, array $criteria) |
42
|
|
|
{ |
43
|
|
|
return $this->getRepositoryByClass($modelClass)->findOneBy($criteria); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $modelClass |
48
|
|
|
* @param array $criteria |
49
|
|
|
* @param array|null $orderBy |
50
|
|
|
* @param int|null $limit |
51
|
|
|
* @param int|null $offset |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function findBy( |
56
|
|
|
string $modelClass, |
57
|
|
|
array $criteria, |
58
|
|
|
array $orderBy = null, |
59
|
|
|
int $limit = null, |
60
|
|
|
int $offset = null |
61
|
|
|
): array { |
62
|
|
|
return $this->getRepositoryByClass($modelClass)->findBy($criteria, $orderBy, $limit, $offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $modelClass |
67
|
|
|
* @param string $id |
68
|
|
|
* |
69
|
|
|
* @return \Closure |
70
|
|
|
*/ |
71
|
|
|
public function lazyFind(string $modelClass, string $id): \Closure |
72
|
|
|
{ |
73
|
|
|
return function () use ($modelClass, $id) { |
74
|
|
|
return $this->find($modelClass, $id); |
75
|
|
|
}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $modelClass |
80
|
|
|
* @param array $criteria |
81
|
|
|
* |
82
|
|
|
* @return \Closure |
83
|
|
|
*/ |
84
|
|
|
public function lazyFindOneBy(string $modelClass, array $criteria): \Closure |
85
|
|
|
{ |
86
|
|
|
return function () use ($modelClass, $criteria) { |
87
|
|
|
return $this->findOneBy($modelClass, $criteria); |
88
|
|
|
}; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $modelClass |
93
|
|
|
* @param array $criteria |
94
|
|
|
* @param array|null $orderBy |
95
|
|
|
* @param int|null $limit |
96
|
|
|
* @param int|null $offset |
97
|
|
|
* |
98
|
|
|
* @return \Closure |
99
|
|
|
*/ |
100
|
|
|
public function lazyFindBy( |
101
|
|
|
string $modelClass, |
102
|
|
|
array $criteria, |
103
|
|
|
array $orderBy = null, |
104
|
|
|
int $limit = null, |
105
|
|
|
int $offset = null |
106
|
|
|
): \Closure { |
107
|
|
|
return function () use ($modelClass, $criteria, $orderBy, $limit, $offset) { |
108
|
|
|
return $this->findBy($modelClass, $criteria, $orderBy, $limit, $offset); |
109
|
|
|
}; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param ModelInterface $model |
114
|
|
|
*/ |
115
|
|
|
public function persist(ModelInterface $model) |
116
|
|
|
{ |
117
|
|
|
$this->getRepositoryByClass(get_class($model))->persist($model); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ModelInterface $model |
122
|
|
|
*/ |
123
|
|
|
public function remove(ModelInterface $model) |
124
|
|
|
{ |
125
|
|
|
$this->getRepositoryByClass(get_class($model))->remove($model); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $modelClass |
130
|
|
|
* |
131
|
|
|
* @return RepositoryInterface |
132
|
|
|
*/ |
133
|
|
|
private function getRepositoryByClass(string $modelClass): RepositoryInterface |
134
|
|
|
{ |
135
|
|
|
if (!$this->container->has($modelClass)) { |
136
|
|
|
throw MissingRepositoryException::create($modelClass); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->container->get($modelClass); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|