Sfoglia il codice sorgente

[DomCrawler] added some shortcut methods to the Form classes to make the API more friendly

Fabien Potencier 15 anni fa
parent
commit
b17400454b

+ 40 - 0
src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php

@@ -41,6 +41,46 @@ class ChoiceFormField extends FormField
         return true;
     }
 
+    /**
+     * Sets the value of the field.
+     *
+     * @param string $value The value of the field
+     *
+     * @throws \InvalidArgumentException When value type provided is not correct
+     */
+    public function select($value)
+    {
+        $this->setValue($value);
+    }
+
+    /**
+     * Ticks a checkbox.
+     *
+     * @throws \InvalidArgumentException When value type provided is not correct
+     */
+    public function tick()
+    {
+        if ('checkbox' !== $this->type) {
+            throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
+        }
+
+        $this->setValue(true);
+    }
+
+    /**
+     * Ticks a checkbox.
+     *
+     * @throws \InvalidArgumentException When value type provided is not correct
+     */
+    public function untick()
+    {
+        if ('checkbox' !== $this->type) {
+            throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
+        }
+
+        $this->setValue(false);
+    }
+
     /**
      * Sets the value of the field.
      *

+ 10 - 0
src/Symfony/Components/DomCrawler/Field/FileFormField.php

@@ -37,6 +37,16 @@ class FileFormField extends FormField
         $this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
     }
 
+    /**
+     * Sets the value of the field.
+     *
+     * @param string $value The value of the field
+     */
+    public function upload($value)
+    {
+        $this->setValue($value);
+    }
+
     /**
      * Sets the value of the field.
      *

+ 48 - 1
src/Symfony/Components/DomCrawler/Form.php

@@ -18,7 +18,7 @@ namespace Symfony\Components\DomCrawler;
  * @subpackage Components_DomCrawler
  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  */
-class Form
+class Form implements \ArrayAccess
 {
     protected $document;
     protected $button;
@@ -327,4 +327,51 @@ class Form
             }
         }
     }
+
+    /**
+     * Returns true if the named field exists.
+     *
+     * @param string $name The field name
+     *
+     * @param Boolean true if the field exists, false otherwise
+     */
+    public function offsetExists($name)
+    {
+        return $this->hasValue($name);
+    }
+
+    /**
+     * Gets the value of a field.
+     *
+     * @param string $name The field name
+     *
+     * @throws \InvalidArgumentException if the field does not exist
+     */
+    public function offsetGet($name)
+    {
+        return $this->getValue($name);
+    }
+
+    /**
+     * 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 offsetSet($name, $value)
+    {
+        $this->setValue($name, $value);
+    }
+
+    /**
+     * Unimplemented.
+     *
+     * @param string $name The field name
+     */
+    public function offsetUnset($name)
+    {
+        throw new \LogicException('The Form fields cannot be removed.');
+    }
 }