~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Zend/Filter/Inflector.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Filter
 
17
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
19
 * @version    $Id: Inflector.php 8226 2008-02-20 22:07:05Z ralph $
 
20
 */
 
21
 
 
22
/**
 
23
 * @see Zend_Filter
 
24
 * @see Zend_Filter_Interface
 
25
 */
 
26
require_once 'Zend/Filter.php';
 
27
 
 
28
/**
 
29
 * @see Zend_Loader_PluginLoader
 
30
 */
 
31
require_once 'Zend/Loader/PluginLoader.php';
 
32
 
 
33
/**
 
34
 * Filter chain for string inflection
 
35
 *
 
36
 * @category   Zend
 
37
 * @package    Zend_Filter
 
38
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
39
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
40
 */
 
41
class Zend_Filter_Inflector implements Zend_Filter_Interface 
 
42
{
 
43
    /**
 
44
     * @var Zend_Loader_PluginLoader_Interface
 
45
     */
 
46
    protected $_pluginLoader = null;
 
47
    
 
48
    /**
 
49
     * @var string
 
50
     */
 
51
    protected $_target = null;
 
52
        
 
53
    /**
 
54
     * @var bool
 
55
     */
 
56
    protected $_throwTargetExceptionsOn = true;
 
57
    
 
58
    /**
 
59
     * @var string
 
60
     */
 
61
    protected $_targetReplacementIdentifier = ':';
 
62
 
 
63
    /**
 
64
     * @var array
 
65
     */
 
66
    protected $_rules = array();
 
67
 
 
68
    /**
 
69
     * Constructor
 
70
     *
 
71
     * @param string $target
 
72
     * @param array $rules
 
73
     */
 
74
    public function __construct($target = null, Array $rules = array(), $throwTargetExceptionsOn = null, $targetReplacementIdentifer = null)
 
75
    {
 
76
        if ($target instanceof Zend_Config) {
 
77
            $this->setConfig($target);
 
78
        } else {
 
79
            if ((null !== $target) && is_string($target)) {
 
80
                $this->setTarget($target);
 
81
            }
 
82
 
 
83
            if (null !== $rules) {
 
84
                $this->addRules($rules);
 
85
            }
 
86
            
 
87
            if ($throwTargetExceptionsOn !== null) {
 
88
                $this->setThrowTargetExceptionsOn($throwTargetExceptionsOn);
 
89
            }
 
90
            
 
91
            if ($targetReplacementIdentifer != '') {
 
92
                $this->setTargetReplacementIdentifier($targetReplacementIdentifer);
 
93
            }
 
94
        }
 
95
    }
 
96
    
 
97
    /**
 
98
     * Retreive PluginLoader
 
99
     *
 
100
     * @return Zend_Loader_PluginLoader_Interface
 
101
     */
 
102
    public function getPluginLoader()
 
103
    {
 
104
        if (!$this->_pluginLoader instanceof Zend_Loader_PluginLoader_Interface) {
 
105
            $this->_pluginLoader = new Zend_Loader_PluginLoader(array('Zend_Filter_' => 'Zend/Filter/'), __CLASS__);
 
106
        }
 
107
        
 
108
        return $this->_pluginLoader;
 
109
    }
 
110
    
 
111
    /**
 
112
     * Set PluginLoader
 
113
     *
 
114
     * @param Zend_Loader_PluginLoader_Interface $pluginLoader
 
115
     * @return Zend_Filter_Inflector
 
116
     */
 
117
    public function setPluginLoader(Zend_Loader_PluginLoader_Interface $pluginLoader)
 
118
    {
 
119
        $this->_pluginLoader = $pluginLoader;
 
120
        return $this;
 
121
    }
 
122
 
 
123
    /**
 
124
     * Use Zend_Config object to set object state
 
125
     * 
 
126
     * @param  Zend_Config $config 
 
127
     * @return Zend_Filter_Inflector
 
128
     */
 
129
    public function setConfig(Zend_Config $config)
 
130
    {
 
131
        foreach ($config as $key => $value) {
 
132
            switch ($key) {
 
133
                case 'target':
 
134
                    $this->setTarget($value);
 
135
                    break;
 
136
                case 'filterPrefixPath':
 
137
                    if (is_scalar($value)) {
 
138
                        break;
 
139
                    }
 
140
                    $paths = $value->toArray();
 
141
                    foreach ($paths as $prefix => $path) {
 
142
                        $this->addFilterPrefixPath($prefix, $path);
 
143
                    }
 
144
                    break;
 
145
                case 'throwTargetExceptionsOn':
 
146
                    $this->setThrowTargetExceptionsOn($value);
 
147
                    break;
 
148
                case 'targetReplacementIdentifier':
 
149
                    $this->setTargetReplacementIdentifier($value);
 
150
                    break;
 
151
                case 'rules':
 
152
                    $this->addRules($value->toArray());
 
153
                    break;
 
154
                default:
 
155
                    break;
 
156
            }
 
157
        }
 
158
        return $this;
 
159
    }
 
160
    
 
161
    /**
 
162
     * Convienence method to add prefix and path to PluginLoader
 
163
     *
 
164
     * @param string $prefix
 
165
     * @param string $path
 
166
     * @return Zend_Filter_Inflector
 
167
     */
 
168
    public function addFilterPrefixPath($prefix, $path)
 
169
    {
 
170
        $this->getPluginLoader()->addPrefixPath($prefix, $path);
 
171
        return $this;
 
172
    }
 
173
 
 
174
    /**
 
175
     * Set Whether or not the inflector should throw an exception when a replacement
 
176
     * identifier is still found within an inflected target.
 
177
     *
 
178
     * @param bool $throwTargetExceptions
 
179
     * @return Zend_Filter_Inflector
 
180
     */
 
181
    public function setThrowTargetExceptionsOn($throwTargetExceptionsOn)
 
182
    {
 
183
        $this->_throwTargetExceptionsOn = ($throwTargetExceptionsOn == true) ? true : false;
 
184
        return $this;
 
185
    }
 
186
    
 
187
    /**
 
188
     * Will exceptions be thrown?
 
189
     *
 
190
     * @return bool
 
191
     */
 
192
    public function isThrowTargetExceptionsOn()
 
193
    {
 
194
        return $this->_throwTargetExceptionsOn;
 
195
    }
 
196
 
 
197
    /**
 
198
     * Set the Target Replacement Identifier, by default ':'
 
199
     *
 
200
     * @param string $targetReplacementIdentifier
 
201
     * @return Zend_Filter_Inflector
 
202
     */
 
203
    public function setTargetReplacementIdentifier($targetReplacementIdentifier)
 
204
    {
 
205
        $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier;
 
206
        return $this;
 
207
    }
 
208
    
 
209
    /**
 
210
     * Get Target Replacement Identifier
 
211
     *
 
212
     * @return string
 
213
     */
 
214
    public function getTargetReplacementIdentifier()
 
215
    {
 
216
        return $this->_targetReplacementIdentifier;
 
217
    }
 
218
    
 
219
    /**
 
220
     * Set a Target
 
221
     * ex: 'scripts/:controller/:action.:suffix'
 
222
     * 
 
223
     * @param string
 
224
     * @return Zend_Filter_Inflector
 
225
     */
 
226
    public function setTarget($target)
 
227
    {
 
228
        $this->_target = (string) $target;
 
229
        return $this;
 
230
    }
 
231
 
 
232
    /**
 
233
     * Retrieve target
 
234
     * 
 
235
     * @return string
 
236
     */
 
237
    public function getTarget()
 
238
    {
 
239
        return $this->_target;
 
240
    }
 
241
 
 
242
    /**
 
243
     * Set Target Reference
 
244
     *
 
245
     * @param reference $target
 
246
     * @return Zend_Filter_Inflector
 
247
     */
 
248
    public function setTargetReference(&$target)
 
249
    {
 
250
        $this->_target =& $target;
 
251
        return $this;
 
252
    }
 
253
 
 
254
    /**
 
255
     * SetRules() is the same as calling addRules() with the exception that it
 
256
     * clears the rules before adding them.
 
257
     *
 
258
     * @param array $rules
 
259
     * @return Zend_Filter_Inflector
 
260
     */
 
261
    public function setRules(Array $rules)
 
262
    {
 
263
        $this->clearRules();
 
264
        $this->addRules($rules);
 
265
        return $this;
 
266
    }
 
267
    
 
268
    /**
 
269
     * AddRules(): multi-call to setting filter rules.  
 
270
     *
 
271
     * If prefixed with a ":" (colon), a filter rule will be added.  If not 
 
272
     * prefixed, a static replacement will be added.
 
273
     * 
 
274
     * ex:
 
275
     * array(
 
276
     *     ':controller' => array('CamelCaseToUnderscore','StringToLower'),
 
277
     *     ':action'     => array('CamelCaseToUnderscore','StringToLower'),
 
278
     *     'suffix'      => 'phtml'
 
279
     *     );
 
280
     * 
 
281
     * @param array
 
282
     * @return Zend_Filter_Inflector
 
283
     */
 
284
    public function addRules(Array $rules)
 
285
    {
 
286
        $keys = array_keys($rules);
 
287
        foreach ($keys as $spec) {
 
288
            if ($spec[0] == ':') {
 
289
                $this->addFilterRule($spec, $rules[$spec]);
 
290
            } else {
 
291
                $this->setStaticRule($spec, $rules[$spec]);
 
292
            }
 
293
        }
 
294
        
 
295
        return $this;
 
296
    }
 
297
    
 
298
    /**
 
299
     * Get rules
 
300
     *
 
301
     * By default, returns all rules. If a $spec is provided, will return those 
 
302
     * rules if found, false otherwise.
 
303
     * 
 
304
     * @param  string $spec 
 
305
     * @return array|false
 
306
     */
 
307
    public function getRules($spec = null)
 
308
    {
 
309
        if (null !== $spec) {
 
310
            $spec = $this->_normalizeSpec($spec);
 
311
            if (isset($this->_rules[$spec])) {
 
312
                return $this->_rules[$spec];
 
313
            }
 
314
            return false;
 
315
        }
 
316
 
 
317
        return $this->_rules;
 
318
    }
 
319
    
 
320
    /**
 
321
     * getRule() returns a rule set by setFilterRule(), a numeric index must be provided
 
322
     *
 
323
     * @param string $spec
 
324
     * @param int $index
 
325
     * @return Zend_Filter_Interface|false
 
326
     */
 
327
    public function getRule($spec, $index)
 
328
    {
 
329
        $spec = $this->_normalizeSpec($spec);
 
330
        if (isset($this->_rules[$spec]) && is_array($this->_rules[$spec])) {
 
331
            if (isset($this->_rules[$spec][$index])) {
 
332
                return $this->_rules[$spec][$index];
 
333
            }
 
334
        }
 
335
        return false;
 
336
    }
 
337
    
 
338
    /**
 
339
     * ClearRules() clears the rules currently in the inflector
 
340
     *
 
341
     * @return Zend_Filter_Inflector
 
342
     */
 
343
    public function clearRules()
 
344
    {
 
345
        $this->_rules = array();
 
346
        return $this;
 
347
    }
 
348
    
 
349
    /**
 
350
     * Set a filtering rule for a spec.  $ruleSet can be a string, Filter object
 
351
     * or an array of strings or filter objects.
 
352
     *
 
353
     * @param string $spec
 
354
     * @param array|string|Zend_Filter_Interface $ruleSet
 
355
     * @return Zend_Filter_Inflector
 
356
     */
 
357
    public function setFilterRule($spec, $ruleSet)
 
358
    {
 
359
        $spec = $this->_normalizeSpec($spec);
 
360
        $this->_rules[$spec] = array();
 
361
        return $this->addFilterRule($spec, $ruleSet);
 
362
    }
 
363
 
 
364
    /**
 
365
     * Add a filter rule for a spec
 
366
     * 
 
367
     * @param mixed $spec 
 
368
     * @param mixed $ruleSet 
 
369
     * @return void
 
370
     */
 
371
    public function addFilterRule($spec, $ruleSet)
 
372
    {
 
373
        $spec = $this->_normalizeSpec($spec);
 
374
        if (!isset($this->_rules[$spec])) {
 
375
            $this->_rules[$spec] = array();
 
376
        }
 
377
        if (!is_array($ruleSet)) {
 
378
            $ruleSet = array($ruleSet);
 
379
        }
 
380
        foreach ($ruleSet as $rule) {
 
381
            $this->_rules[$spec][] = $this->_getRule($rule);
 
382
        }
 
383
        return $this;
 
384
    }
 
385
    
 
386
    /**
 
387
     * Set a static rule for a spec.  This is a single string value
 
388
     *
 
389
     * @param string $name
 
390
     * @param string $value
 
391
     * @return Zend_Filter_Inflector
 
392
     */
 
393
    public function setStaticRule($name, $value)
 
394
    {
 
395
        $name = $this->_normalizeSpec($name);
 
396
        $this->_rules[$name] = (string) $value;
 
397
        return $this;
 
398
    }
 
399
    
 
400
    /**
 
401
     * Set Static Rule Reference. 
 
402
     *
 
403
     * This allows a consuming class to pass a property or variable
 
404
     * in to be referenced when its time to build the output string from the 
 
405
     * target.
 
406
     *
 
407
     * @param string $name
 
408
     * @param mixed $reference
 
409
     * @return Zend_Filter_Inflector
 
410
     */
 
411
    public function setStaticRuleReference($name, &$reference)
 
412
    {
 
413
        $name = $this->_normalizeSpec($name);
 
414
        $this->_rules[$name] =& $reference;
 
415
        return $this;
 
416
    }
 
417
 
 
418
    /**
 
419
     * Inflect
 
420
     *
 
421
     * @param  string|array $source
 
422
     * @return string
 
423
     */
 
424
    public function filter($source)
 
425
    {
 
426
        // clean source
 
427
        foreach ( (array) $source as $sourceName => $sourceValue) {
 
428
            $source[ltrim($sourceName, ':')] = $sourceValue;
 
429
        }
 
430
 
 
431
        $pregQuotedTargetReplacementIdentifier = preg_quote($this->_targetReplacementIdentifier, '#');
 
432
        
 
433
        foreach ($this->_rules as $ruleName => $ruleValue) {
 
434
            if (isset($source[$ruleName])) {
 
435
                if (is_string($ruleValue)) {
 
436
                    // overriding the set rule
 
437
                    $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $source[$ruleName]);
 
438
                } elseif (is_array($ruleValue)) {
 
439
                    $processedPart = $source[$ruleName];
 
440
                    foreach ($ruleValue as $ruleFilter) {
 
441
                        $processedPart = $ruleFilter->filter($processedPart);
 
442
                    }
 
443
                    $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $processedPart);
 
444
                }
 
445
            } elseif (is_string($ruleValue)) {
 
446
                $processedParts['#'.$pregQuotedTargetReplacementIdentifier.$ruleName.'#'] = str_replace('\\', '\\\\', $ruleValue);
 
447
            }
 
448
        }
 
