SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Protected Member Functions | Private Attributes | List of all members
RegexIterator Class Reference
Inheritance diagram for RegexIterator:
Inheritance graph
Collaboration diagram for RegexIterator:
Collaboration graph

Public Member Functions

 __call ($func, $params)
 
 __construct (Iterator $it, $regex, $mode=0, $flags=0, $preg_flags=0)
 
 accept ()
 
 current ()
 
 getFlags ()
 
 getInnerIterator ()
 
 getMode ()
 
 getPregFlags ()
 
 getRegex ()
 
 key ()
 
 next ()
 
 rewind ()
 
 setFlags ($flags)
 
 setMode ($mode)
 
 setPregFlags ($preg_flags)
 
 valid ()
 

Public Attributes

const ALL_MATCHES = 2
 
const GET_MATCH = 1
 
const MATCH = 0
 
const REPLACE = 4
 
const SPLIT = 3
 
const USE_KEY = 0x00000001
 

Protected Member Functions

 __clone ()
 
 fetch ()
 

Private Attributes

 $current
 
 $flags
 
 $key
 
 $mode
 
 $preg_flags
 
 $regex
 

Detailed Description

Regular expression filter for iterators.

Author
Marcus Boerger
Version
1.0
Since
PHP 5.1

This filter iterator assumes that the inner iterator

Definition at line 20 of file regexiterator.inc.

Constructor & Destructor Documentation

RegexIterator::__construct ( Iterator  $it,
  $regex,
  $mode = 0,
  $flags = 0,
  $preg_flags = 0 
)

Constructs a regular expression filter around an iterator whose elemnts or keys are strings.

Parameters
itinner iterator
regexthe regular expression to match
modeoperation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT)
flagsspecial flags (self::USE_KEY)
preg_flagsglobal PREG_* flags, see preg_match(), preg_match_all(), preg_split()

Definition at line 52 of file regexiterator.inc.

References $flags, $mode, $preg_flags, and $regex.

52  {
53  parent::__construct($it);
54  $this->regex = $regex;
55  $this->flags = $flags;
56  $this->mode = $mode;
57  $this->preg_flags = $preg_flags;
58  }
$flags
special flags (self::USE_KEY)
$regex
the regular expression to match against
$mode
operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) ...
$preg_flags
PREG_* flags, see preg_match(), preg_match_all(), preg_split()

Member Function Documentation

FilterIterator::__call (   $func,
  $params 
)
inherited

Aggregate the inner iterator.

Parameters
funcName of method to invoke
paramsArray of parameters to pass to method

Definition at line 121 of file filteriterator.inc.

122  {
123  return call_user_func_array(array($this->it, $func), $params);
124  }
FilterIterator::__clone ( )
protectedinherited

hidden __clone

Definition at line 104 of file filteriterator.inc.

104  {
105  // disallow clone
106  }
RegexIterator::accept ( )

Match current or key against regular expression using mode, flags and preg_flags.

Returns
whether this is a match
Warning
never call this twice for the same state

Definition at line 68 of file regexiterator.inc.

References current(), and key().

69  {
70  $matches = array();
71  $this->key = parent::key();
72  $this->current = parent::current();
73  /* note that we use $this->current, rather than calling parent::current() */
74  $subject = ($this->flags & self::USE_KEY) ? $this->key : $this->current;
75  switch($this->mode)
76  {
77  case self::MATCH:
78  return preg_match($this->regex, $subject, $matches, $this->preg_flags);
79 
80  case self::GET_MATCH:
81  $this->current = array();
82  return preg_match($this->regex, $subject, $this->current, $this->preg_flags) > 0;
83 
84  case self::ALL_MATCHES:
85  $this->current = array();
86  return preg_match_all($this->regex, $subject, $this->current, $this->preg_flags) > 0;
87 
88  case self::SPLIT:
89  $this->current = array();
90  preg_split($this->regex, $subject, $this->current, $this->preg_flags) > 1;
91 
92  case self::REPLACE:
93  $this->current = array();
94  $result = preg_replace($this->regex, $this->replacement, $subject);
95  if ($this->flags & self::USE_KEY)
96  {
97  $this->key = $result;
98  }
99  else
100  {
101  $this->current = $result;
102  }
103  }
104  }

Here is the call graph for this function:

RegexIterator::current ( )
Returns
the current value after accept has been called

Implements Iterator.

Definition at line 115 of file regexiterator.inc.

References $current.

Referenced by accept().

116  {
117  return $this->current;
118  }
$current
the value used for current()
FilterIterator::fetch ( )
protectedinherited

Fetch next element and store it.

Returns
void

Definition at line 61 of file filteriterator.inc.

References FilterIterator\accept().

Referenced by FilterIterator\next(), and FilterIterator\rewind().

61  {
62  while ($this->it->valid()) {
63  if ($this->accept()) {
64  return;
65  }
66  $this->it->next();
67  };
68  }
accept()
Accept function to decide whether an element of the inner iterator should be accessible through the F...

