Completed
Push — feature/issue-49 ( 9a3dca...dba810 )
by Mikaël
28:22
created

Rules::getEnumerationRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
8
9
class Rules
10
{
11
    /**
12
     * @var StructAttribute
13
     */
14
    private $attribute;
15
    /**
16
     * @var AbstractModelFile
17
     */
18
    private $file;
19
    /**
20
     * @var PhpMethod
21
     */
22
    private $method;
23
    /**
24
     * @param StructAttribute $attribute
25
     */
26 88
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute)
27
    {
28 66
        $this
29 88
            ->setFile($file)
30 88
            ->setMethod($method)
31 88
            ->setAttribute($attribute);
32 88
    }
33
    /**
34
     * @param string $parameterName
35
     * @param bool $itemType
36
     */
37 88
    public function applyRules($parameterName, $itemType = false)
38
    {
39 88
        foreach ($this->getAttribute()->getMeta() as $metaName=>$metaValue) {
40 88
            $rule = $this->getRule($metaName);
41 88
            if ($rule instanceof AbstractRule) {
42 28
                $rule->applyRule($parameterName, $metaValue, $itemType);
43 6
            }
44 66
        }
45 88
        if ($this->getAttribute()->isArray() && !$itemType) {
46 48
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
47 88
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->getAttribute())) {
48 44
            $this->getEnumerationRule()->applyRule($parameterName, null, $itemType);
49 87
        } elseif ($itemType) {
50 44
            $this->getItemTypeRule()->applyRule($parameterName, null, $itemType);
51 78
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute()))) instanceof AbstractRule) {
52 60
            $rule->applyRule($parameterName, null, $itemType);
53 45
        }
54 88
    }
55
    /**
56
     * @param string $metaName
57
     * @return AbstractRule
58
     */
59 88
    protected function getRule($metaName)
60
    {
61 88
        if (is_string($metaName)) {
62 88
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($metaName));
63 88
            if (class_exists($className)) {
64 88
                return new $className($this);
65
            }
66 66
        }
67 88
        return null;
68
    }
69
    /**
70
     * @return ArrayRule
71
     */
72 48
    public function getArrayRule()
73
    {
74 48
        return $this->getRule('array');
75
    }
76
    /**
77
     * @return EnumerationRule
78
     */
79 44
    public function getEnumerationRule()
80
    {
81 44
        return $this->getRule('enumeration');
82
    }
83
    /**
84
     * @return ItemTypeRule
85
     */
86 44
    public function getItemTypeRule()
87
    {
88 44
        return $this->getRule('itemType');
89
    }
90
    /**
91
     * @return StructAttribute
92
     */
93 88
    public function getAttribute()
94
    {
95 88
        return $this->attribute;
96
    }
97
    /**
98
     * @param StructAttribute $attribute
99
     */
100 88
    public function setAttribute(StructAttribute $attribute)
101
    {
102 88
        $this->attribute = $attribute;
103 88
        return $this;
104
    }
105
    /**
106
     * @return AbstractModelFile
107
     */
108 88
    public function getFile()
109
    {
110 88
        return $this->file;
111
    }
112
    /**
113
     * @param AbstractModelFile $file
114
     */
115 88
    public function setFile(AbstractModelFile $file)
116
    {
117 88
        $this->file = $file;
118 88
        return $this;
119
    }
120
    /**
121
     * @return PhpMethod
122
     */
123 88
    public function getMethod()
124
    {
125 88
        return $this->method;
126
    }
127
    /**
128
     * @param PhpMethod $method
129
     */
130 88
    public function setMethod(PhpMethod $method)
131
    {
132 88
        $this->method = $method;
133 88
        return $this;
134
    }
135
}
136