1 | <?php |
||
24 | trait ObjectById |
||
25 | { |
||
26 | |||
27 | use Object; |
||
28 | |||
29 | /** |
||
30 | * @var BaseObject[] |
||
31 | */ |
||
32 | protected $cacheById = []; |
||
33 | |||
34 | /** |
||
35 | * @param Record $record |
||
36 | * @param string|null $toScenario |
||
37 | * @return BaseObject |
||
38 | */ |
||
39 | abstract protected function findByRecord(Record $record, string $toScenario = null): BaseObject; |
||
40 | |||
41 | |||
42 | /******************************************* |
||
43 | * FIND/GET BY ID |
||
44 | *******************************************/ |
||
45 | |||
46 | /** |
||
47 | * @param int $id |
||
48 | * @param string|null $toScenario |
||
49 | * @return BaseObject|null |
||
50 | */ |
||
51 | public function findById(int $id, string $toScenario = null) |
||
72 | |||
73 | /** |
||
74 | * @param int $id |
||
75 | * @param string|null $toScenario |
||
76 | * @return BaseObject |
||
77 | * @throws ObjectNotFoundException |
||
78 | */ |
||
79 | public function getById(int $id, string $toScenario = null): BaseObject |
||
89 | |||
90 | /** |
||
91 | * @param int $id |
||
92 | * @param string|null $toScenario |
||
93 | * @return BaseObject|null |
||
94 | */ |
||
95 | public function freshFindById(int $id, string $toScenario = null) |
||
105 | |||
106 | /** |
||
107 | * @param int $id |
||
108 | * @param string|null $toScenario |
||
109 | * @return BaseObject |
||
110 | * @throws ObjectNotFoundException |
||
111 | */ |
||
112 | public function freshGetById(int $id, string $toScenario = null): BaseObject |
||
121 | |||
122 | |||
123 | /******************************************* |
||
124 | * CACHE |
||
125 | *******************************************/ |
||
126 | |||
127 | /** |
||
128 | * Find an existing cache by ID |
||
129 | * |
||
130 | * @param $id |
||
131 | * @return BaseObject|null |
||
132 | */ |
||
133 | public function findCacheById(int $id) |
||
143 | |||
144 | /** |
||
145 | * Identify whether in cache by ID |
||
146 | * |
||
147 | * @param $id |
||
148 | * @return bool |
||
149 | */ |
||
150 | protected function isCachedById(int $id) |
||
154 | |||
155 | /** |
||
156 | * @param ObjectWithId $object |
||
157 | * @return $this |
||
158 | */ |
||
159 | protected function cacheById(ObjectWithId $object) |
||
170 | |||
171 | |||
172 | /******************************************* |
||
173 | * RECORD BY ID |
||
174 | *******************************************/ |
||
175 | |||
176 | /** |
||
177 | * @param int $id |
||
178 | * @param string|null $toScenario |
||
179 | * @return RecordWithId|null |
||
180 | */ |
||
181 | public function findRecordById(int $id, string $toScenario = null) |
||
191 | |||
192 | /** |
||
193 | * @param int $id |
||
194 | * @param string|null $toScenario |
||
195 | * @throws RecordNotFoundException |
||
196 | * @return RecordWithId|null |
||
197 | */ |
||
198 | public function getRecordById(int $id, string $toScenario = null) |
||
207 | |||
208 | |||
209 | /******************************************* |
||
210 | * EXCEPTIONS |
||
211 | *******************************************/ |
||
212 | |||
213 | /** |
||
214 | * @param int|null $id |
||
215 | * @throws ObjectNotFoundException |
||
216 | */ |
||
217 | protected function notFoundByIdException(int $id = null) |
||
227 | |||
228 | /** |
||
229 | * @param int|null $id |
||
230 | * @throws RecordNotFoundException |
||
231 | */ |
||
232 | protected function notFoundRecordByIdException(int $id = null) |
||
242 | } |
||
243 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.