Completed
Pull Request — master (#18)
by SignpostMarv
03:37
created

Restriction::loadRestriction()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 6
nop 3
dl 0
loc 26
ccs 20
cts 20
cp 1
crap 4
rs 8.5806
c 2
b 0
f 0
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Schema\Inheritance;
3
4
use DOMElement;
5
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
6
use GoetasWebservices\XML\XSDReader\SchemaReader;
7
use GoetasWebservices\XML\XSDReader\SchemaReaderLoadAbstraction;
8
9
class Restriction extends Base
10
{
11
    /**
12
    * @var mixed[][]
13
    */
14
    protected $checks = array();
15
16
    /**
17
    * @param string $type
18
    * @param mixed[] $value
19
    *
20
    * @return $this
21
    */
22 135
    public function addCheck($type, $value)
23
    {
24 135
        $this->checks[$type][] = $value;
25 135
        return $this;
26
    }
27
28
    /**
29
    * @return mixed[][]
30
    */
31 30
    public function getChecks()
32
    {
33 30
        return $this->checks;
34
    }
35
36
    /**
37
    * @param string $type
38
    *
39
    * @return mixed[]
40
    */
41
    public function getChecksByType($type)
42
    {
43
        return isset($this->checks[$type])?$this->checks[$type]:array();
44
    }
45
46 135
    public static function loadRestriction(
47
        SchemaReaderLoadAbstraction $reader,
48
        Type $type,
49
        DOMElement $node
50
    ) {
51 135
        $restriction = new Restriction();
52 135
        $type->setRestriction($restriction);
53 135
        if ($node->hasAttribute("base")) {
54 135
            $reader->findAndSetSomeBase($type, $restriction, $node);
55 45
        } else {
56 135
            $addCallback = function (Type $restType) use ($restriction) {
57 135
                $restriction->setBase($restType);
58 135
            };
59
60 135
            Type::loadTypeWithCallbackOnChildNodes(
61 135
                $reader,
62 135
                $type->getSchema(),
63 135
                $node,
64 90
                $addCallback
65 45
            );
66
        }
67 135
        foreach ($node->childNodes as $childNode) {
68 135
            if ($childNode instanceof DOMElement) {
69 135
                static::maybeLoadRestrictionOnChildNode(
70 135
                    $restriction,
71 90
                    $childNode
72 45
                );
73 45
            }
74 45
        }
75 135
    }
76
77 135
    protected static function maybeLoadRestrictionOnChildNode(
78
        Restriction $restriction,
79
        DOMElement $childNode
80
    ) {
81
        if (
82 135
            in_array(
83 135
                $childNode->localName,
84
                [
85 135
                    'enumeration',
86 45
                    'pattern',
87 45
                    'length',
88 45
                    'minLength',
89 45
                    'maxLength',
90 45
                    'minInclusive',
91 45
                    'maxInclusive',
92 45
                    'minExclusive',
93 45
                    'maxExclusive',
94 45
                    'fractionDigits',
95 45
                    'totalDigits',
96
                    'whiteSpace'
97 45
                ],
98 90
                true
99 45
            )
100 45
        ) {
101 135
            static::definitelyLoadRestrictionOnChildNode(
102 135
                $restriction,
103 90
                $childNode
104 45
            );
105 45
        }
106 135
    }
107
108 135
    protected static function definitelyLoadRestrictionOnChildNode(
109
        Restriction $restriction,
110
        DOMElement $childNode
111
    ) {
112 135
        $restriction->addCheck(
113 135
            $childNode->localName,
114
            [
115 135
                'value' => $childNode->getAttribute("value"),
116 135
                'doc' => SchemaReader::getDocumentation($childNode)
117 45
            ]
118 45
        );
119 135
    }
120
}
121