Passed
Push — master ( c34495...8adf73 )
by Darío
03:13
created

Form::fill()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 18
ccs 6
cts 10
cp 0.6
crap 5.024
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Dom\Element;
12
13
use Drone\Dom\Attribute;
14
15
/**
16
 * Form class
17
 *
18
 * Represents a html Form element
19
 */
20
class Form extends AbstractElement
21
{
22
    /**
23
     * The node name of the element
24
     *
25
     * @var string
26
     */
27
    const NODE_NAME = 'FORM';
28
29
    /**
30
     * Tells if the element has a end tag
31
     *
32
     * @var boolean
33
     */
34
    const HAS_END_TAG = true;
35
36
    /**
37
     * Fills the form with all passed values
38
     *
39
     * @param array $values
40
     *
41
     * @throws Exception\ChildNotFoundException
42
     *
43
     * @return null
44
     */
45 5
    public function fill(Array $values)
46
    {
47 5
        foreach ($values as $label => $value) {
48 5
            $child = $this->getChild($label);
49
50 5
            if (is_null($child)) {
51
                throw new Exception\ChildNotFoundException(
52
                    "The child '$label' does not exists inside the form element"
53
                );
54
            }
55
56 5
            if (!$child->isFormControl()) {
57
                throw new Exception\NotFormControlException(
58
                    "The child '$label' is not a form control"
59
                );
60
            }
61
62 5
            $child->setAttribute(new Attribute("value", $value));
63
        }
64 5
    }
65
}
66