Completed
Push — master ( fcc408...ec4885 )
by Zoltán
06:43
created

tryGetClassFromCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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
     * Runtime caching
43
     *
44
     * @type array
45
     */
46
    private $cache = [];
47
    
48
    /**
49
     * @type \SimpleXMLElement
50
     */
51
    private $element;
52
53
    /**
54
     * SimpleXMLNodeTypedAttributeGetter constructor.
55
     * This is the standard definition <dataSetProperty /> nodes.
56
     *
57
     * @param \SimpleXMLElement $element
58
     */
59 3
    public function __construct(SimpleXMLElement $element) {
60 3
        $this->element = $element;
61 3
    }
62
63
    /**
64
     * Post-processing the node value by additionally specified type
65
     * declaration. If no type is defined, values returned as string.
66
     *
67
     * @return bool|float|int|string
68
     */
69 3
    public function getValue() {
70 3
        $type = (string) $this->element->attributes()->type;
71 3
        $type = (empty($type)) ? 'string' : $type;
72 3
        $value = (string) $this->element->attributes()->value;
73
74 3
        $casterClassName = $this->tryGetClassFromCache($type);
75
76
        // If caching not provides result
77 3
        if($casterClassName === NULL) {
78 3
            $casterClassName = $this->resolveCasterClassName($type);
79 3
        }
80
81
        /** @type \BuildR\TestTools\Caster\CasterInterface $casterObject */
82 3
        $casterObject = new $casterClassName($value);
83 3
        return $casterObject->cast();
84
    }
85
86
    /**
87
     * Resolve the correct caster class by defined type
88
     *
89
     * @param string $type
90
     *
91
     * @return string
92
     * @throws \BuildR\TestTools\Exception\CasterException
93
     */
94 3
    private function resolveCasterClassName($type) {
95 3
        foreach($this->casters as $typeNames => $casterClass) {
96 3
            $names = explode('|', $typeNames);
97
98 3
            if(!in_array($type, $names)) {
99 1
                continue;
100
            }
101
102 3
            return $casterClass;
103
        }
104
        
105
        throw CasterException::unresolvableType($type);
106
    }
107
    
108
    /**
109
     * Try to fetch resolved type caster class from runtime cache
110
     *
111
     * @param string $type
112
     *
113
     * @return string|NULL
114
     */
115 3
    private function tryGetClassFromCache($type) {
116 3
        if(isset($this->cache[$type])) {
117
            return $this->cache[$type];
118
        }
119
120 3
        return NULL;
121
    }
122
123
    /**
124
     * Returns the node name. If the node not defines 'name'
125
     * attribute an empty string will be returned.
126
     *
127
     * @return string
128
     */
129 3
    public function getName() {
130 3
        return (string) $this->element->attributes()->name;
131
    }
132
133
}
134