/
Filter.php
executable file
·257 lines (257 loc) · 7.85 KB
/
Filter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Definition of class PHP_Beautifier_Filter
*
* PHP version 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @category PHP
* @package PHP_Beautifier
* @subpackage Filter
* @author Claudio Bustos <cdx@users.sourceforge.com>
* @copyright 2004-2006 Claudio Bustos
* @link http://pear.php.net/package/PHP_Beautifier
* @link http://beautifyphp.sourceforge.net
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
*/
/**
* PHP_Beautifier_Filter
*
* Definition for creation of Filters
* For concrete details, please see {@link PHP_Beautifier_Filter_Default}
* @category PHP
* @package PHP_Beautifier
* @subpackage Filter
* @tutorial PHP_Beautifier/Filter/Filter.create.pkg
* @author Claudio Bustos <cdx@users.sourceforge.com>
* @copyright 2004-2006 Claudio Bustos
* @link http://pear.php.net/package/PHP_Beautifier
* @link http://beautifyphp.sourceforge.net
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: @package_version@
*/
abstract class PHP_Beautifier_Filter
{
/**
* Stores a reference to main PHP_Beautifier
* @var PHP_Beautifier
*/
protected $oBeaut;
/**
* Associative array of functions to use when some token are found
* @var array
*/
protected $aFilterTokenFunctions = array();
/**
* Settings for the Filter
* @var array
*/
protected $aSettings = array();
/**
* Definition of the settings
* Should be an associative array. The keys are the names of settings
* and the values are an array with the keys 'type' and '
* @var array
*/
protected $aSettingsDefinition = array();
/**
* Description of the Filter
* @var string
*/
protected $sDescription = 'Filter for PHP_Beautifier';
/**
* If a method for parse Tokens of a Filter returns this, the control of the process
* is handle by the next Filter
*/
const BYPASS = 'BYPASS';
/**
* Switch to 'turn' on and off the filter
* @var bool
*/
protected $bOn = true;
/**
* Current token
*/
protected $aToken = false;
/**
* Constructor
* If you need to overload this (for example, to create a
* definition for setting with {@link addSettingDefinition()}
* remember call the parent constructor.
* <code>
* parent::__construct($oBeaut, $aSettings)
* </code>
* @param PHP_Beautifier
* @param array settings for the Filter
*/
public function __construct(PHP_Beautifier $oBeaut, $aSettings = array())
{
$this->oBeaut = $oBeaut;
if ($aSettings) {
$this->aSettings = $aSettings;
}
}
/**
* Add a setting definition
* @param string
*/
protected function addSettingDefinition($sSetting, $sType, $sDescription)
{
$this->aSettingsDefinition[$sSetting] = array(
'type' => $sType,
'description' => $sDescription
);
}
/**
* return @string
*/
public function getName()
{
return str_ireplace('PHP_Beautifier_Filter_', '', get_class($this));
}
/**
* Turn on the Filter
* Use inside the code to beautify
* Ex.
* <code>
* ...some code...
* // ArrayNested->on()
* ...other code ...
* </code>
*/
final public function on()
{
$this->bOn = true;
}
/**
* Turn off the Filter
* Use inside the code to beautify
* Ex.
* <code>
* ...some code...
* // ArrayNested->off()
* ...other code ...
* </code>
*/
public function off()
{
$this->bOn = false;
}
/**
* Get a setting of the Filter
* @param string name of setting
* @return mixed value of setting or false
*/
final public function getSetting($sSetting)
{
return (array_key_exists($sSetting, $this->aSettings)) ? $this->aSettings[$sSetting] : false;
}
/**
* Set a value of a Setting
* @param string name of setting
* @param mixed value of setting
*/
final public function setSetting($sSetting, $sValue)
{
if (array_key_exists($sSetting, $this->aSettings)) {
$this->aSettings[$sSetting] = $sValue;
}
}
/**
* Function called from {@link PHP_Beautifier::process()} to process the tokens.
*
* If the received token is one of the keys of {@link $aFilterTokenFunctions}
* a function with the same name of the value of that key is called.
* If the method doesn't exists, {@link __call()} is called, and return
* {@link PHP_Beautifier_Filter::BYPASS}. PHP_Beautifier, now, call the next Filter is its list.
* If the method exists, it can return true or {@link PHP_Beautifier_Filter::BYPASS}.
* @param array token
* @return bool true if the token is processed, false bypass to the next Filter
* @see PHP_Beautifier::process()
*/
public function handleToken($token)
{
$this->aToken = $token;
if (!$this->bOn) {
return false;
}
$sMethod = $sValue = false;
if (array_key_exists($token[0], $this->aFilterTokenFunctions)) {
$sMethod = $this->aFilterTokenFunctions[$token[0]];
$sValue = $token[1];
} elseif ($this->oBeaut->getTokenFunction($token[0])) {
$sMethod = $this->oBeaut->getTokenFunction($token[0]);
}
$sValue = $token[1];
if ($sMethod) {
if ($this->oBeaut->iVerbose > 5) {
echo $sMethod . ":" . trim($sValue) . "\n";
}
// return false if PHP_Beautifier_Filter::BYPASS
return ($this->$sMethod($sValue) !== PHP_Beautifier_Filter::BYPASS);
} else { // WEIRD!!! -> Add the same received
$this->oBeaut->add($token[1]);
PHP_Beautifier_Common::getLog()->log("Add same received:" . trim($token[1]) , PEAR_LOG_DEBUG);
return true;
}
// never go here
return false;
}
/**
* @param string metodo
* @param array arguments
* @return mixed null or {@link PHP_Beautifier_Filter::BYPASS}
*/
public function __call($sMethod, $aArgs)
{
return PHP_Beautifier_Filter::BYPASS;
}
/**
* Called from {@link PHP_Beautifier::process()} at the beginning
* of the processing
* @return void
*/
public function preProcess()
{
}
/**
* Called from {@link PHP_Beautifier::process()} at the end of processing
* The post-process must be made in {@link PHP_Beautifier::$aOut}
* @return void
*/
public function postProcess()
{
}
public function __sleep()
{
return array(
'aSettings'
);
}
public function getDescription()
{
return $this->sDescription;
}
public function __toString()
{
// php_beautifier->setBeautify(false);
$sOut='Filter: '.$this->getName()."\n".
"Description: ".$this->getDescription()."\n";
if (!$this->aSettingsDefinition) {
$sOut.= "Settings: No declared settings";
} else {
$sOut.="Settings:\n";
foreach($this->aSettingsDefinition as $sSetting=>$aSettings) {
$sOut.=sprintf("- %s : %s (type %s)\n",$sSetting, $aSettings['description'], $aSettings['type']);
}
}
// php_beautifier->setBeautify(true);
return $sOut;
}
}
?>