Browse Source

[ZF-8997] Zend_Filter:

- added Zend_Config and Array support
- added setOptions()

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21217 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 years ago
parent
commit
410365af37

+ 3 - 3
documentation/manual/en/module_specs/Zend_Filter-Inflector.xml

@@ -455,7 +455,7 @@ $inflector->addRules(array(
             You can use <classname>Zend_Config</classname> to set rules, filter prefix
             paths, and other object state in your inflectors, either by passing
             a <classname>Zend_Config</classname> object to the constructor or
-            <methodname>setConfig()</methodname>. The following settings may be specified:
+            <methodname>setOptions()</methodname>. The following settings may be specified:
         </para>
 
         <itemizedlist>
@@ -505,9 +505,9 @@ $inflector->addRules(array(
 $config    = new Zend_Config($options);
 $inflector = new Zend_Filter_Inflector($config);
 
-// Or with setConfig():
+// Or with setOptions():
 $inflector = new Zend_Filter_Inflector();
-$inflector->setConfig($config);
+$inflector->setOptions($config);
 ]]></programlisting>
         </example>
     </sect2>

+ 50 - 20
library/Zend/Filter/Inflector.php

@@ -68,30 +68,36 @@ class Zend_Filter_Inflector implements Zend_Filter_Interface
     /**
      * Constructor
      *
-     * @param string $target
-     * @param array $rules
+     * @param string|array $options Options to set
      */
-    public function __construct($target = null, Array $rules = array(), $throwTargetExceptionsOn = null, $targetReplacementIdentifer = null)
+    public function __construct($options = null)
     {
-        if ($target instanceof Zend_Config) {
-            $this->setConfig($target);
-        } else {
-            if ((null !== $target) && is_string($target)) {
-                $this->setTarget($target);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } else if (!is_array($options)) {
+            $options = func_get_args();
+            $temp    = array();
+
+            if (!empty($options)) {
+                $temp['target'] = array_shift($options);
             }
 
-            if (null !== $rules) {
-                $this->addRules($rules);
+            if (!empty($options)) {
+                $temp['rules'] = array_shift($options);
             }
 
-            if ($throwTargetExceptionsOn !== null) {
-                $this->setThrowTargetExceptionsOn($throwTargetExceptionsOn);
+            if (!empty($options)) {
+                $temp['throwTargetExceptionsOn'] = array_shift($options);
             }
 
-            if ($targetReplacementIdentifer != '') {
-                $this->setTargetReplacementIdentifier($targetReplacementIdentifer);
+            if (!empty($options)) {
+                $temp['targetReplacementIdentifier'] = array_shift($options);
             }
+
+            $options = $temp;
         }
+
+        $this->setOptions($options);
     }
 
     /**
@@ -123,38 +129,60 @@ class Zend_Filter_Inflector implements Zend_Filter_Interface
     /**
      * Use Zend_Config object to set object state
      *
+     * @deprecated Use setOptions() instead
      * @param  Zend_Config $config
      * @return Zend_Filter_Inflector
      */
     public function setConfig(Zend_Config $config)
     {
-        foreach ($config as $key => $value) {
+        return $this->setOptions($config);
+    }
+
+    /**
+     * Set options
+     *
+     * @param  array $options
+     * @return Zend_Filter_Inflector
+     */
+    public function setOptions($options) {
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        }
+
+        foreach ($options as $key => $value) {
             switch ($key) {
                 case 'target':
                     $this->setTarget($value);
                     break;
+
                 case 'filterPrefixPath':
                     if (is_scalar($value)) {
                         break;
                     }
-                    $paths = $value->toArray();
-                    foreach ($paths as $prefix => $path) {
+
+                    foreach ($value as $prefix => $path) {
                         $this->addFilterPrefixPath($prefix, $path);
                     }
+
                     break;
+
                 case 'throwTargetExceptionsOn':
                     $this->setThrowTargetExceptionsOn($value);
                     break;
+
                 case 'targetReplacementIdentifier':
                     $this->setTargetReplacementIdentifier($value);
                     break;
+
                 case 'rules':
-                    $this->addRules($value->toArray());
+                    $this->addRules($value);
                     break;
+
                 default:
                     break;
             }
         }
+
         return $this;
     }
 
@@ -202,7 +230,10 @@ class Zend_Filter_Inflector implements Zend_Filter_Interface
      */
     public function setTargetReplacementIdentifier($targetReplacementIdentifier)
     {
-        $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier;
+        if ($targetReplacementIdentifier) {
+            $this->_targetReplacementIdentifier = (string) $targetReplacementIdentifier;
+        }
+
         return $this;
     }
 
@@ -502,5 +533,4 @@ class Zend_Filter_Inflector implements Zend_Filter_Interface
 
         return $ruleObject;
     }
-
 }

+ 42 - 0
tests/Zend/Filter/InflectorTest.php

@@ -470,6 +470,7 @@ class Zend_Filter_InflectorTest extends PHPUnit_Framework_TestCase
         $inflector = new Zend_Filter_Inflector('something', array(), false, false);
         $this->assertFalse($inflector->isThrowTargetExceptionsOn());
         $this->assertEquals($inflector->getTargetReplacementIdentifier(), ':');
+
         $inflector = new Zend_Filter_Inflector('something', array(), false, '#');
         $this->assertEquals($inflector->getTargetReplacementIdentifier(), '#');
     }
@@ -504,6 +505,47 @@ class Zend_Filter_InflectorTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(5, count($rules));
     }
 
+    /**
+     * @group ZF-8997
+     */
+    public function testPassingArrayToConstructorSetsStateAndRules()
+    {
+        $options = $this->getOptions();
+        $inflector = new Zend_Filter_Inflector($options);
+        $this->_testOptions($inflector);
+    }
+
+    /**
+     * @group ZF-8997
+     */
+    public function testPassingArrayToSetConfigSetsStateAndRules()
+    {
+        $options = $this->getOptions();
+        $inflector = new Zend_Filter_Inflector();
+        $inflector->setOptions($options);
+        $this->_testOptions($inflector);
+    }
+
+    /**
+     * @group ZF-8997
+     */
+    public function testPassingZendConfigObjectToConstructorSetsStateAndRules()
+    {
+        $config = $this->getConfig();
+        $inflector = new Zend_Filter_Inflector($config);
+        $this->_testOptions($inflector);
+    }
+
+    /**
+     * @group ZF-8997
+     */
+    public function testPassingZendConfigObjectToSetConfigSetsStateAndRules()
+    {
+        $config = $this->getConfig();
+        $inflector = new Zend_Filter_Inflector();
+        $inflector->setOptions($config);
+        $this->_testOptions($inflector);
+    }
 }
 
 // Call Zend_Filter_InflectorTest::main() if this source file is executed directly.