Here is the call graph for this function:

RegexIterator::getFlags ( )
Returns
current operation flags

Definition at line 136 of file regexiterator.inc.

References $flags.

137  {
138  return $this->flags;
139  }
$flags
special flags (self::USE_KEY)
FilterIterator::getInnerIterator ( )
inherited
RegexIterator::getMode ( )
Returns
current operation mode

Definition at line 122 of file regexiterator.inc.

References $mode.

123  {
124  return $this->mode;
125  }
$mode
operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) ...
RegexIterator::getPregFlags ( )
Returns
current PREG flags

Definition at line 150 of file regexiterator.inc.

References $preg_flags.

151  {
152  return $this->preg_flags;
153  }
$preg_flags
PREG_* flags, see preg_match(), preg_match_all(), preg_split()
RegexIterator::getRegex ( )
Returns
current regular expression

Definition at line 164 of file regexiterator.inc.

References $regex.

165  {
166  return $this->regex;
167  }
$regex
the regular expression to match against
RegexIterator::key ( )
Returns
the key after accept has been called

Implements Iterator.

Definition at line 108 of file regexiterator.inc.

References $key.

Referenced by accept().

109  {
110  return $this->key;
111  }
$key
the value used for key()
FilterIterator::next ( )
inherited

Move to next element.

Returns
void

Implements Iterator.

Definition at line 75 of file filteriterator.inc.

References FilterIterator\fetch().

75  {
76  $this->it->next();
77  $this->fetch();
78  }
fetch()
Fetch next element and store it.

Here is the call graph for this function:

FilterIterator::rewind ( )
inherited

Rewind the inner iterator.

Implements Iterator.

Definition at line 42 of file filteriterator.inc.

References FilterIterator\fetch().

42  {
43  $this->it->rewind();
44  $this->fetch();
45  }
fetch()
Fetch next element and store it.

Here is the call graph for this function:

RegexIterator::setFlags (   $flags)
Parameters
flagsnew operaion flags

Definition at line 143 of file regexiterator.inc.

References $flags.

144  {
145  $this->flags = $flags;
146  }
$flags
special flags (self::USE_KEY)
RegexIterator::setMode (   $mode)
Parameters
modenew operaion mode

Definition at line 129 of file regexiterator.inc.

References $mode.

130  {
131  $this->mode = $mode;
132  }
$mode
operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) ...
RegexIterator::setPregFlags (   $preg_flags)
Parameters
preg_flagsnew PREG flags

Definition at line 157 of file regexiterator.inc.

References $preg_flags.

158  {
159  $this->preg_flags = $preg_flags;
160  }
$preg_flags
PREG_* flags, see preg_match(), preg_match_all(), preg_split()
FilterIterator::valid ( )
inherited
Returns
Whether more elements are available

Implements Iterator.

Definition at line 83 of file filteriterator.inc.

83  {
84  return $this->it->valid();
85  }

Member Data Documentation

RegexIterator::$current
private

the value used for current()

Definition at line 38 of file regexiterator.inc.

Referenced by current().

RegexIterator::$flags
private

special flags (self::USE_KEY)

Definition at line 34 of file regexiterator.inc.

Referenced by RecursiveRegexIterator\__construct(), __construct(), getFlags(), and setFlags().

RegexIterator::$key
private

the value used for key()

Definition at line 37 of file regexiterator.inc.

Referenced by key().

RegexIterator::$mode
private

operation mode (one of self::MATCH, self::GET_MATCH, self::ALL_MATCHES, self::SPLIT)

Definition at line 32 of file regexiterator.inc.

Referenced by RecursiveRegexIterator\__construct(), __construct(), getMode(), and setMode().

RegexIterator::$preg_flags
private

PREG_* flags, see preg_match(), preg_match_all(), preg_split()

Definition at line 35 of file regexiterator.inc.

Referenced by RecursiveRegexIterator\__construct(), __construct(), getPregFlags(), and setPregFlags().

RegexIterator::$regex
private

the regular expression to match against

Definition at line 31 of file regexiterator.inc.

Referenced by RecursiveRegexIterator\__construct(), __construct(), and getRegex().

const RegexIterator::ALL_MATCHES = 2

Mode: Return all matches (if any)

Definition at line 27 of file regexiterator.inc.

const RegexIterator::GET_MATCH = 1

Mode: Return the first matche (if any)

Definition at line 26 of file regexiterator.inc.

const RegexIterator::MATCH = 0

Mode: Executed a plain match only.

Definition at line 25 of file regexiterator.inc.

const RegexIterator::REPLACE = 4

Mode: Replace the input key or current.

Definition at line 29 of file regexiterator.inc.

const RegexIterator::SPLIT = 3

Mode: Return the split values (if any)

Definition at line 28 of file regexiterator.inc.

const RegexIterator::USE_KEY = 0x00000001

If present in $flags the key is used rather then the current value.

Definition at line 22 of file regexiterator.inc.


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