浏览代码

[DomCrawler] removed redundant methods

Fabien Potencier 15 年之前
父节点
当前提交
2722da2146
共有 2 个文件被更改,包括 17 次插入45 次删除
  1. 12 39
      src/Symfony/Components/DomCrawler/Form.php
  2. 5 6
      tests/Symfony/Tests/Components/DomCrawler/FormTest.php

+ 12 - 39
src/Symfony/Components/DomCrawler/Form.php

@@ -69,41 +69,6 @@ class Form implements \ArrayAccess
         return $this->node;
     }
 
-    /**
-     * Gets the value of a field.
-     *
-     * @param string $name The field name
-     *
-     * @throws \InvalidArgumentException if the field does not exist
-     */
-    public function getValue($name)
-    {
-        if (!$this->hasField($name)) {
-            throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
-        }
-
-        return $this->fields[$name]->getValue();
-    }
-
-    /**
-     * Sets the value of a field.
-     *
-     * @param string       $name  The field name
-     * @param string|array $value The value of the field
-     *
-     * @throws \InvalidArgumentException if the field does not exist
-     */
-    public function setValue($name, $value)
-    {
-        if (!$this->hasField($name)) {
-            throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
-        }
-
-        $this->fields[$name]->setValue($value);
-
-        return $this;
-    }
-
     /**
      * Sets the value of the fields.
      *
@@ -112,7 +77,7 @@ class Form implements \ArrayAccess
     public function setValues(array $values)
     {
         foreach ($values as $name => $value) {
-            $this->setValue($name, $value);
+            $this[$name] = $value;
         }
 
         return $this;
@@ -337,7 +302,7 @@ class Form implements \ArrayAccess
      */
     public function offsetExists($name)
     {
-        return $this->hasValue($name);
+        return $this->hasField($name);
     }
 
     /**
@@ -349,7 +314,11 @@ class Form implements \ArrayAccess
      */
     public function offsetGet($name)
     {
-        return $this->getValue($name);
+        if (!$this->hasField($name)) {
+            throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
+        }
+
+        return $this->fields[$name]->getValue();
     }
 
     /**
@@ -362,7 +331,11 @@ class Form implements \ArrayAccess
      */
     public function offsetSet($name, $value)
     {
-        $this->setValue($name, $value);
+        if (!$this->hasField($name)) {
+            throw new \InvalidArgumentException(sprintf('The form field "%s" does not exist', $name));
+        }
+
+        $this->fields[$name]->setValue($value);
     }
 
     /**

+ 5 - 6
tests/Symfony/Tests/Components/DomCrawler/FormTest.php

@@ -155,22 +155,21 @@ class FormTest extends \PHPUnit_Framework_TestCase
     {
         $form = $this->createForm('<form><input type="text" name="foo" value="foo" /><input type="submit" /></form>');
 
-        $this->assertEquals('foo', $form->getValue('foo'), '->getValue() returns the value of a form field');
+        $this->assertEquals('foo', $form['foo'], '->getValue() returns the value of a form field');
 
-        $ret = $form->setValue('foo', 'bar');
+        $form['foo'] = 'bar';
 
-        $this->assertEquals($form, $ret, '->setValue() implements a fluent interface');
-        $this->assertEquals('bar', $form->getValue('foo'), '->setValue() changes the value of a form field');
+        $this->assertEquals('bar', $form['foo'], '->setValue() changes the value of a form field');
 
         try {
-            $form->setValue('foobar', 'bar');
+            $form['foobar'] = 'bar';
             $this->pass('->setValue() throws an \InvalidArgumentException exception if the field does not exist');
         } catch (\InvalidArgumentException $e) {
             $this->assertTrue(true, '->setValue() throws an \InvalidArgumentException exception if the field does not exist');
         }
 
         try {
-            $form->getValue('foobar');
+            $form['foobar'];
             $this->pass('->getValue() throws an \InvalidArgumentException exception if the field does not exist');
         } catch (\InvalidArgumentException $e) {
             $this->assertTrue(true, '->getValue() throws an \InvalidArgumentException exception if the field does not exist');