Source for file Preg.class.php

Documentation is available at Preg.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Filter
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * PREG Filter Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Filter
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc PREG Filter Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Filter");
  34.  
  35. class Gumbo_Filter_Preg implements Gumbo_Interface_Filter {
  36.     
  37.     /** @var string $_pattern regular expression pattern */
  38.     private $_pattern;
  39.     /** @var string $_char replacement character */
  40.     private $_char;
  41.     /** @var int $_limit */
  42.     private $_limit = -1;
  43.     /** @var int $_count occurences of pattern */
  44.     private $_count = 0;
  45.     
  46.     
  47.     
  48.     /**
  49.      * Constructor
  50.      * @param string $pattern regular expression pattern
  51.      * @param string $char replacement character
  52.      * @param int $limit 
  53.      */
  54.     public function __construct ($pattern=null$char=null$limit=null{
  55.         if (!is_null ($pattern)) $this->setPattern ($pattern)}
  56.         if (!is_null ($limit)) $this->setLimit ($limit)}
  57.         $this->setChar ($char);
  58.     }
  59.     
  60.     
  61.     
  62.     /** ACTION METHODS **/
  63.     /**
  64.      * Runs the Filter process, returning the results
  65.      * @param string|array$data data to filter
  66.      * @return string|array
  67.      */
  68.     public function run ($data{
  69.         $res $data;
  70.         try {
  71.             // verify precondition
  72.             if (!$this->getPattern ()) {
  73.                 throw new Gumbo_Exception ("Filter Missing Required Pattern");
  74.             }
  75.             if (!is_string ($data&& !is_array ($data)) {
  76.                 throw new Gumbo_Exception ("Invalid Argument 'data:str|arr' => {$data}:gettype ($data));
  77.             }
  78.             
  79.             $count 0;
  80.             $res preg_replace ($this->getPattern ()$this->getChar ()$data$this->getLimit ()$count);
  81.             $this->setCount ($count);
  82.         catch (Gumbo_Exception $e{
  83.             $e->setFunction (__METHOD__);
  84.             gumbo_trigger ($e);
  85.         }
  86.         return $res;
  87.     }
  88.     
  89.     
  90.     
  91.     /** MUTATOR METHODS **/
  92.     /**
  93.      * Sets the Regular Expression Pattern
  94.      * @param string $pattern 
  95.      * @throws Gumbo_Exception
  96.      */
  97.     public function setPattern ($pattern{
  98.         try {
  99.             // verify precondition
  100.             if (!is_string ($pattern)) {
  101.                 throw new Gumbo_Exception ("Invalid Argument 'pattern:str' => {$pattern}:gettype ($pattern));
  102.             }
  103.             
  104.             $this->_pattern = $pattern;
  105.         catch (Gumbo_Exception $e{
  106.             $e->setFunction (__METHOD__);
  107.             gumbo_trigger ($e);
  108.         }
  109.     }
  110.     
  111.     /**
  112.      * Sets the replacement character
  113.      * @param string $char 
  114.      * @throws Gumbo_Exception
  115.      */
  116.     public function setChar ($char=null{
  117.         try {
  118.             // verify precondition
  119.             if (!is_null ($char&& !is_string ($char)) {
  120.                 throw new Gumbo_Exception ("Invalid Argument 'char:str|null' => {$char}:gettype ($char));
  121.             }
  122.             
  123.             $this->_char = $char;
  124.         catch (Gumbo_Exception $e{
  125.             $e->setFunction (__METHOD__);
  126.             gumbo_trigger ($e);
  127.         }
  128.     }
  129.     
  130.     /**
  131.      * Sets the Limit
  132.      * @precondition $limit >= -1 (no limit)
  133.      * @param int $limit 
  134.      * @throws Gumbo_Exception
  135.      */
  136.     public function setLimit ($limit{
  137.         try {
  138.             // verify precondition
  139.             if (!is_int ($limit)) {
  140.                 throw new Gumbo_Exception ("Invalid Argument 'limit:int' => {$limit}:gettype ($limit));
  141.             }
  142.             if ($limit < -1{
  143.                 throw new Gumbo_Exception ("Out Of Range 'limit >= -1' => {$limit}");
  144.             }
  145.             
  146.             $this->_limit = $limit;
  147.         catch (Gumbo_Exception $e{
  148.             $e->setFunction (__METHOD__);
  149.             gumbo_trigger ($e);
  150.         }
  151.     }
  152.     
  153.     /**
  154.      * Sets the count
  155.      * @precondition $count >= 0
  156.      * @param int $count 
  157.      * @throws Gumbo_Exception
  158.      */
  159.     protected function setCount ($count{
  160.         try {
  161.             // verify precondition
  162.             if (!is_int ($count)) {
  163.                 throw new Gumbo_Exception ("Invalid Argument 'count:int' => {$count}:gettype ($count));
  164.             }
  165.             if ($count 0{
  166.                 throw new Gumbo_Exception ("Out Of Range 'count >= 0' => {$count}");
  167.             }
  168.             
  169.             $this->_count = $count;
  170.         catch (Gumbo_Exception $e{
  171.             $e->setFunction (__METHOD__);
  172.             gumbo_trigger ($e);
  173.         }
  174.     }
  175.     
  176.     
  177.     
  178.     /** ACCESSOR METHODS **/
  179.     /**
  180.      * Returns the Regular Expression Pattern
  181.      * @return string 
  182.      */
  183.     public function getPattern ({
  184.         return $this->_pattern;
  185.     }
  186.     
  187.     /**
  188.      * Returns the replacement character
  189.      * @return string 
  190.      */
  191.     public function getChar ({
  192.         return $this->_char;
  193.     }
  194.     
  195.     /**
  196.      * Returns the limit
  197.      * @return int 
  198.      */
  199.     public function getLimit ({
  200.         return $this->_limit;
  201.     }
  202.     
  203.     /**
  204.      * Returns the count of occurences of the pattern
  205.      * @return int 
  206.      */
  207.     public function getCount ({
  208.         return $this->_count;
  209.     }
  210.     
  211. }
  212.  
  213. ?>