1 | <?php |
||
18 | abstract class iDokladAbstractFunction implements iDokladFunctionInterface |
||
19 | { |
||
20 | /** @var ResponseInterface|null $response */ |
||
21 | protected $response; |
||
22 | |||
23 | /** @var array $config */ |
||
24 | protected $config; |
||
25 | |||
26 | /** @var null|iDokladFilter $filter */ |
||
27 | protected $filter; |
||
28 | |||
29 | /** @var null|iDokladPaginator $paginator */ |
||
30 | protected $paginator; |
||
31 | |||
32 | /** @var null|iDokladSortable $sortable */ |
||
33 | protected $sortable; |
||
34 | |||
35 | /** |
||
36 | * @param iDokladPaginator $paginator |
||
37 | * |
||
38 | * @return iDokladAbstractFunction |
||
39 | */ |
||
40 | public function paginator(iDokladPaginator $paginator): self |
||
46 | |||
47 | /** |
||
48 | * @param iDokladSortable $sortable |
||
49 | * |
||
50 | * @return iDokladAbstractFunction |
||
51 | */ |
||
52 | public function sortable(iDokladSortable $sortable): self |
||
58 | |||
59 | /** |
||
60 | * @param iDokladFilter $filter |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function filter(iDokladFilter $filter) |
||
70 | |||
71 | /** |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function injectAccessTokenToHeaders(): bool |
||
78 | |||
79 | /** |
||
80 | * Handle response from iDoklad and create model instance. |
||
81 | * |
||
82 | * @param ResponseInterface $response |
||
83 | * |
||
84 | * @throws \InvalidArgumentException |
||
85 | * @throws \Fousky\Component\iDoklad\Exception\InvalidResponseException |
||
86 | * @throws \ReflectionException |
||
87 | * |
||
88 | * @return iDokladModelInterface |
||
89 | */ |
||
90 | public function handleResponse(ResponseInterface $response): iDokladModelInterface |
||
94 | |||
95 | /** |
||
96 | * @param string|iDokladModelInterface $modelClass |
||
97 | * @param ResponseInterface $response |
||
98 | * |
||
99 | * @throws \ReflectionException |
||
100 | * @throws \InvalidArgumentException |
||
101 | * @throws \Fousky\Component\iDoklad\Exception\InvalidResponseException |
||
102 | * |
||
103 | * @return iDokladModelInterface |
||
104 | */ |
||
105 | protected function createModel($modelClass, ResponseInterface $response): iDokladModelInterface |
||
119 | |||
120 | /** |
||
121 | * @param iDokladAbstractModel $model |
||
122 | * |
||
123 | * @throws InvalidModelException |
||
124 | */ |
||
125 | protected function validate(iDokladAbstractModel $model) |
||
147 | |||
148 | /** |
||
149 | * @param array $config |
||
150 | * |
||
151 | * @throws ExceptionInterface |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public static function assertConfiguration(array $config): array |
||
182 | |||
183 | /** |
||
184 | * @param array $config |
||
185 | * |
||
186 | * @return iDokladFunctionInterface |
||
187 | */ |
||
188 | public function setConfig(array $config): iDokladFunctionInterface |
||
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function hasSortable(): bool |
||
202 | |||
203 | /** |
||
204 | * @throws \RuntimeException |
||
205 | * |
||
206 | * @return iDokladSortable |
||
207 | */ |
||
208 | public function getSortable(): iDokladSortable |
||
219 | |||
220 | /** |
||
221 | * @param null|iDokladSortable $sortable |
||
222 | */ |
||
223 | public function setSortable(iDokladSortable $sortable = null) |
||
227 | |||
228 | /** |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function hasPaginator(): bool |
||
235 | |||
236 | /** |
||
237 | * @throws \RuntimeException |
||
238 | * |
||
239 | * @return iDokladPaginator |
||
240 | */ |
||
241 | public function getPaginator(): iDokladPaginator |
||
252 | |||
253 | /** |
||
254 | * @param null|iDokladPaginator $paginator |
||
255 | */ |
||
256 | public function setPaginator(iDokladPaginator $paginator = null) |
||
260 | |||
261 | /** |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function hasFilter(): bool |
||
268 | |||
269 | /** |
||
270 | * @throws \RuntimeException |
||
271 | * |
||
272 | * @return iDokladFilter |
||
273 | */ |
||
274 | public function getFilter(): iDokladFilter |
||
285 | |||
286 | /** |
||
287 | * @param null|iDokladFilter $filter |
||
288 | */ |
||
289 | public function setFilter(iDokladFilter $filter = null) |
||
293 | } |
||
294 |
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.