1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm\Repository\QueryObject; |
11
|
|
|
|
12
|
|
|
use Slick\Database\Sql\Select; |
13
|
|
|
use Slick\Orm\Entity\EntityCollection; |
14
|
|
|
use Slick\Orm\EntityInterface; |
15
|
|
|
use Slick\Orm\RepositoryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* QueryObject |
19
|
|
|
* |
20
|
|
|
* @package Slick\Orm\Repository |
21
|
|
|
* @author Filipe Silva <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class QueryObject extends Select implements QueryObjectInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var RepositoryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $repository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* QueryObject has a repository as a dependency. |
33
|
|
|
* |
34
|
|
|
* @param RepositoryInterface $repository |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct(RepositoryInterface $repository) |
37
|
|
|
{ |
38
|
10 |
|
$this->repository = $repository; |
39
|
10 |
|
$this->adapter = $repository->getAdapter(); |
40
|
10 |
|
parent::__construct( |
41
|
10 |
|
$repository->getEntityDescriptor()->getTableName() |
42
|
10 |
|
); |
43
|
10 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve all records matching this select query |
47
|
|
|
* |
48
|
|
|
* @return \Slick\Database\RecordList |
49
|
|
|
*/ |
50
|
6 |
|
public function all() |
51
|
|
|
{ |
52
|
6 |
|
$cid = $this->getId($this); |
53
|
6 |
|
$collection = $this->repository |
54
|
6 |
|
->getCollectionsMap() |
55
|
6 |
|
->get($cid, false); |
56
|
|
|
|
57
|
6 |
View Code Duplication |
if (false === $collection) { |
|
|
|
|
58
|
4 |
|
$data = $this->adapter->query($this, $this->getParameters()); |
59
|
4 |
|
$collection = $this->repository->getEntityMapper() |
60
|
4 |
|
->createFrom($data); |
61
|
4 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
62
|
4 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
63
|
4 |
|
} |
64
|
|
|
|
65
|
6 |
|
return $collection; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve first record matching this select query |
70
|
|
|
* |
71
|
|
|
* @return EntityInterface|null |
72
|
|
|
*/ |
73
|
2 |
|
public function first() |
74
|
|
|
{ |
75
|
2 |
|
$sql = clone($this); |
76
|
2 |
|
$sql->limit(1); |
77
|
2 |
|
$cid = $this->getId($sql); |
78
|
|
|
|
79
|
2 |
|
$collection = $this->repository |
80
|
2 |
|
->getCollectionsMap() |
81
|
2 |
|
->get($cid, false); |
82
|
|
|
|
83
|
2 |
View Code Duplication |
if (false === $collection) { |
|
|
|
|
84
|
2 |
|
$data = $this->adapter->query($sql, $sql->getParameters()); |
85
|
2 |
|
$collection = $this->repository->getEntityMapper() |
86
|
2 |
|
->createFrom($data); |
87
|
2 |
|
$this->repository->getCollectionsMap()->set($cid, $collection); |
|
|
|
|
88
|
2 |
|
$this->updateIdentityMap($collection); |
|
|
|
|
89
|
2 |
|
} |
90
|
|
|
|
91
|
2 |
|
return $collection->isEmpty() |
|
|
|
|
92
|
2 |
|
? null |
93
|
2 |
|
: $collection[0]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the id for this query |
98
|
|
|
* |
99
|
|
|
* @param QueryObjectInterface $query |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
8 |
|
protected function getId(QueryObjectInterface $query) |
104
|
|
|
{ |
105
|
8 |
|
$str = $query->getQueryString(); |
106
|
8 |
|
$search = array_keys($query->getParameters()); |
107
|
8 |
|
$values = array_values($query->getParameters()); |
108
|
8 |
|
return str_replace($search, $values, $str); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Registers every entity in collection to the repository identity map |
113
|
|
|
* |
114
|
|
|
* @param EntityCollection $collection |
115
|
|
|
* |
116
|
|
|
* @return self |
117
|
|
|
*/ |
118
|
6 |
|
protected function updateIdentityMap(EntityCollection $collection) |
119
|
|
|
{ |
120
|
6 |
|
if ($collection->isEmpty()) { |
121
|
2 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
4 |
|
foreach ($collection as $entity) |
125
|
|
|
{ |
126
|
4 |
|
$this->repository->getIdentityMap()->set($entity); |
127
|
4 |
|
} |
128
|
4 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the repository that is using this query object |
133
|
|
|
* |
134
|
|
|
* @return RepositoryInterface |
135
|
|
|
*/ |
136
|
10 |
|
public function getRepository() |
137
|
|
|
{ |
138
|
10 |
|
return $this->repository; |
139
|
|
|
} |
140
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.