xref: /PHP-5.6/ext/spl/examples/keyfilter.inc (revision 1b9e0de2)
1<?php
2
3/** @file keyfilter.inc
4 * @ingroup Examples
5 * @brief class KeyFilter
6 * @author  Marcus Boerger
7 * @date    2003 - 2005
8 *
9 * SPL - Standard PHP Library
10 */
11
12/** @ingroup Examples
13 * @brief   Regular expression filter for string iterators
14 * @author  Marcus Boerger
15 * @version 1.1
16 *
17 * Instances of this class act as a filter around iterators whose elements
18 * are strings. In other words you can put an iterator into the constructor
19 * and the instance will only return elements which match the given regular
20 * expression.
21 */
22class KeyFilter extends FilterIterator
23{
24	/** @internal regular exoression used as filter */
25	private $regex;
26
27	/**
28	 * Constructs a filter around an iterator whose elemnts are strings.
29	 * If the given iterator is of type spl_sequence then its rewind()
30	 * method is called.
31	 *
32	 * @param it     Object that implements at least spl_forward
33	 * @param regex  Regular expression used as a filter.
34	 */
35	function __construct(Iterator $it, $regex)
36	{
37		parent::__construct($it);
38		$this->regex = $regex;
39	}
40
41	/** \return whether the current key mathes the regular expression
42	 */
43	function accept()
44	{
45		return ereg($this->regex, $this->getInnerIterator()->key());
46	}
47
48	/** @return regular expression used as filter
49	 */
50	function getRegex()
51	{
52		return $this->regex;
53	}
54
55	/**
56	 * hidden __clone
57	 */
58	protected function __clone()
59	{
60		// disallow clone
61	}
62}
63
64?>