Skip to content

Commit b174004

Browse files
committed
[DomCrawler] added some shortcut methods to the Form classes to make the API more friendly
1 parent 16f7d3a commit b174004

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

src/Symfony/Components/DomCrawler/Field/ChoiceFormField.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,46 @@ public function hasValue()
4141
return true;
4242
}
4343

44+
/**
45+
* Sets the value of the field.
46+
*
47+
* @param string $value The value of the field
48+
*
49+
* @throws \InvalidArgumentException When value type provided is not correct
50+
*/
51+
public function select($value)
52+
{
53+
$this->setValue($value);
54+
}
55+
56+
/**
57+
* Ticks a checkbox.
58+
*
59+
* @throws \InvalidArgumentException When value type provided is not correct
60+
*/
61+
public function tick()
62+
{
63+
if ('checkbox' !== $this->type) {
64+
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
65+
}
66+
67+
$this->setValue(true);
68+
}
69+
70+
/**
71+
* Ticks a checkbox.
72+
*
73+
* @throws \InvalidArgumentException When value type provided is not correct
74+
*/
75+
public function untick()
76+
{
77+
if ('checkbox' !== $this->type) {
78+
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
79+
}
80+
81+
$this->setValue(false);
82+
}
83+
4484
/**
4585
* Sets the value of the field.
4686
*

src/Symfony/Components/DomCrawler/Field/FileFormField.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public function setErrorCode($error)
3737
$this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
3838
}
3939

40+
/**
41+
* Sets the value of the field.
42+
*
43+
* @param string $value The value of the field
44+
*/
45+
public function upload($value)
46+
{
47+
$this->setValue($value);
48+
}
49+
4050
/**
4151
* Sets the value of the field.
4252
*

src/Symfony/Components/DomCrawler/Form.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @subpackage Components_DomCrawler
1919
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2020
*/
21-
class Form
21+
class Form implements \ArrayAccess
2222
{
2323
protected $document;
2424
protected $button;
@@ -327,4 +327,51 @@ protected function initialize()
327327
}
328328
}
329329
}
330+
331+
/**
332+
* Returns true if the named field exists.
333+
*
334+
* @param string $name The field name
335+
*
336+
* @param Boolean true if the field exists, false otherwise
337+
*/
338+
public function offsetExists($name)
339+
{
340+
return $this->hasValue($name);
341+
}
342+
343+
/**
344+
* Gets the value of a field.
345+
*
346+
* @param string $name The field name
347+
*
348+
* @throws \InvalidArgumentException if the field does not exist
349+
*/
350+
public function offsetGet($name)
351+
{
352+
return $this->getValue($name);
353+
}
354+
355+
/**
356+
* Sets the value of a field.
357+
*
358+
* @param string $name The field name
359+
* @param string|array $value The value of the field
360+
*
361+
* @throws \InvalidArgumentException if the field does not exist
362+
*/
363+
public function offsetSet($name, $value)
364+
{
365+
$this->setValue($name, $value);
366+
}
367+
368+
/**
369+
* Unimplemented.
370+
*
371+
* @param string $name The field name
372+
*/
373+
public function offsetUnset($name)
374+
{
375+
throw new \LogicException('The Form fields cannot be removed.');
376+
}
330377
}

0 commit comments

Comments
 (0)