diff --git a/src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php b/src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php index 5333b3059d2a..dde8b405d96b 100644 --- a/src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php +++ b/src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php @@ -41,6 +41,46 @@ public function hasValue() 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. * diff --git a/src/Symfony/Components/DomCrawler/Field/FileFormField.php b/src/Symfony/Components/DomCrawler/Field/FileFormField.php index 64a06290e4d2..63c8dfd24e05 100644 --- a/src/Symfony/Components/DomCrawler/Field/FileFormField.php +++ b/src/Symfony/Components/DomCrawler/Field/FileFormField.php @@ -37,6 +37,16 @@ public function setErrorCode($error) $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. * diff --git a/src/Symfony/Components/DomCrawler/Form.php b/src/Symfony/Components/DomCrawler/Form.php index f665dd888785..a7f6e8705c6e 100644 --- a/src/Symfony/Components/DomCrawler/Form.php +++ b/src/Symfony/Components/DomCrawler/Form.php @@ -18,7 +18,7 @@ * @subpackage Components_DomCrawler * @author Fabien Potencier */ -class Form +class Form implements \ArrayAccess { protected $document; protected $button; @@ -327,4 +327,51 @@ protected function initialize() } } } + + /** + * 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.'); + } }