449
        
 
450
        // all of the values of processedParts would have been str_replace('\\', '\\\\', ..)'d to disable preg_replace backreferences 
 
451
        $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->_target);
 
452
 
 
453
        if ($this->_throwTargetExceptionsOn && (preg_match('#(?='.$pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true)) {
 
454
            require_once 'Zend/Filter/Exception.php';
 
455
            throw new Zend_Filter_Exception('A replacement identifier ' . $this->_targetReplacementIdentifier . ' was found inside the inflected target, perhaps a rule was not satisfied with a target source?  Unsatisfied inflected target: ' . $inflectedTarget);
 
456
        }
 
457
        
 
458
        return $inflectedTarget;
 
459
    }
 
460
    
 
461
    /**
 
462
     * Normalize spec string
 
463
     * 
 
464
     * @param  string $spec 
 
465
     * @return string
 
466
     */
 
467
    protected function _normalizeSpec($spec)
 
468
    {
 
469
        return ltrim((string) $spec, ':&');
 
470
    }
 
471
    
 
472
    /**
 
473
     * Resolve named filters and convert them to filter objects.
 
474
     *
 
475
     * @param  string $rule
 
476
     * @return Zend_Filter_Interface
 
477
     */
 
478
    protected function _getRule($rule)
 
479
    {
 
480
        if ($rule instanceof Zend_Filter_Interface) {
 
481
            return $rule;
 
482
        }
 
483
        
 
484
        $rule = (string) $rule;
 
485
        
 
486
        $className  = $this->getPluginLoader()->load($rule);
 
487
        $ruleObject = new $className();
 
488
        if (!$ruleObject instanceof Zend_Filter_Interface) {
 
489
            require_once 'Zend/Filter/Exception.php';
 
490
            throw new Zend_Filter_Exception('No class named ' . $rule . ' implementing Zend_Filter_Interface could be found');
 
491
        }
 
492
        
 
493
        return $ruleObject;
 
494
    }
 
495
 
 
496
}