xref: /PHP-7.0/ext/spl/internal/regexiterator.inc (revision 1b9e0de2)
1<?php
2
3/** @file regexiterator.inc
4 * @ingroup SPL
5 * @brief class RegexIterator
6 * @author  Marcus Boerger
7 * @date    2003 - 2009
8 *
9 * SPL - Standard PHP Library
10 */
11
12/**
13 * @brief   Regular expression filter for iterators
14 * @author  Marcus Boerger
15 * @version 1.0
16 * @since PHP 5.1
17 *
18 * This filter iterator assumes that the inner iterator
19 */
20class RegexIterator extends FilterIterator
21{
22	const USE_KEY     = 0x00000001; /**< If present in $flags the key is
23	                                     used rather then the current value. */
24
25	const MATCH       = 0; /**< Mode: Executed a plain match only      */
26	const GET_MATCH   = 1; /**< Mode: Return the first matche (if any) */
27	const ALL_MATCHES = 2; /**< Mode: Return all matches (if any)      */
28	const SPLIT       = 3; /**< Mode: Return the split values (if any) */
29	const REPLACE     = 4; /**< Mode: Replace the input key or current */
30
31	private $regex;     /**< the regular expression to match against */
32	private $mode;      /**< operation mode (one of self::MATCH,
33	                         self::GET_MATCH, self::ALL_MATCHES, self::SPLIT) */
34	private $flags;     /**< special flags (self::USE_KEY) */
35	private $preg_flags;/**< PREG_* flags, see preg_match(), preg_match_all(),
36	                         preg_split() */
37	private $key;       /**< the value used for key() */
38	private $current;   /**< the value used for current() */
39
40	/**
41	 * Constructs a regular expression filter around an iterator whose
42	 * elemnts or keys are strings.
43	 *
44	 * @param it          inner iterator
45	 * @param regex       the regular expression to match
46	 * @param mode        operation mode (one of self::MATCH, self::GET_MATCH,
47	 *                    self::ALL_MATCHES, self::SPLIT)
48	 * @param flags       special flags (self::USE_KEY)
49	 * @param preg_flags  global PREG_* flags, see preg_match(),
50	 *                    preg_match_all(), preg_split()
51	 */
52	function __construct(Iterator $it, $regex, $mode = 0, $flags = 0, $preg_flags = 0) {
53		parent::__construct($it);
54		$this->regex = $regex;
55		$this->flags = $flags;
56		$this->mode = $mode;
57		$this->preg_flags = $preg_flags;
58	}
59
60	/**
61	 * Match current or key against regular expression using mode, flags and
62	 * preg_flags.
63	 *
64	 * @return whether this is a match
65	 *
66	 * @warning never call this twice for the same state
67	 */
68	function accept()
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	}
105
106	/** @return the key after accept has been called
107	 */
108	function key()
109	{
110		return $this->key;
111	}
112
113	/** @return the current value after accept has been called
114	 */
115	function current()
116	{
117		return $this->current;
118	}
119
120	/** @return current operation mode
121	 */
122	function getMode()
123	{
124		return $this->mode;
125	}
126
127	/** @param mode new operaion mode
128	 */
129	function setMode($mode)
130	{
131		$this->mode = $mode;
132	}
133
134	/** @return current operation flags
135	 */
136	function getFlags()
137	{
138		return $this->flags;
139	}
140
141	/** @param flags new operaion flags
142	 */
143	function setFlags($flags)
144	{
145		$this->flags = $flags;
146	}
147
148	/** @return current PREG flags
149	 */
150	function getPregFlags()
151	{
152		return $this->preg_flags;
153	}
154
155	/** @param preg_flags new PREG flags
156	 */
157	function setPregFlags($preg_flags)
158	{
159		$this->preg_flags = $preg_flags;
160	}
161
162	/** @return current regular expression
163	*/
164	function getRegex()
165	{
166		return $this->regex;
167	}
168}
169
170?>
171