Hoa central
Public Member Functions | Public Attributes | Protected Attributes | List of all members
Hoa\Iterator\RegularExpression Class Reference
Inheritance diagram for Hoa\Iterator\RegularExpression:

Public Member Functions

 __construct (\Iterator $iterator, $regex, $mode=self::MATCH, $flags=0, $pregFlags=0)
 
 accept ()
 
 key ()
 
 current ()
 
 setMode ($mode)
 
 setFlags ($flags)
 
 setPregFlags ($pregFlags)
 
 getRegex ()
 
 getMode ()
 
 getFlags ()
 
 getPregFlags ()
 

Public Attributes

const USE_KEY = 1
 
const INVERT_MATCH = 2
 
const MATCH = 0
 
const GET_MATCH = 1
 
const ALL_MATCHES = 2
 
const SPLIT = 3
 
const REPLACE = 4
 
 $replacement = null
 

Protected Attributes

 $_regex = null
 
 $_mode = 0
 
 $_flags = 0
 
 $_pregFlags = 0
 
 $_key = 0
 
 $_current = null
 

Detailed Description

Class .

Re-implement the SPL RegexIterator class. There are too many bugs in php-src and HHVM, so we re-implement it from scratch without extending the existing class.

Inspired by hhvm://hphp/system/php/spl/iterators/RegexIterator.php

Definition at line 51 of file RegularExpression.php.

Constructor & Destructor Documentation

Hoa\Iterator\RegularExpression::__construct ( \Iterator  $iterator,
  $regex,
  $mode = self::MATCH,
  $flags = 0,
  $pregFlags = 0 
)

Constructor.

Parameters
\RecursiveIterator$iteratorThe recursive iterator to apply this regex filter to.
string$regexThe regular expression to match.
int$modeOperation mode, please see the ::setMode method.
int$flagsSpecial flags, please see the ::setFlags method.
int$pregFlagsRegular expression flags, please see constants.
Returns
void

Definition at line 171 of file RegularExpression.php.

