Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
19 / 19
CRAP
100.00% covered (success)
100.00%
1 / 1
Rules
100.00% covered (success)
100.00%
44 / 44
100.00% covered (success)
100.00%
19 / 19
31
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 applyRules
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
10
 getRule
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getArrayRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getXmlRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEnumerationRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getItemTypeRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getListRule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setAttribute
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMethod
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMethod
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMethods
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGenerator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 ruleHasBeenAppliedToAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasRuleBeenAppliedToAttribute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applyRulesFromAttribute
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getAppliedRuleToAttributeKey
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace WsdlToPhp\PackageGenerator\File\Validation;
6
7use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
8use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9use WsdlToPhp\PackageGenerator\Generator\Generator;
10use WsdlToPhp\PackageGenerator\Model\StructAttribute;
11use WsdlToPhp\PhpGenerator\Element\PhpMethod;
12
13final class Rules
14{
15    private StructAttribute $attribute;
16
17    private AbstractModelFile $file;
18
19    private PhpMethod $method;
20
21    private MethodContainer $methods;
22
23    private static array $rulesAppliedToAttribute = [];
24
25    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods)
26    {
27        $this->file = $file;
28        $this->method = $method;
29        $this->attribute = $attribute;
30        $this->methods = $methods;
31    }
32
33    public function applyRules(string $parameterName, bool $itemType = false): void
34    {
35        if (!$itemType && $this->attribute->isArray()) {
36            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
37        } elseif (!$itemType && $this->attribute->isXml()) {
38            $this->getXmlRule()->applyRule($parameterName, null, $itemType);
39        } elseif (!$itemType && $this->attribute->isList()) {
40            $this->getListRule()->applyRule($parameterName, null, $itemType);
41        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) {
42            $this->getEnumerationRule()->applyRule($parameterName, null);
43        } elseif ($itemType) {
44            $this->getItemTypeRule()->applyRule($parameterName, null);
45        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) {
46            $rule->applyRule($parameterName, null, $itemType);
47        }
48
49        $this->applyRulesFromAttribute($parameterName, $itemType);
50    }
51
52    public function getRule(string $name): ?AbstractRule
53    {
54        $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name));
55
56        return class_exists($className) ? new $className($this) : null;
57    }
58
59    public function getArrayRule(): ArrayRule
60    {
61        return $this->getRule('array');
62    }
63
64    public function getXmlRule(): XmlRule
65    {
66        return $this->getRule('xml');
67    }
68
69    public function getEnumerationRule(): EnumerationRule
70    {
71        return $this->getRule('enumeration');
72    }
73
74    public function getItemTypeRule(): ItemTypeRule
75    {
76        return $this->getRule('itemType');
77    }
78
79    public function getListRule(): ListRule
80    {
81        return $this->getRule('list');
82    }
83
84    public function getAttribute(): StructAttribute
85    {
86        return $this->attribute;
87    }
88
89    public function setAttribute(StructAttribute $attribute): self
90    {
91        $this->attribute = $attribute;
92
93        return $this;
94    }
95
96    public function getFile(): AbstractModelFile
97    {
98        return $this->file;
99    }
100
101    public function getMethod(): PhpMethod
102    {
103        return $this->method;
104    }
105
106    public function setMethod(PhpMethod $method): self
107    {
108        $this->method = $method;
109
110        return $this;
111    }
112
113    public function getMethods(): MethodContainer
114    {
115        return $this->methods;
116    }
117
118    public function getGenerator(): Generator
119    {
120        return $this->file->getGenerator();
121    }
122
123    public static function ruleHasBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): void
124    {
125        self::$rulesAppliedToAttribute[self::getAppliedRuleToAttributeKey($rule, $value, $attribute)] = true;
126    }
127
128    public static function hasRuleBeenAppliedToAttribute(AbstractRule $rule, $value, StructAttribute $attribute): bool
129    {
130        return array_key_exists(self::getAppliedRuleToAttributeKey($rule, $value, $attribute), self::$rulesAppliedToAttribute);
131    }
132
133    private function applyRulesFromAttribute(string $parameterName, bool $itemType = false): void
134    {
135        foreach ($this->attribute->getMeta() as $metaName => $metaValue) {
136            if (!($rule = $this->getRule($metaName)) instanceof AbstractRule) {
137                continue;
138            }
139
140            $rule->applyRule($parameterName, $metaValue, $itemType);
141        }
142    }
143
144    private static function getAppliedRuleToAttributeKey(AbstractRule $rule, $value, StructAttribute $attribute): string
145    {
146        return implode('_', [
147            $rule->validationRuleComment($value),
148            $attribute->getOwner()->getName(),
149            $attribute->getName(),
150        ]);
151    }
152}