Passed
Push — master ( 76cc38...d8bdc7 )
by Christopher
02:23
created

SimpleTypeBase::isBaseValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace AlgoWeb\xsdTypes;
3
4
/**
5
 * Base Class representing xsd anySimpleTypes
6
 * @property-write array $enumeration Defines a list of acceptable values
7
 * @property-write integer $fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
8
 * @property-write integer $length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
9
 * @property-write integer $maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
10
 * @property-write integer $maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
11
 * @property-write integer $maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
12
 * @property-write integer $minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
13
 * @property-write integer $minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
14
 * @property-write integer $minLength Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
15
 * @property-write string $pattern Defines the exact sequence of characters that are acceptable
16
 * @property-write integer $totalDigits Specifies the exact number of digits allowed. Must be greater than zero
17
 * @property-write string $whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
18
 */
19
abstract class SimpleTypeBase
20
{
21
    /**
22
     * @Exclude
23
     * @var array Defines a list of acceptable values
24
     */
25
    private $enumeration = array();
26
    /**
27
     * @Exclude
28
     * @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
29
     */
30
    private $maxLength = null;
31
    /**
32
     * @Exclude
33
     * @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
34
     */
35
    private $minLength = null;
36
    /**
37
     * @Exclude
38
     * @var string Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
39
     */
40
    private $whiteSpace = "preserve";
41
    /**
42
     * @Exclude
43
     * @var string Defines the exact sequence of characters that are acceptable
44
     */
45
    private $pattern = null;
46
    /**
47
     * @Exclude
48
     * @var string Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
49
     */
50
    private $fractionDigits = null;
51
    /**
52
     * @Exclude
53
     * @var integer Specifies the lower bounds for numeric values (the value must be greater than this value)
54
     */
55
    private $minExclusive = null;
56
    /**
57
     * @Exclude
58
     * @var integer Specifies the upper bounds for numeric values (the value must be less than this value)
59
     */
60
    private $maxExclusive = null;
61
62
    /**
63
     * @property mixed $__value
64
     */
65
    private $__value = null;
66
67
    /**
68
     * Construct
69
     *
70
     * @param mixed $value
71
     */
72
    public function __construct($value)
73
    {
74
        $this->value($value);
75
    }
76
77
    /**
78
     * Gets or sets the inner value
79
     *
80
     * @param mixed ...$value
81
     * @return mixed
82
     * @throws \Exception
83
     */
84
    public function value(...$value)
85
    {
86
        if (0 >= count($value)) {
87
            return $this->__value;
88
        }
89
        $v = $this->fixValue($value[0]);
90
        $this->isBaseValid($v);
91
        $this->__value = $v;
92
        return $v;
93
    }
94
95
    protected function fixValue($v)
96
    {
97
        return $this->fixWhitespace($v, $this->whiteSpace);
98
    }
99
100
    protected function fixWhitespace($val, $handle = "preserve")
101
    {
102
        switch ($handle) {
103
            case "preserve":
104
                return $val;
105
            case "replace":
106
                return preg_replace('/\s/', ' ', $val);
107
            case "collapse":
108
                return preg_replace('/\s+/', ' ', $val);
109
            default:
110
                throw new \InvalidArgumentException(__CLASS__ . " Called Fix whitespace with invalid handle operation");
111
        }
112
    }
113
114
    private function isBaseValid($v)
115
    {
116
        $this->checkMinLength($v);
117
        $this->checkMaxLength($v);
118
        $this->checkEnumeration($v);
119
        $this->checkPattern($v);
120
        $this->isValid($v);
121
    }
122
123 View Code Duplication
    private function checkMinLength($v)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $stringLen = strlen($v);
126
        if ($this->minLength != null) {
127
            if ($stringLen > $this->minLength) {
128
                throw new \InvalidArgumentException("the provided value for " . __CLASS__ . " is to long minLength: "
129
                    . $this->minLength);
130
            }
131
        }
132
    }
133
134 View Code Duplication
    private function checkMaxLength($v)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $stringLen = strlen($v);
137
        if ($this->maxLength != null) {
138
            if ($stringLen < $this->maxLength) {
139
                throw new \InvalidArgumentException("the provided value for " . __CLASS__ . " is to short MaxLength: "
140
                    . $this->maxLength);
141
            }
142
        }
