1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Iterator\Recursive;
38
39use Hoa\Iterator;
40
41/**
42 * Class \Hoa\Iterator\Recursive\RegularExpression.
43 *
44 * Re-implement the SPL RecursiveRegexIterator class.
45 * There are too many bugs in php-src and HHVM, so we re-implement it from
46 * scratch without extending the existing class.
47 *
48 * Inspired by hhvm://hphp/system/php/spl/iterators/RecursiveRegexIterator.php
49 *
50 * @copyright  Copyright © 2007-2017 Hoa community
51 * @license    New BSD License
52 */
53class RegularExpression extends Iterator\RegularExpression implements Recursive
54{
55    /**
56     * Constructor.
57     *
58     * @param   \RecursiveIterator  $iterator     The recursive iterator to
59     *                                            apply this regex filter to.
60     * @param   string              $regex        The regular expression to
61     *                                            match.
62     * @param   int                 $mode         Operation mode, please see the
63     *                                            \RegexIterator::setMode method.
64     * @param   int                 $flags        Special flags, please see the
65     *                                            \RegexIterator::setFlags method.
66     * @param   int                 $pregFlags    Regular expression flags,
67     *                                            please see
68     *                                            \RegexIterator constants.
69     */
70    public function __construct(
71        \RecursiveIterator $iterator,
72        $regex,
73        $mode      = self::MATCH,
74        $flags     = 0,
75        $pregFlags = 0
76    ) {
77        parent::__construct($iterator, $regex, $mode, $flags, $pregFlags);
78
79        return;
80    }
81
82    /**
83     * Get accept status.
84     *
85     * @return  bool
86     */
87    public function accept()
88    {
89        return
90            true === $this->hasChildren() ||
91            true === parent::accept();
92    }
93
94    /**
95     * Get an iterator for the current entry.
96     *
97     * @return  \Hoa\Iterator\Recursive\RegularExpression
98     */
99    public function getChildren()
100    {
101        return new static(
102            true === $this->hasChildren()
103                ? $this->getInnerIterator()->getChildren()
104                : null,
105            $this->getRegex(),
106            $this->getMode(),
107            $this->getFlags(),
108            $this->getPregFlags()
109        );
110    }
111
112    /**
113     * Check whether an iterator can be obtained for the current entry.
114     *
115     * @return  bool
116     */
117    public function hasChildren()
118    {
119        return $this->getInnerIterator()->hasChildren();
120    }
121}
122