Completed
Push — develop ( de5d0b...06b47b )
by Mikaël
31:11
created

Rules::applyRules()   B

Complexity

Conditions 8
Paths 15

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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