143
    }
144
145
    private function checkEnumeration($v)
146
    {
147
        if (is_array($this->enumeration) && !in_array($v, $this->enumeration)) {
148
            throw new \InvalidArgumentException("the provided value for " . __CLASS__ . " is not " .
149
                implode(" || ", $this->enumeration));
150
        }
151
    }
152
153
    private function checkPattern($v)
154
    {
155
        if ($this->pattern != null) {
156
            if (!$this->matchesRegexPattern($this->pattern, $v)) {
157
                throw new \InvalidArgumentException("assigned value that dose not match pattern " . __CLASS__);
158
            }
159
        }
160
    }
161
162
    /**
163
     * Checks a pattern against a string
164
     * @param string $pattern the regex pattern
165
     * @param string $string the string to check
166
     * @return bool true if string matches pattern
167
     */
168
    private function matchesRegexPattern($pattern, $string)
169
    {
170
        $matches = null;
171
        return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]);
172
    }
173
174
    abstract protected function isValid($v);
175
176
    public function __set($name, $value)
177
    {
178
        if (!in_array($name, ["enumeration", "totalDigits", "length", "maxLength", "minLength", "whiteSpace",
179
            "pattern", "fractionDigits", "minInclusive", "minExclusive", "maxExclusive"])
180
        ) {
181
            throw new \InvalidArgumentException("Invalid parameters (facets) assignment for: " . __CLASS__);
182
        }
183
        $setFunctionName = "set" . ucfirst($name);
184
        $this->$setFunctionName($value);
185
    }
186
187
    /**
188
     * Gets a string value
189
     *
190
     * @return string
191
     */
192
    public function __toString()
193
    {
194
        return strval($this->__value);
195
196
    }
197
198
    private function setEnumoration($value)
199
    {
200
        if (!is_array($value)) {
201
            throw new \InvalidArgumentException("enumoration values MUST be an array " . __CLASS__);
202
        }
203
        if (0 == count($value)) {
204
            throw new \InvalidArgumentException("enumoration values MUST have at least one value " . __CLASS__);
205
        }
206
        $this->enumeration = $value;
207
    }
208
209
    private function setLength($value)
210
    {
211
        $this->setMinLength($value);
212
        $this->setMaxLength($value);
213
    }
214
215
    private function setMinLength($value)
216
    {
217
        $this->checkLength($value);
218
        $this->minLength = $value;
219
    }
220
221
    private function checkLength($value, $min = 0)
222
    {
223
        if (((int)$value) != $value) {
224
            throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__);
225
        }
226
        if ($min >= $value) {
227
            throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__);
228
        }
229
    }
230
231
    private function setMaxLength($value)
232
    {
233
        $this->checkLength($value);
234
        $this->maxLength = $value;
235
    }
236
237
    private function setWhiteSpaceHandle($value)
238
    {
239
        if (!in_array($value, ["preserve", "replace", "collapse"])) {
240
            throw new \InvalidArgumentException("Invalid white space handleing method " . __CLASS__);
241
        }
242
        $this->whiteSpace = $value;
243
    }
244
245
    private function setPattern($value)
246
    {
247
        if (!$this->checkRegexValidPattern($value)) {
248
            $value = "/" . $value . "/";
249
            if (!$this->checkRegexValidPattern($value)) {
250
                throw new \InvalidArgumentException("Invalid regex Pattern provided: " . __CLASS__);
251
            }
252
        }
253
        $this->pattern = $value;
254
    }
255
256
    private function checkRegexValidPattern($pattern)
257
    {
258
        return (@preg_match($pattern, null) === false);
259
    }
260
261
    private function setFractionDigits($value)
262
    {
263
        $this->checkLength($value);
264
        $this->fractionDigits = $value;
265
    }
266
267
    private function setMinExclusive($value)
268
    {
269
        $this->checkLength($value, -1);
270
        $this->minExclusive = $value;
271
    }
272
273
    private function setMaxExclusive($value)
274
    {
275
        $this->checkLength($value);
276
        $this->maxExclusive = $value;
277
    }
278
}
279