1 | <?php |
||
25 | class HierarchicalPath extends AbstractHierarchicalComponent implements HierarchicalPathInterface |
||
26 | { |
||
27 | use PathTrait; |
||
28 | |||
29 | /** |
||
30 | * @inheritdoc |
||
31 | */ |
||
32 | protected static $separator = '/'; |
||
33 | |||
34 | /** |
||
35 | * DEPRECATION WARNING! This method will be removed in the next major point release |
||
36 | * |
||
37 | * @deprecated deprecated since version 4.2 |
||
38 | * |
||
39 | * return a new instance from an array or a traversable object |
||
40 | * |
||
41 | * @param \Traversable|string[] $data The segments list |
||
42 | * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE |
||
43 | * |
||
44 | * @throws InvalidArgumentException If $type is not a recognized constant |
||
45 | * |
||
46 | * @return static |
||
47 | */ |
||
48 | public static function createFromArray($data, $type = self::IS_RELATIVE) |
||
52 | |||
53 | /** |
||
54 | * return a new instance from an array or a traversable object |
||
55 | * |
||
56 | * @param \Traversable|string[] $data The segments list |
||
57 | * @param int $type one of the constant IS_ABSOLUTE or IS_RELATIVE |
||
58 | * |
||
59 | * @throws InvalidArgumentException If $type is not a recognized constant |
||
60 | * |
||
61 | * @return static |
||
62 | */ |
||
63 | 237 | public static function createFromSegments($data, $type = self::IS_RELATIVE) |
|
78 | |||
79 | /** |
||
80 | * New Instance |
||
81 | * |
||
82 | * @param string $path |
||
83 | */ |
||
84 | 1009 | public function __construct($path = '') |
|
104 | |||
105 | /** |
||
106 | * validate the submitted data |
||
107 | * |
||
108 | * @param string $data |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | protected function validate($data) |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | 9 | public static function __set_state(array $properties) |
|
130 | |||
131 | /** |
||
132 | * Return a new instance when needed |
||
133 | * |
||
134 | * @param array $data |
||
135 | * |
||
136 | * @return static |
||
137 | */ |
||
138 | 63 | protected function newCollectionInstance(array $data) |
|
142 | |||
143 | /** |
||
144 | * Retrieves a single path segment. |
||
145 | * |
||
146 | * Retrieves a single path segment. If the segment offset has not been set, |
||
147 | * returns the default value provided. |
||
148 | * |
||
149 | * @param string $offset the segment offset |
||
150 | * @param mixed $default Default value to return if the offset does not exist. |
||
151 | * |
||
152 | * @return mixed |
||
153 | */ |
||
154 | 9 | public function getSegment($offset, $default = null) |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 967 | public function getContent() |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 967 | public function __toString() |
|
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | */ |
||
189 | 30 | public function prepend($component) |
|
196 | |||
197 | /** |
||
198 | * Returns an instance with the specified component appended |
||
199 | * |
||
200 | * This method MUST retain the state of the current instance, and return |
||
201 | * an instance that contains the modified component with the appended data |
||
202 | * |
||
203 | * @param HierarchicalComponent|string $component the component to append |
||
204 | * |
||
205 | * @return static |
||
206 | */ |
||
207 | 72 | public function append($component) |
|
219 | |||
220 | /** |
||
221 | * Returns the path basename |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | 63 | public function getBasename() |
|
231 | |||
232 | /** |
||
233 | * Returns parent directory's path |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 24 | public function getDirname() |
|
245 | |||
246 | /** |
||
247 | * Returns the basename extension |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 57 | public function getExtension() |
|
257 | |||
258 | /** |
||
259 | * Returns an instance with the specified basename extension |
||
260 | * |
||
261 | * This method MUST retain the state of the current instance, and return |
||
262 | * an instance that contains the extension basename modified. |
||
263 | * |
||
264 | * @param string $extension the new extension |
||
265 | * can preceeded with or without the dot (.) character |
||
266 | * |
||
267 | * @throws InvalidArgumentException If the extension is invalid |
||
268 | * |
||
269 | * @return static |
||
270 | */ |
||
271 | 63 | public function withExtension($extension) |
|
290 | |||
291 | /** |
||
292 | * create a new basename with a new extension |
||
293 | * |
||
294 | * @param string $basenamePart the basename file part |
||
295 | * @param string $extension the new extension to add |
||
296 | * @param string $parameterPart the basename parameter part |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 51 | protected function buildBasename($basenamePart, $extension, $parameterPart) |
|
319 | |||
320 | /** |
||
321 | * validate and format the given extension |
||
322 | * |
||
323 | * @param string $extension the new extension to use |
||
324 | * |
||
325 | * @throws InvalidArgumentException If the extension is not valid |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | 63 | protected function formatExtension($extension) |
|
341 | } |
||
342 |
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.