Completed
Push — master ( b53abb...31ca73 )
by Zoltán
03:02
created

SimpleXMLNodeTypedAttributeGetter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 61
ccs 0
cts 29
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
C getValue() 0 26 11
A getName() 0 3 1
1
<?php namespace BuildR\TestTools\DataSetLoader\XML\Helper;
2
3
use SimpleXMLElement;
4
5
/**
6
 * Simple helper class for StandardXMLDefinitionParser class. This class takes the
7
 * parsed SimpleXML nodes (dataSetProperty) and process defined values
8
 * and force the defined type if is specified.
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package TestTools
14
 * @subpackage DataSetLoader\XML\Helper
15
 *
16
 * @copyright    Copyright 2016, Zoltán Borsos.
17
 * @license      https://github.com/BuildrPHP/Test-Tools/blob/master/LICENSE.md
18
 * @link         https://github.com/BuildrPHP/Test-Tools
19
 */
20
class SimpleXMLNodeTypedAttributeGetter {
21
22
    /**
23
     * @type \SimpleXMLElement
24
     */
25
    private $element;
26
27
    /**
28
     * SimpleXMLNodeTypedAttributeGetter constructor.
29
     * This is the standard definition <dataSetProperty /> nodes.
30
     *
31
     * @param \SimpleXMLElement $element
32
     */
33
    public function __construct(SimpleXMLElement $element) {
34
        $this->element = $element;
35
    }
36
37
    /**
38
     * Post-processing the node value by additionally specified type
39
     * declaration. If no type is defined, values returned as string.
40
     *
41
     * @return bool|float|int|string
42
     */
43
    public function getValue() {
44
        $type = (string) $this->element->attributes()->type;
45
        $type = (empty($type)) ? 'string' : $type;
46
        $value = (string) $this->element->attributes()->value;
47
48
        switch($type) {
49
            case 'int':
50
            case 'integer':
51
                $value = (int) $value;
52
                break;
53
            case 'bool':
54
            case 'boolean':
55
                if($value == 'true' || $value == 'false') {
56
                    $value = ($value == 'true') ? TRUE : FALSE;
57
                }
58
59
                $value = (bool) $value;
60
                break;
61
            case 'float':
62
            case 'double':
63
                $value = (float) $value;
64
                break;
65
        }
66
67
        return $value;
68
    }
69
70
    /**
71
     * Returns the node name. If the node not defines 'name'
72
     * attribute an empty string will be returned.
73
     *
74
     * @return string
75
     */
76
    public function getName() {
77
        return (string) $this->element->attributes()->name;
78
    }
79
80
}
81