ElementFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 36
ccs 13
cts 14
cp 0.9286
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 25 7
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 6
    public static function create($node_name, array $attributes = null, array $elements = null)
32
    {
33 6
        $element = ucfirst(strtolower($node_name));
34 6
        $className = "\Drone\Dom\Element\\" . $element;
35 6
        $instance = new $className;
36
37 6
        if (count($attributes)) {
38 6
            foreach ($attributes as $name => $value) {
39 6
                if (!is_string($name)) {
40
                    throw new \InvalidArgumentException("Attribute only accepts strings as names");
41
                }
42
43 6
                $instance->setAttribute(new Attribute($name, $value));
44
            }
45
        }
46
47 6
        if (count($elements)) {
48 6
            foreach ($elements as $_node_name => $element) {
49 6
                foreach ($element as $label => $_attributes) {
50 6
                    $instance->setChild($label, self::create($_node_name, $_attributes));
51
                }
52
            }
53
        }
54
55 6
        return $instance;
56
    }
57
}
58