PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/script/lib/phing/util/regexp/PregEngine.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 183 lines | 68 code | 17 blank | 98 comment | 2 complexity | 10425f7cb1a863d800677f79ede0e461 MD5 | raw file
  1<?php
  2/* 
  3 *  $Id: PregEngine.php 782 2010-05-14 14:52:51Z mrook $
  4 *
  5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 16 *
 17 * This software consists of voluntary contributions made by many individuals
 18 * and is licensed under the LGPL. For more information please see
 19 * <http://phing.info>. 
 20 */
 21
 22require_once 'phing/util/regexp/RegexpEngine.php';
 23
 24/**
 25 * PREG Regexp Engine.
 26 * Implements a regexp engine using PHP's preg_match(), preg_match_all(), and preg_replace() functions.
 27 * 
 28 * @author hans lellelid, hans@velum.net
 29 * @package phing.util.regex
 30 */
 31class PregEngine implements RegexpEngine
 32{
 33    
 34    /**
 35     * Set to null by default to distinguish between false and not set
 36     * @var boolean
 37     */
 38    private $ignoreCase = null;
 39    
 40    /**
 41     * Set to null by default to distinguish between false and not set
 42     * @var boolean
 43     */
 44    private $multiline = null;
 45    
 46    /**
 47     * Pattern modifiers 
 48     * @link http://php.net/manual/en/reference.pcre.pattern.modifiers.php
 49     * @var string
 50     */
 51    private $modifiers = null;
 52
 53    /**
 54     * Sets pattern modifiers for regex engine
 55     *
 56     * @param string $mods Modifiers to be applied to a given regex
 57     * @return void
 58     */
 59    public function setModifiers($mods)
 60    {
 61        $this->modifiers = (string) $mods;
 62    }
 63
 64    /**
 65     * Gets pattern modifiers.
 66     * @return string
 67     */
 68    public function getModifiers()
 69    {
 70        $mods = $this->modifiers;
 71        if ($this->getIgnoreCase())
 72        {
 73            $mods .= 'i';
 74        }
 75        elseif ($this->getIgnoreCase() === false)
 76        {
 77            $mods = str_replace('i', '', $mods);
 78        }
 79        if ($this->getMultiline())
 80        {
 81            $mods .= 's';
 82        }
 83        elseif ($this->getMultiline() === false)
 84        {
 85            $mods = str_replace('s', '', $mods);
 86        }
 87        // filter out duplicates
 88        $mods = preg_split('//', $mods, - 1, PREG_SPLIT_NO_EMPTY);
 89        $mods = implode('', array_unique($mods));
 90        return $mods;
 91    }
 92
 93    /**
 94     * Sets whether or not regex operation is case sensitive.
 95     * @param boolean $bit
 96     * @return void
 97     */
 98    function setIgnoreCase($bit)
 99    {
100        $this->ignoreCase = (boolean) $bit;
101    }
102
103    /**
104     * Gets whether or not regex operation is case sensitive.
105     * @return boolean
106     */
107    function getIgnoreCase()
108    {
109        return $this->ignoreCase;
110    }
111
112    /**
113     * Sets whether regexp should be applied in multiline mode.
114     * @param boolean $bit
115     */
116    function setMultiline($bit)
117    {
118        $this->multiline = $bit;
119    }
120
121    /**
122     * Gets whether regexp is to be applied in multiline mode.
123     * @return boolean
124     */
125    function getMultiline()
126    {
127        return $this->multiline;
128    }
129
130    /**
131     * The pattern needs to be converted into PREG style -- which includes adding expression delims & any flags, etc.
132     * @param string $pattern
133     * @return string prepared pattern.
134     */
135    private function preparePattern($pattern)
136    {
137        // Use backquotes since hardly ever found in a regexp pattern, avoids using preg_quote
138        return '`' . $pattern . '`' . $this->getModifiers();
139    }
140
141    /**
142     * Matches pattern against source string and sets the matches array.
143     * @param string $pattern The regex pattern to match.
144     * @param string $source The source string.
145     * @param array $matches The array in which to store matches.
146     * @return boolean Success of matching operation.
147     */
148    function match($pattern, $source, &$matches)
149    {
150        return preg_match($this->preparePattern($pattern), $source, $matches);
151    }
152
153    /**
154     * Matches all patterns in source string and sets the matches array.
155     * @param string $pattern The regex pattern to match.
156     * @param string $source The source string.
157     * @param array $matches The array in which to store matches.
158     * @return boolean Success of matching operation.
159     */
160    function matchAll($pattern, $source, &$matches)
161    {
162        return preg_match_all($this->preparePattern($pattern), $source, $matches);
163    }
164
165    /**
166     * Replaces $pattern with $replace in $source string.
167     * References to \1 group matches will be replaced with more preg-friendly
168     * $1.
169     * @param string $pattern The regex pattern to match.
170     * @param string $replace The string with which to replace matches.
171     * @param string $source The source string.
172     * @return string The replaced source string.
173     */
174    function replace($pattern, $replace, $source)
175    {
176        // convert \1 -> $1, because we want to use the more generic \1 in the XML
177        // but PREG prefers $1 syntax.
178        $replace = preg_replace('/\\\(\d+)/', '\$$1', $replace);
179        return preg_replace($this->preparePattern($pattern), $replace, $source);
180    }
181
182}
183