1 | <?php |
||
26 | trait StreamFilter |
||
27 | { |
||
28 | /** |
||
29 | * collection of stream filters |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $stream_filters = []; |
||
34 | |||
35 | /** |
||
36 | * Stream filtering mode to apply on all filters |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | protected $stream_filter_mode = STREAM_FILTER_ALL; |
||
41 | |||
42 | /** |
||
43 | *the real path |
||
44 | * |
||
45 | * @var string the real path to the file |
||
46 | * |
||
47 | */ |
||
48 | protected $stream_uri; |
||
49 | |||
50 | /** |
||
51 | * PHP Stream Filter Regex |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $stream_regex = ',^ |
||
56 | php://filter/ |
||
57 | (?P<mode>:?read=|write=)? # The resource open mode |
||
58 | (?P<filters>.*?) # The resource registered filters |
||
59 | /resource=(?P<resource>.*) # The resource path |
||
60 | $,ix'; |
||
61 | |||
62 | /** |
||
63 | * Internal path setter |
||
64 | * |
||
65 | * The path must be an SplFileInfo object |
||
66 | * an object that implements the `__toString` method |
||
67 | * a path to a file |
||
68 | * |
||
69 | * @param \SplFileObject|string $path The file path |
||
70 | */ |
||
71 | 357 | protected function initStreamFilter($path) |
|
72 | { |
||
73 | 357 | $this->stream_filters = []; |
|
74 | 357 | if (!is_string($path)) { |
|
75 | 315 | $this->stream_uri = null; |
|
76 | |||
77 | 315 | return; |
|
78 | } |
||
79 | |||
80 | 48 | if (!preg_match($this->stream_regex, $path, $matches)) { |
|
81 | 33 | $this->stream_uri = $path; |
|
82 | |||
83 | 33 | return; |
|
84 | } |
||
85 | 15 | $this->stream_uri = $matches['resource']; |
|
86 | 15 | $this->stream_filters = explode('|', $matches['filters']); |
|
87 | 15 | $this->stream_filter_mode = $this->fetchStreamModeAsInt($matches['mode']); |
|
88 | 15 | } |
|
89 | |||
90 | /** |
||
91 | * Get the stream mode |
||
92 | * |
||
93 | * @param string $mode |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | 15 | protected function fetchStreamModeAsInt($mode) |
|
111 | |||
112 | /** |
||
113 | * Check if the trait methods can be used |
||
114 | * |
||
115 | * @throws LogicException If the API can not be use |
||
116 | */ |
||
117 | 57 | protected function assertStreamable() |
|
123 | |||
124 | /** |
||
125 | * Tells whether the stream filter capabilities can be used |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 9 | public function isActiveStreamFilter() |
|
133 | |||
134 | /** |
||
135 | * stream filter mode Setter |
||
136 | * |
||
137 | * Set the new Stream Filter mode and remove all |
||
138 | * previously attached stream filters |
||
139 | * |
||
140 | * @param int $mode |
||
141 | * |
||
142 | * @throws OutOfBoundsException If the mode is invalid |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | 6 | public function setStreamFilterMode($mode) |
|
158 | |||
159 | /** |
||
160 | * stream filter mode getter |
||
161 | * |
||
162 | * @return int |
||
163 | */ |
||
164 | 15 | public function getStreamFilterMode() |
|
170 | |||
171 | /** |
||
172 | * append a stream filter |
||
173 | * |
||
174 | * @param string $filter_name a string or an object that implements the '__toString' method |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | 24 | public function appendStreamFilter($filter_name) |
|
185 | |||
186 | /** |
||
187 | * prepend a stream filter |
||
188 | * |
||
189 | * @param string $filter_name a string or an object that implements the '__toString' method |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | 9 | public function prependStreamFilter($filter_name) |
|
200 | |||
201 | /** |
||
202 | * Sanitize the stream filter name |
||
203 | * |
||
204 | * @param string $filter_name the stream filter name |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | 21 | protected function sanitizeStreamFilter($filter_name) |
|
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | abstract public function validateString($str); |
||
219 | |||
220 | /** |
||
221 | * Detect if the stream filter is already present |
||
222 | * |
||
223 | * @param string $filter_name |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 18 | public function hasStreamFilter($filter_name) |
|
233 | |||
234 | /** |
||
235 | * Remove a filter from the collection |
||
236 | * |
||
237 | * @param string $filter_name |
||
238 | * |
||
239 | * @return $this |
||
240 | */ |
||
241 | 3 | public function removeStreamFilter($filter_name) |
|
251 | |||
252 | /** |
||
253 | * Remove all registered stream filter |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | 3 | public function clearStreamFilter() |
|
264 | |||
265 | /** |
||
266 | * Return the filter path |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 33 | protected function getStreamFilterPath() |
|
282 | |||
283 | /** |
||
284 | * Return PHP stream filter prefix |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 21 | protected function getStreamFilterPrefix() |
|
300 | } |
||
301 |