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 | * @inheritdoc |
||
36 | */ |
||
37 | protected static $invalidCharactersRegex = ',[?#],'; |
||
38 | |||
39 | /** |
||
40 | * New Instance |
||
41 | * |
||
42 | * @param string $path |
||
43 | */ |
||
44 | 908 | public function __construct($path = '') |
|
45 | { |
||
46 | 908 | $path = $this->validateString($path); |
|
47 | 899 | $this->assertValidComponent($path); |
|
48 | 893 | $this->isAbsolute = self::IS_RELATIVE; |
|
49 | 893 | if (static::$separator == mb_substr($path, 0, 1, 'UTF-8')) { |
|
50 | 721 | $this->isAbsolute = self::IS_ABSOLUTE; |
|
51 | 721 | $path = mb_substr($path, 1, mb_strlen($path), 'UTF-8'); |
|
52 | 721 | } |
|
53 | |||
54 | 893 | $append_delimiter = false; |
|
55 | 893 | if (static::$separator === mb_substr($path, -1, 1, 'UTF-8')) { |
|
56 | 105 | $path = mb_substr($path, 0, -1, 'UTF-8'); |
|
57 | 105 | $append_delimiter = true; |
|
58 | 105 | } |
|
59 | |||
60 | 893 | $this->data = $this->validate($path); |
|
61 | 893 | if ($append_delimiter) { |
|
62 | 105 | $this->data[] = ''; |
|
63 | 105 | } |
|
64 | 893 | } |
|
65 | |||
66 | /** |
||
67 | * validate the submitted data |
||
68 | * |
||
69 | * @param string $data |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | protected function validate($data) |
||
83 | |||
84 | /** |
||
85 | * Retrieves a single path segment. |
||
86 | * |
||
87 | * Retrieves a single path segment. If the segment offset has not been set, |
||
88 | * returns the default value provided. |
||
89 | * |
||
90 | * @param string $offset the segment offset |
||
91 | * @param mixed $default Default value to return if the offset does not exist. |
||
92 | * |
||
93 | * @return mixed |
||
94 | */ |
||
95 | 9 | public function getSegment($offset, $default = null) |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 9 | public static function __set_state(array $properties) |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 860 | public function getContent() |
|
116 | { |
||
117 | 860 | $front_delimiter = ''; |
|
118 | 860 | if ($this->isAbsolute == self::IS_ABSOLUTE) { |
|
119 | 679 | $front_delimiter = static::$separator; |
|
120 | 679 | } |
|
121 | |||
122 | 860 | return $front_delimiter.$this->encodePath(implode(static::$separator, $this->data)); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns an instance with the specified component appended |
||
127 | * |
||
128 | * This method MUST retain the state of the current instance, and return |
||
129 | * an instance that contains the modified component with the appended data |
||
130 | * |
||
131 | * @param HierarchicalComponent|string $component the component to append |
||
132 | * |
||
133 | * @return static |
||
134 | */ |
||
135 | 72 | public function append($component) |
|
147 | |||
148 | /** |
||
149 | * Returns the path basename |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | 63 | public function getBasename() |
|
159 | |||
160 | /** |
||
161 | * Returns parent directory's path |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 24 | public function getDirname() |
|
173 | |||
174 | /** |
||
175 | * Returns the basename extension |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 57 | public function getExtension() |
|
185 | |||
186 | /** |
||
187 | * Returns an instance with the specified basename extension |
||
188 | * |
||
189 | * This method MUST retain the state of the current instance, and return |
||
190 | * an instance that contains the extension basename modified. |
||
191 | * |
||
192 | * @param string $extension the new extension |
||
193 | * can preceeded with or without the dot (.) character |
||
194 | * |
||
195 | * @throws InvalidArgumentException If the extension is invalid |
||
196 | * |
||
197 | * @return static |
||
198 | */ |
||
199 | 63 | public function withExtension($extension) |
|
218 | |||
219 | /** |
||
220 | * create a new basename with a new extension |
||
221 | * |
||
222 | * @param string $basenamePart the basename file part |
||
223 | * @param string $extension the new extension to add |
||
224 | * @param string $parameterPart the basename parameter part |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 51 | protected function buildBasename($basenamePart, $extension, $parameterPart) |
|
247 | |||
248 | /** |
||
249 | * validate and format the given extension |
||
250 | * |
||
251 | * @param string $extension the new extension to use |
||
252 | * |
||
253 | * @throws InvalidArgumentException If the extension is not valid |
||
254 | * |
||
255 | * @return string |
||
256 | */ |
||
257 | 63 | protected function formatExtension($extension) |
|
269 | } |
||
270 |