1 | <?php |
||
26 | class Repository implements RepositoryInterface |
||
27 | { |
||
28 | use MessageMapperTrait; |
||
29 | |||
30 | /** |
||
31 | * @var Pagination |
||
32 | */ |
||
33 | protected $pagination; |
||
34 | |||
35 | /** |
||
36 | * @var StorageInterface |
||
37 | */ |
||
38 | private $storage; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $table; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $class; |
||
49 | |||
50 | /** |
||
51 | * @var FinderInterface |
||
52 | */ |
||
53 | private $finder; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param StorageInterface $storage |
||
59 | * @param string $table |
||
60 | * @param string $class |
||
61 | */ |
||
62 | public function __construct(StorageInterface $storage, $table, $class) |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function save(ReadModelInterface $model) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function count() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function findById($id) |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function withSpecifications(array $specifications) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function withSpecification(SpecificationInterface $specification) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function withPagination($limit, $page) |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function findOne(array $fields) |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function find(array $fields) |
||
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | public function findAll() |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function remove($id) |
||
159 | |||
160 | /* |
||
161 | * {@inheritdoc} |
||
162 | */ |
||
163 | public function reset() |
||
170 | |||
171 | /** |
||
172 | * Get finder that used for find data with complex criteria. |
||
173 | * |
||
174 | * @return FinderInterface |
||
175 | */ |
||
176 | protected function finder() |
||
184 | |||
185 | /** |
||
186 | * Add criteria. |
||
187 | * |
||
188 | * @param array $fields |
||
189 | * |
||
190 | * @return static |
||
191 | */ |
||
192 | protected function addCriteria(array $fields) |
||
200 | |||
201 | /** |
||
202 | * Get a collection. |
||
203 | * |
||
204 | * @return Collection |
||
205 | */ |
||
206 | protected function get() |
||
217 | |||
218 | /** |
||
219 | * Find first record. |
||
220 | * |
||
221 | * @return ReadModelInterface |
||
222 | */ |
||
223 | protected function first() |
||
230 | |||
231 | /** |
||
232 | * Assert that read model instance should be same. |
||
233 | * |
||
234 | * @param ReadModelInterface $model |
||
235 | */ |
||
236 | protected function assertSameInstance(ReadModelInterface $model) |
||
247 | |||
248 | /** |
||
249 | * Build pagination. |
||
250 | */ |
||
251 | private function paginate() |
||
260 | } |
||
261 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.