1 | <?php |
||
29 | class RequestBuilder |
||
30 | { |
||
31 | const RETURN_ALL_ERRORS_IN_EXCEPTION = '\Mcustiel\SimpleRequest\AllErrorsRequestParser'; |
||
32 | const THROW_EXCEPTION_ON_FIRST_ERROR = '\Mcustiel\SimpleRequest\FirstErrorRequestParser'; |
||
33 | |||
34 | /** |
||
35 | * @var \Psr\Cache\CacheItemPoolInterface |
||
36 | */ |
||
37 | private $cache; |
||
38 | /** |
||
39 | * @var ParserGenerator |
||
40 | */ |
||
41 | private $parserGenerator; |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @param \Psr\Cache\CacheItemPoolInterface $cache |
||
46 | * @param \Mcustiel\SimpleRequest\ParserGenerator $parserGenerator |
||
47 | */ |
||
48 | 99 | public function __construct( |
|
55 | |||
56 | /** |
||
57 | * Main method of this class. Used to convert a request to an object of a given class by |
||
58 | * using a requestParser. |
||
59 | * |
||
60 | * @param array|\stdClass $request The request to convert to an object. |
||
61 | * @param string|array $className The class of the object to which the request must be converted. |
||
62 | * @param string $behavior The behaviour of the parser. |
||
63 | */ |
||
64 | 97 | public function parseRequest( |
|
79 | |||
80 | /** |
||
81 | * @param array $request |
||
82 | * @param RequestParser $parserObject |
||
83 | * @param bool $isArray |
||
84 | * |
||
85 | * @return object|object[] |
||
86 | */ |
||
87 | 95 | private function executeParsing(array $request, RequestParser $parserObject, $isArray) |
|
94 | |||
95 | /** |
||
96 | * @param array $request |
||
97 | * @param RequestParser $parserObject |
||
98 | */ |
||
99 | 1 | private function parseArray(array $request, RequestParser $parserObject) |
|
107 | |||
108 | /** |
||
109 | * @param string|array $className |
||
110 | * @param bool $isArray |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 97 | private function getClassName($className, $isArray) |
|
118 | |||
119 | /** |
||
120 | * @param string $className |
||
121 | * @param RequestParser $parser |
||
122 | * |
||
123 | * @return \Mcustiel\SimpleRequest\RequestParser|mixed |
||
124 | */ |
||
125 | 97 | private function generateRequestParserObject($className, RequestParser $parser) |
|
138 | |||
139 | /** |
||
140 | * @param mixed $request |
||
141 | * |
||
142 | * @throws \Mcustiel\SimpleRequest\Exception\InvalidRequestException |
||
143 | * |
||
144 | * @return \stdClass|mixed |
||
145 | */ |
||
146 | 97 | private function sanitizeRequestOrThrowExceptionIfInvalid($request) |
|
156 | } |
||
157 |
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.