Skip to content

Commit

Permalink
[DomCrawler] added some shortcut methods to the Form classes to make …
Browse files Browse the repository at this point in the history
…the API more friendly
  • Loading branch information
fabpot committed Jun 14, 2010
1 parent 16f7d3a commit b174004
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Components/DomCrawler/Field/FileFormField.php
Expand Up @@ -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.
*
Expand Down
49 changes: 48 additions & 1 deletion src/Symfony/Components/DomCrawler/Form.php
Expand Up @@ -18,7 +18,7 @@
* @subpackage Components_DomCrawler
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class Form
class Form implements \ArrayAccess
{
protected $document;
protected $button;
Expand Down Expand Up @@ -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.');
}
}

0 comments on commit b174004

Please sign in to comment.