Hoa central
Filter.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Stream\Filter;
38 
39 use Hoa\Core;
40 use Hoa\Stream;
41 
50 abstract class Filter extends Stream
51 {
57  const OVERWRITE = true;
58 
64  const DO_NOT_OVERWRITE = false;
65 
71  const READ = STREAM_FILTER_READ;
72 
78  const WRITE = STREAM_FILTER_WRITE;
79 
85  const READ_AND_WRITE = STREAM_FILTER_ALL;
86 
92  protected static $_resources = [];
93 
94 
95 
106  public static function register(
107  $name,
108  $class,
109  $overwrite = self::DO_NOT_OVERWRITE
110  ) {
111  if ($overwrite === self::DO_NOT_OVERWRITE &&
112  true === self::isRegistered($name)) {
113  throw new Exception('Filter %s is already registered.', 0, $name);
114  }
115 
116  if (empty($name)) {
117  throw new Exception('Filter name cannot be empty.', 1);
118  }
119 
120  if (is_object($class)) {
121  $class = get_class($class);
122  }
123 
124  return stream_filter_register($name, $class);
125  }
126 
139  public static function append(
140  $stream,
141  $name,
142  $mode = self::READ, $parameters = null
143  ) {
144  if ($stream instanceof Stream) {
145  $stream = $stream->getStream();
146  }
147 
148  if (null === $parameters) {
149  return self::$_resources[$name] = stream_filter_append(
150  $stream,
151  $name,
152  $mode
153  );
154  }
155 
156  return self::$_resources[$name] = stream_filter_append(
157  $stream,
158  $name,
159  $mode,
160  $parameters
161  );
162  }
163 
176  public static function prepend(
177  $stream,
178  $name,
179  $mode = self::READ, $parameters = null
180  ) {
181  if ($stream instanceof Stream) {
182  $stream = $stream->getStream();
183  }
184 
185  if (null === $parameters) {
186  return self::$_resources[$name] = stream_filter_prepend(
187  $stream,
188  $name,
189  $mode
190  );
191  }
192 
193  return self::$_resources[$name] = stream_filter_prepend(
194  $stream,
195  $name,
196  $mode,
197  $parameters
198  );
199  }
200 
208  public static function remove($streamFilter)
209  {
210  if (!is_resource($streamFilter)) {
211  if (isset(self::$_resources[$streamFilter])) {
212  $streamFilter = self::$_resources[$streamFilter];
213  } else {
214  throw new Exception(
215  'Cannot remove the stream filter %s because no resource was ' .
216  'found with this name.',
217  2,
218  $streamFilter
219  );
220  }
221  }
222 
223  return stream_filter_remove($streamFilter);
224  }
225 
232  public static function isRegistered($name)
233  {
234  return in_array($name, self::getRegistered());
235  }
236 
242  public static function getRegistered()
243  {
244  return stream_get_filters();
245  }
246 }
247 
251 Core\Consistency::flexEntity('Hoa\Stream\Filter\Filter');
static prepend($stream, $name, $mode=self::READ, $parameters=null)
Definition: Filter.php:176
static isRegistered($name)
Definition: Filter.php:232
static append($stream, $name, $mode=self::READ, $parameters=null)
Definition: Filter.php:139