177  {
178  parent::__construct($iterator);
179 
180  $this->_regex = $regex;
181  $this->setMode($mode);
182  $this->setFlags($flags);
183  $this->setPregFlags($pregFlags);
184  $this->replacement = null;
185 
186  return;
187  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Iterator\RegularExpression::accept ( )

Get accept status.

Returns
bool

Definition at line 194 of file RegularExpression.php.

195  {
196  if (is_array(parent::current())) {
197  return false;
198  }
199 
200  $this->_key = parent::key();
201  $this->_current = parent::current();
202 
203  $matches = [];
204  $useKey = $this->_flags & self::USE_KEY;
205  $subject = $useKey ? $this->_key : $this->_current;
206  $out = false;
207 
208  switch ($this->_mode) {
209 
210  case self::MATCH:
211  $out = 0 !== preg_match(
212  $this->_regex,
213  $subject,
214  $matches,
215  $this->_pregFlags
216  );
217 
218  break;
219 
220  case self::GET_MATCH:
221  $this->_current = [];
222  $out = 0 !== preg_match(
223  $this->_regex,
224  $subject,
225  $this->_current,
226  $this->_pregFlags
227  );
228 
229  break;
230 
231  case self::ALL_MATCHES:
232  $this->_current = [];
233  $out = 0 < preg_match_all(
234  $this->_regex,
235  $subject,
236  $this->_current,
237  $this->_pregFlags
238  );
239 
240  break;
241 
242  case self::SPLIT:
243  $this->_current = preg_split(
244  $this->_regex,
245  $subject,
246  null,
247  $this->_pregFlags
248  );
249 
250  $out =
251  is_array($this->_current) &&
252  1 < count($this->_current);
253 
254  break;
255 
256  case self::REPLACE:
257  $numberOfReplacement = 0;
258  $result = preg_replace(
259  $this->_regex,
260  $this->replacement,
261  $subject,
262  -1,
263  $numberOfReplacement
264  );
265 
266  if (null === $result || 0 === $numberOfReplacement) {
267  $out = false;
268 
269  break;
270  }
271 
272  if (0 !== $useKey) {
273  $this->_key = $result;
274  $out = true;
275 
276  break;
277  }
278 
279  $this->_current = $result;
280  $out = true;
281 
282  break;
283 
284  default:
285  $out = false;
286 
287  break;
288  }
289 
290  if (0 !== ($this->_flags & self::INVERT_MATCH)) {
291  return false === $out;
292  }
293 
294  return $out;
295  }
Hoa\Iterator\RegularExpression::current ( )

Get current value.

Returns
string

Definition at line 312 of file RegularExpression.php.

313  {
314  return $this->_current;
315  }
Hoa\Iterator\RegularExpression::getFlags ( )

Get flags.

Returns
int

Definition at line 387 of file RegularExpression.php.

388  {
389  return $this->_flags;
390  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::getMode ( )

Get mode.

Returns
int

Definition at line 377 of file RegularExpression.php.

378  {
379  return $this->_mode;
380  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::getPregFlags ( )

Get preg flags.

Returns
int

Definition at line 397 of file RegularExpression.php.

398  {
399  return $this->_pregFlags;
400  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::getRegex ( )

Get regular expression.

Returns
string

Definition at line 367 of file RegularExpression.php.

368  {
369  return $this->_regex;
370  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::key ( )

Get current key.

Returns
int

Definition at line 302 of file RegularExpression.php.

303  {
304  return $this->_key;
305  }
Hoa\Iterator\RegularExpression::setFlags (   $flags)

Set flags.

Parameters
int$flagsFlags.
Returns
void

Definition at line 342 of file RegularExpression.php.

343  {
344  $this->_flags = $flags;
345 
346  return;
347  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::setMode (   $mode)

Set mode.

Parameters
int$modeMode.
Returns
void

Definition at line 323 of file RegularExpression.php.

324  {
325  if ($mode < self::MATCH || $mode > self::REPLACE) {
326  throw new \InvalidArgumentException(
327  'Illegal mode ' . $mode . '.'
328  );
329  }
330 
331  $this->_mode = $mode;
332 
333  return;
334  }

Here is the caller graph for this function:

Hoa\Iterator\RegularExpression::setPregFlags (   $pregFlags)

Set preg flags.

Parameters
int$pregFlagsPreg flags.
Returns
void

Definition at line 355 of file RegularExpression.php.

356  {
357  $this->_pregFlags = $pregFlags;
358 
359  return;
360  }

Here is the caller graph for this function:

Member Data Documentation

Hoa\Iterator\RegularExpression::$_current = null
protected

Definition at line 144 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$_flags = 0
protected

Definition at line 123 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$_key = 0
protected

Definition at line 137 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$_mode = 0
protected

Definition at line 115 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$_pregFlags = 0
protected

Definition at line 130 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$_regex = null
protected

Definition at line 107 of file RegularExpression.php.

Hoa\Iterator\RegularExpression::$replacement = null

Definition at line 151 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::ALL_MATCHES = 2

Mode and preg flag: all matches for the current entry.

int

Definition at line 86 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::GET_MATCH = 1

Mode and preg flag: first match for the current entry.

int

Definition at line 79 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::INVERT_MATCH = 2

Flag: invert match.

int

Definition at line 65 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::MATCH = 0

Mode and preg flag: only execute match (filter) for the current entry.

int

Definition at line 72 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::REPLACE = 4

Mode and preg flag: replace the current entry.

int

Definition at line 100 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::SPLIT = 3

Mode and preg flag: split values for the current entry.

int

Definition at line 93 of file RegularExpression.php.

const Hoa\Iterator\RegularExpression::USE_KEY = 1

Flag: match the entry key instead of the entry value.

int

Definition at line 58 of file RegularExpression.php.


The documentation for this class was generated from the following file: