SimpleXMLNodeTypedAttributeGetter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 1
cbo 2
dl 0
loc 81
ccs 20
cts 20
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getValue() 0 11 2
A resolveCasterClassName() 0 13 3
A getName() 0 3 1
1
<?php namespace BuildR\TestTools\DataSetLoader\XML\StaticType;
2
3
use BuildR\TestTools\Caster\ArrayCaster;
4
use BuildR\TestTools\Caster\BoolCaster;
5
use BuildR\TestTools\Caster\FloatCaster;
6
use BuildR\TestTools\Caster\IntCaster;
7
use BuildR\TestTools\Caster\StringCaster;
8
use BuildR\TestTools\Exception\CasterException;
9
use SimpleXMLElement;
10
11
/**
12
 * Simple helper class for StandardXMLDefinitionParser class. This class takes the
13
 * parsed SimpleXML nodes (dataSetProperty) and process defined values
14
 * and force the defined type if is specified.
15
 *
16
 * BuildR PHP Framework
17
 *
18
 * @author Zoltán Borsos <[email protected]>
19
 * @package TestTools
20
 * @subpackage DataSetLoader\XML\StaticType
21
 *
22
 * @copyright    Copyright 2016, Zoltán Borsos.
23
 * @license      https://github.com/BuildrPHP/Test-Tools/blob/master/LICENSE.md
24
 * @link         https://github.com/BuildrPHP/Test-Tools
25
 */
26
class SimpleXMLNodeTypedAttributeGetter {
27
28
    /**
29
     * List of available casters
30
     *
31
     * @type array
32
     */
33
    private $casters = [
34
        'string' => StringCaster::class,
35
        'int|integer' => IntCaster::class,
36
        'bool|boolean' => BoolCaster::class,
37
        'float|double' => FloatCaster::class,
38
        'array' => ArrayCaster::class,
39
    ];
40
    
41
    /**
42
     * @type \SimpleXMLElement
43
     */
44
    private $element;
45
46
    /**
47
     * SimpleXMLNodeTypedAttributeGetter constructor.
48
     * This is the standard definition <dataSetProperty /> nodes.
49
     *
50
     * @param \SimpleXMLElement $element
51
     */
52 4
    public function __construct(SimpleXMLElement $element) {
53 4
        $this->element = $element;
54 4
    }
55
56
    /**
57
     * Post-processing the node value by additionally specified type
58
     * declaration. If no type is defined, values returned as string.
59
     *
60
     * @return bool|float|int|string
61
     */
62 4
    public function getValue() {
63 4
        $type = (string) $this->element->attributes()->type;
64 4
        $type = (empty($type)) ? 'string' : $type;
65 4
        $value = (string) $this->element->attributes()->value;
66
67 4
        $casterClassName = $this->resolveCasterClassName($type);
68
69
        /** @type \BuildR\TestTools\Caster\CasterInterface $casterObject */
70 3
        $casterObject = new $casterClassName($value);
71 3
        return $casterObject->cast();
72
    }
73
74
    /**
75
     * Resolve the correct caster class by defined type
76
     *
77
     * @param string $type
78
     *
79
     * @return string
80
     * @throws \BuildR\TestTools\Exception\CasterException
81
     */
82 4
    private function resolveCasterClassName($type) {
83 4
        foreach($this->casters as $typeNames => $casterClass) {
84 4
            $names = explode('|', $typeNames);
85
86 4
            if(!in_array($type, $names)) {
87 2
                continue;
88
            }
89
90 3
            return $casterClass;
91 1
        }
92
        
93 1
        throw CasterException::unresolvableType($type);
94
    }
95
96
    /**
97
     * Returns the node name. If the node not defines 'name'
98
     * attribute an empty string will be returned.
99
     *
100
     * @return string
101
     */
102 3
    public function getName() {
103 3
        return (string) $this->element->attributes()->name;
104
    }
105
106
}
107