summaryrefslogtreecommitdiff
path: root/ext/spl/internal
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2006-07-16 21:12:32 +0000
committerMarcus Boerger <helly@php.net>2006-07-16 21:12:32 +0000
commit55f0596fabfe9613d535adcb19caa820b2104e51 (patch)
tree6bd3b70c35d123b9c900278cb2adcde0be3d1253 /ext/spl/internal
parentbf4c9ef68616eb6998154b04337bbbe2ffac67fb (diff)
downloadphp-git-55f0596fabfe9613d535adcb19caa820b2104e51.tar.gz
- MFH:
. Upgrade RegexIterator capabilities, see docu . Update docu . Add test (which fails right now: iterator_049.phpt) . Add tests for new functionality
Diffstat (limited to 'ext/spl/internal')
-rwxr-xr-xext/spl/internal/filteriterator.inc10
-rwxr-xr-xext/spl/internal/recursiveregexiterator.inc55
-rwxr-xr-xext/spl/internal/regexiterator.inc90
3 files changed, 149 insertions, 6 deletions
diff --git a/ext/spl/internal/filteriterator.inc b/ext/spl/internal/filteriterator.inc
index cc9c999a40..9820d48318 100755
--- a/ext/spl/internal/filteriterator.inc
+++ b/ext/spl/internal/filteriterator.inc
@@ -4,13 +4,13 @@
* @ingroup SPL
* @brief class FilterIterator
* @author Marcus Boerger
- * @date 2003 - 2005
+ * @date 2003 - 2006
*
* SPL - Standard PHP Library
*/
/**
- * @brief Regular expression filter for string iterators
+ * @brief Abstract filter for iterators
* @author Marcus Boerger
* @version 1.1
* @since PHP 5.0
@@ -28,11 +28,9 @@ abstract class FilterIterator implements OuterIterator
private $it;
/**
- * Constructs a filter around an iterator whose elemnts are strings.
- * If the given iterator is of type spl_sequence then its rewind()
- * method is called.
+ * Constructs a filter around another iterator.
*
- * @param it Object that implements at least spl_forward
+ * @param it Iterator to filter
*/
function __construct(Iterator $it) {
$this->it = $it;
diff --git a/ext/spl/internal/recursiveregexiterator.inc b/ext/spl/internal/recursiveregexiterator.inc
new file mode 100755
index 0000000000..caaee97b54
--- /dev/null
+++ b/ext/spl/internal/recursiveregexiterator.inc
@@ -0,0 +1,55 @@
+<?php
+
+/** @file recursiveregexiterator.inc
+ * @ingroup SPL
+ * @brief class RegexIterator
+ * @author Marcus Boerger
+ * @date 2003 - 2006
+ *
+ * SPL - Standard PHP Library
+ */
+
+/**
+ * @brief Recursive regular expression filter for iterators
+ * @author Marcus Boerger
+ * @version 1.0
+ * @since PHP 5.1
+ *
+ * This filter iterator assumes that the inner iterator
+ */
+class RecursiveRegexIterator extends RegexIterator implements RecursiveIterator
+{
+ /**
+ * Constructs a regular expression filter around an iterator whose
+ * elemnts or keys are strings.
+ *
+ * @param it Object that implements at least
+ */
+ function __construct(RecursiveIterator $it, $regex, $flags = 0, $mode = 0, $preg_flags = 0) {
+ parent::__construct($it, $regex, $flags, $mode, $preg_flags);
+ }
+
+ /** @return whether the current element has children
+ */
+ function hasChildren()
+ {
+ return $this->getInnerIterator()->hasChildren();
+ }
+
+ /** @return an iterator for the current elements children
+ *
+ * @note the returned iterator will be of the same class as $this
+ */
+ function getChildren()
+ {
+ if (empty($this->ref))
+ {
+ $this->ref = new ReflectionClass($this);
+ }
+ return $this->ref->newInstance($this->getInnerIterator()->getChildren());
+ }
+
+ private $ref;
+}
+
+?> \ No newline at end of file
diff --git a/ext/spl/internal/regexiterator.inc b/ext/spl/internal/regexiterator.inc
new file mode 100755
index 0000000000..84ebf986d2
--- /dev/null
+++ b/ext/spl/internal/regexiterator.inc
@@ -0,0 +1,90 @@
+<?php
+
+/** @file regexiterator.inc
+ * @ingroup SPL
+ * @brief class RegexIterator
+ * @author Marcus Boerger
+ * @date 2003 - 2006
+ *
+ * SPL - Standard PHP Library
+ */
+
+/**
+ * @brief Regular expression filter for iterators
+ * @author Marcus Boerger
+ * @version 1.1
+ * @since PHP 5.1
+ *
+ * This filter iterator assumes that the inner iterator
+ */
+class RegexIterator implements FilterIterator
+{
+ const USE_KEY = 0x00000001;
+
+ const MATCH = 0;
+ const GET_MATCH = 1;
+ const ALL_MATCHES = 2;
+ const SPLIT = 3;
+
+ private $regex; /**< the regular expression to match against */
+ private $flags; /**< special flags (USE_KEY) */
+ private $mode; /**< operation mode (MATCH, GET_MATCH, ALL_MATCHES, SPLIT) */
+ private $preg_flags;/**< PREG_* flags, see preg_match(), preg_match_all(), preg_split() */
+ private $current; /**< the value used for current() */
+
+ /**
+ * Constructs a regular expression filter around an iterator whose
+ * elemnts or keys are strings.
+ *
+ * @param it Object that implements at least
+ */
+ function __construct(Iterator $it, $regex, $flags = 0, $mode = 0, $preg_flags = 0) {
+ parent::__construct($it);
+ $this->regex = $regex;
+ $this->flags = $flags;
+ $this->mode = $mode;
+ $this->preg_flags = $preg_flags;
+ }
+
+ /**
+ * Match current or key against regular expression using mode, flags and
+ * preg_flags.
+ *
+ * @return whether this is a match
+ *
+ * @warning never call this twice for the same state
+ */
+ function accept()
+ {
+ $matches = array();
+ $this->current = parent::current();
+ /* note that we use $this->current, rather than calling parent::current() */
+ $subject = ($this->flags & self::USE_KEY) ? parent::key() : $this->current;
+ switch($this->mode)
+ {
+ case self::MATCH:
+ return preg_match($this->regex, $subject, $matches, $this->preg_flags);
+
+ case self::GET_MATCH:
+ $this->current = array();
+ return preg_match($this->regex, $subject, $this->current, $this->preg_flags) > 0;
+
+ case self::ALL_MATCHES:
+ $this->current = array();
+ return preg_match_all($this->regex, $subject, $this->current, $this->preg_flags) > 0;
+
+ case self::SPLIT:
+ $this->current = array();
+ preg_split($this->regex, $subject, $this->current, $this->preg_flags) > 1;
+ }
+ }
+
+ /** @return the current value after accept has been called
+ */
+ function current()
+ {
+ return $this->current;
+ }
+}
+
+?> \ No newline at end of file