3 * Definition of class PHP_Beautifier_Filter
7 * LICENSE: This source file is subject to version 3.0 of the PHP license
8 * that is available through the world-wide-web at the following URI:
9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
10 * the PHP License and are unable to obtain it through the web, please
11 * send a note to license@php.net so we can mail you a copy immediately.
13 * @package PHP_Beautifier
15 * @author Claudio Bustos <cdx@users.sourceforge.com>
16 * @copyright 2004-2006 Claudio Bustos
17 * @link http://pear.php.net/package/PHP_Beautifier
18 * @link http://beautifyphp.sourceforge.net
19 * @license http://www.php.net/license/3_0.txt PHP License 3.0
23 * PHP_Beautifier_Filter
25 * Definition for creation of Filters
26 * For concrete details, please see {@link PHP_Beautifier_Filter_Default}
28 * @package PHP_Beautifier
30 * @tutorial PHP_Beautifier/Filter/Filter.create.pkg
31 * @author Claudio Bustos <cdx@users.sourceforge.com>
32 * @copyright 2004-2006 Claudio Bustos
33 * @link http://pear.php.net/package/PHP_Beautifier
34 * @link http://beautifyphp.sourceforge.net
35 * @license http://www.php.net/license/3_0.txt PHP License 3.0
36 * @version Release: 0.1.14
38 abstract class PHP_Beautifier_Filter
41 * Stores a reference to main PHP_Beautifier
46 * Associative array of functions to use when some token are found
49 protected $aFilterTokenFunctions = array();
51 * Settings for the Filter
54 protected $aSettings = array();
56 * Definition of the settings
57 * Should be an associative array. The keys are the names of settings
58 * and the values are an array with the keys 'type' and '
61 protected $aSettingsDefinition = array();
63 * Description of the Filter
66 protected $sDescription = 'Filter for PHP_Beautifier';
68 * If a method for parse Tokens of a Filter returns this, the control of the process
69 * is handle by the next Filter
71 const BYPASS
= 'BYPASS';
73 * Switch to 'turn' on and off the filter
76 protected $bOn = true;
80 protected $aToken = false;
83 * If you need to overload this (for example, to create a
84 * definition for setting with {@link addSettingDefinition()}
85 * remember call the parent constructor.
87 * parent::__construct($oBeaut, $aSettings)
89 * @param PHP_Beautifier
90 * @param array settings for the Filter
92 public function __construct(PHP_Beautifier
$oBeaut, $aSettings = array())
94 $this->oBeaut
= $oBeaut;
96 $this->aSettings
= $aSettings;
100 * Add a setting definition
103 protected function addSettingDefinition($sSetting, $sType, $sDescription)
105 $this->aSettingsDefinition
[$sSetting] = array(
107 'description' => $sDescription
113 public function getName()
115 return str_ireplace('PHP_Beautifier_Filter_', '', get_class($this));
119 * Use inside the code to beautify
123 * // ArrayNested->on()
127 final public function on()
132 * Turn off the Filter
133 * Use inside the code to beautify
137 * // ArrayNested->off()
141 public function off()
146 * Get a setting of the Filter
147 * @param string name of setting
148 * @return mixed value of setting or false
150 final public function getSetting($sSetting)
152 return (array_key_exists($sSetting, $this->aSettings
)) ?
$this->aSettings
[$sSetting] : false;
155 * Set a value of a Setting
156 * @param string name of setting
157 * @param mixed value of setting
159 final public function setSetting($sSetting, $sValue)
161 if (array_key_exists($sSetting, $this->aSettings
)) {
162 $this->aSettings
[$sSetting] = $sValue;
166 * Function called from {@link PHP_Beautifier::process()} to process the tokens.
168 * If the received token is one of the keys of {@link $aFilterTokenFunctions}
169 * a function with the same name of the value of that key is called.
170 * If the method doesn't exists, {@link __call()} is called, and return
171 * {@link PHP_Beautifier_Filter::BYPASS}. PHP_Beautifier, now, call the next Filter is its list.
172 * If the method exists, it can return true or {@link PHP_Beautifier_Filter::BYPASS}.
174 * @return bool true if the token is processed, false bypass to the next Filter
175 * @see PHP_Beautifier::process()
177 public function handleToken($token)
179 $this->aToken
= $token;
183 $sMethod = $sValue = false;
184 if (array_key_exists($token[0], $this->aFilterTokenFunctions
)) {
185 $sMethod = $this->aFilterTokenFunctions
[$token[0]];
187 } elseif ($this->oBeaut
->getTokenFunction($token[0])) {
188 $sMethod = $this->oBeaut
->getTokenFunction($token[0]);
192 if ($this->oBeaut
->iVerbose
> 5) {
193 echo $sMethod . ":" . trim($sValue) . "\n";
195 // return false if PHP_Beautifier_Filter::BYPASS
196 return ($this->$sMethod($sValue) !== PHP_Beautifier_Filter
::BYPASS
);
197 } else { // WEIRD!!! -> Add the same received
198 $this->oBeaut
->add($token[1]);
199 PHP_Beautifier_Common
::getLog()->log("Add same received:" . trim($token[1]) , PEAR_LOG_DEBUG
);
206 * @param string metodo
207 * @param array arguments
208 * @return mixed null or {@link PHP_Beautifier_Filter::BYPASS}
210 public function __call($sMethod, $aArgs)
212 return PHP_Beautifier_Filter
::BYPASS
;
215 * Called from {@link PHP_Beautifier::process()} at the beginning
219 public function preProcess()
223 * Called from {@link PHP_Beautifier::process()} at the end of processing
224 * The post-process must be made in {@link PHP_Beautifier::$aOut}
227 public function postProcess()
230 public function __sleep()
236 public function getDescription()
238 return $this->sDescription
;
240 public function __toString()
242 // php_beautifier->setBeautify(false);
243 $sOut='Filter: '.$this->getName()."\n".
244 "Description: ".$this->getDescription()."\n";
245 if (!$this->aSettingsDefinition
) {
246 $sOut.= "Settings: No declared settings";
248 $sOut.="Settings:\n";
249 foreach($this->aSettingsDefinition
as $sSetting=>$aSettings) {
250 $sOut.=sprintf("- %s : %s (type %s)\n",$sSetting, $aSettings['description'], $aSettings['type']);
253 // php_beautifier->setBeautify(true);