Passed
Push — master ( 00604e...06d537 )
by Darío
02:22
created

ElementFactory::create()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 5
nop 3
dl 0
loc 29
rs 8.8333
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
 * ElementFactory class
17
 *
18
 * Create an instance of the specified element
19
 */
20
class ElementFactory extends AbstractElement
21
{
22
    /**
23
     * Creates an instance of an element
24
     *
25
     * @param string $node_name
26
     * @param array $attributes
27
     * @param array $elements
28
     *
29
     * @return AbstractElement
30
     */
31
    public static function create($node_name, Array $attributes = null, Array $elements = null)
32
    {
33
        $element = ucfirst(strtolower($node_name));
34
        $className = "\Drone\Dom\Element\\" . $element;
35
        $instance = new $className;
36
37
        if (count($attributes))
38
        {
39
            foreach ($attributes as $name => $value)
40
            {
41
                if (!is_string($name))
42
                    throw new \InvalidArgumentException("Attribute only accepts strings as names");
43
44
                $instance->setAttribute(new Attribute($name, $value));
45
            }
46
        }
47
48
        if (count($elements))
49
        {
50
            foreach ($elements as $_node_name => $element)
51
            {
52
                foreach ($element as $label => $_attributes)
53
                {
54
                    $instance->setChild($label, self::create($_node_name, $_attributes));
55
                }
56
            }
57
        }
58
59
        return $instance;
60
    }
61
}