Source for file Language.class.php

Documentation is available at Language.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.  * Language 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 Language Filter Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Filter_Preg");
  34.  
  35.     
  36.     /** @var array $_words words (or word patterns) to filter */
  37.     private $_words = array ();
  38.     
  39.     
  40.     
  41.     /**
  42.      * Constructor
  43.      * @param string $char replacement character
  44.      */
  45.     public function __construct ($char=null{
  46.         parent::__construct (null$charnull);
  47.         $this->resetWords ();
  48.     }
  49.     
  50.     
  51.     
  52.     /** ACTION METHODS **/
  53.     /**
  54.      * Runs the Filter process, returning the results
  55.      * @param string $data data to filter
  56.      * @return string 
  57.      */
  58.     public function run ($data{
  59.         $this->setPattern ($this->getWords (true));
  60.         return parent::run ($data);
  61.     }
  62.     
  63.     /**
  64.      * Adds a Word (or Word Pattern) to the list
  65.      * @param string $word 
  66.      * @throws Gumbo_Exception
  67.      */
  68.     public function addWord ($word{
  69.         try {
  70.             // verify precondition
  71.             if (!is_string ($word)) {
  72.                 throw new Gumbo_Exception ("Invalid Argument 'word:str' => {$word}:gettype ($word));
  73.             }
  74.             if ($this->isWord ($word)) {
  75.                 throw new Gumbo_Exception ("Word (PatternExists: {$word}");
  76.             }
  77.             
  78.             $this->_words [$word;
  79.         catch (Gumbo_Exception $e{
  80.             $e->setFunction (__METHOD__);
  81.             gumbo_trigger ($e);
  82.         }
  83.     }
  84.     
  85.     /**
  86.      * Removes a Word (or Word Pattern) from the list
  87.      * @param string $word 
  88.      * @throws Gumbo_Exception
  89.      */
  90.     public function removeWord ($word{
  91.         try {
  92.             // verify precondition
  93.             if (!is_string ($word)) {
  94.                 throw new Gumbo_Exception ("Invalid Argument 'word:str' => {$word}:gettype ($word));
  95.             }
  96.             
  97.             if (!$this->isWord ($word)) {
  98.                 throw new Gumbo_Exception ("Word (PatternUndefined: {$word}");
  99.             }
  100.             
  101.             foreach ($this->getWords (as $key=>$val{
  102.                 if ($tag === $val{
  103.                     unset ($this->_words [$key]);
  104.                     break;
  105.                 }
  106.             }
  107.         catch (Gumbo_Exception $e{
  108.             $e->setFunction (__METHOD__);
  109.             gumbo_trigger ($e);
  110.         }
  111.     }
  112.     
  113.     /**
  114.      * Resets ALL the Words
  115.      * @postcondition words to default
  116.      */
  117.     public function resetWords ({
  118.         $this->_words = array ();
  119.         $this->_words ["\b([\w\d]*)?[fF][uU\W][cC\W][kK\W]([\w\d]*)?\b";
  120.         $this->_words ["\b([\w\d]*)?[sS$][hH\W][iI\W][tT\W]([\w\d]*)?\b";
  121.         $this->_words ["\b[aA@][sS\$]{2}([\w\d]*)?\b";
  122.         $this->_words ["\b[cC][oO0\W][cC\W][kK]([sS][uU][cC][kK]([\w\d]*)?)?\b";
  123.         $this->_words ["\b([gG][oO][dD])?[dD][aA@\W][mM][nN]([\w\d]*)?\b";
  124.         $this->_words ["\b([\w\d]*)?[bB][iI][tT][cC][hH]([\w\d]*)?\b";
  125.         $this->_words ["\b[cC][uU][nN][tT]([\w\d]*)\b";
  126.     }
  127.     
  128.     
  129.     
  130.     /** MUTATOR METHODS **/
  131.     /**
  132.      * Sets the Words (or Word Patterns) to Filter
  133.      * @param string $words 
  134.      * @throws Gumbo_Exception
  135.      */
  136.     public function setWords ($words=null{
  137.         if (is_null ($words)) return}
  138.         try {
  139.             // verify precondition
  140.             if (!is_string ($words&& !is_array ($words)) {
  141.                 throw new Gumbo_Exception ("Invalid Argument 'tag:str|arr' => {$words}:gettype ($words));
  142.             }
  143.             
  144.             // check for multiple arguments
  145.             if (func_num_args (1{
  146.                 $words func_get_args ();
  147.             }
  148.             
  149.             // check if an array
  150.             if (is_array ($words)) {
  151.                 foreach ($words as $val{
  152.                     $this->setTags ($val);
  153.                 }
  154.                 return;
  155.             }
  156.             
  157.             $this->addWord($words);
  158.         catch (Gumbo_Exception $e{
  159.             $e->setFunction (__METHOD__);
  160.             gumbo_trigger ($e);
  161.         }
  162.     }
  163.     
  164.     
  165.     
  166.     /** ACCESSOR METHODS **/
  167.     /**
  168.      * Returns ALL the Filter Words
  169.      * @param bool $as_string formats as a string pattern
  170.      * @return array|string
  171.      */
  172.     public function getWords ($as_string=false{
  173.         try {
  174.             // verify precondition
  175.             if (!is_bool ($as_string)) {
  176.                 throw new Gumbo_Exception ("Invalid Argument 'as_string:bool' => {$as_string}:gettype ($as_string));
  177.             }
  178.         catch (Gumbo_Exception $e{
  179.             $e->setFunction (__METHOD__);
  180.             gumbo_trigger ($e);
  181.             $as_string false;
  182.         }
  183.         
  184.         if ($as_string{
  185.             $pattern "(";
  186.             foreach ($this->getWords (as $val{
  187.                 $pattern .= $val "|";
  188.             }
  189.             return substr ($pattern0strlen ($pattern1")";
  190.         }
  191.         
  192.         return $this->_words;
  193.     }
  194.     
  195.     /**
  196.      * Returns if the Word (Pattern) Exists
  197.      * @param string $word 
  198.      * @return bool 
  199.      */
  200.     public function isWord ($word{
  201.         $found false;
  202.         try {
  203.             // verify precondition
  204.             if (!is_string ($word)) {
  205.                 throw new Gumbo_Exception ("Invalid Argument 'word:str' => {$word}:gettype ($word));
  206.             }
  207.             
  208.             foreach ($this->getWords (as $val{
  209.                 if ($word === $val{
  210.                     $found true;
  211.                     break;
  212.                 }
  213.             }
  214.         catch (Gumbo_Exception $e{
  215.             $e->setFunction (__METHOD__);
  216.             gumbo_trigger ($e);
  217.         }
  218.         return $found;
  219.     }
  220.     
  221. }
  222.  
  223. ?>