|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\Model\Doctrine\DBAL\MissingRepositoryException; |
|
8
|
|
|
use Interop\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class Resolver implements ResolverInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var ContainerInterface|RepositoryInterface[] |
|
14
|
|
|
*/ |
|
15
|
|
|
private $container; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param ContainerInterface $container |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(ContainerInterface $container) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->container = $container; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $modelClass |
|
27
|
|
|
* @param string $id |
|
28
|
|
|
* @return \Closure |
|
29
|
|
|
*/ |
|
30
|
|
|
public function find(string $modelClass, string $id): \Closure |
|
31
|
|
|
{ |
|
32
|
|
|
return function () use ($modelClass, $id) { |
|
33
|
|
|
return $this->getRepositoryByClass($modelClass)->find($id); |
|
34
|
|
|
}; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $modelClass |
|
39
|
|
|
* @param array $criteria |
|
40
|
|
|
* @return \Closure |
|
41
|
|
|
*/ |
|
42
|
|
|
public function findOneBy(string $modelClass, array $criteria): \Closure |
|
43
|
|
|
{ |
|
44
|
|
|
return function () use ($modelClass, $criteria) { |
|
45
|
|
|
return $this->getRepositoryByClass($modelClass)->findOneBy($criteria); |
|
46
|
|
|
}; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $modelClass |
|
51
|
|
|
* @param array $criteria |
|
52
|
|
|
* @param array|null $orderBy |
|
53
|
|
|
* @param int|null $limit |
|
54
|
|
|
* @param int|null $offset |
|
55
|
|
|
* @return \Closure |
|
56
|
|
|
*/ |
|
57
|
|
|
public function findBy( |
|
58
|
|
|
string $modelClass, |
|
59
|
|
|
array $criteria, |
|
60
|
|
|
array $orderBy = null, |
|
61
|
|
|
int $limit = null, |
|
62
|
|
|
int $offset = null |
|
63
|
|
|
): \Closure { |
|
64
|
|
|
return function () use ($modelClass, $criteria, $orderBy, $limit, $offset) { |
|
65
|
|
|
return $this->getRepositoryByClass($modelClass)->findBy($criteria, $orderBy, $limit, $offset); |
|
66
|
|
|
}; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $modelClass |
|
71
|
|
|
* @return RepositoryInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getRepositoryByClass(string $modelClass): RepositoryInterface |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->container->has($modelClass)) { |
|
76
|
|
|
throw MissingRepositoryException::create($modelClass); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->container->get($modelClass); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|