Passed
Push — master ( f32ea8...ea79fa )
by Christopher
02:20
created

SimpleTypeBase::checkMaxLength()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
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 array $fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
8
 * @property-write array $length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
9
 * @property-write array $maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
10
 * @property-write array $maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
11
 * @property-write array $maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
12
 * @property-write array $minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
13
 * @property-write array $minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
14
 * @property-write array $minLength Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
15
 * @property-write array $pattern Defines the exact sequence of characters that are acceptable
16
 * @property-write array $totalDigits Specifies the exact number of digits allowed. Must be greater than zero
17
 * @property-write array 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 $whiteSpaceHandle = "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->whiteSpaceHandle);
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
        switch ($name) {
179
            case "enumeration":
180
                $this->setEnumoration($value);
181
                return;
182
            case "totalDigits":
183
            case "length":
184
                $this->setMaxLength($value);
185
                $this->setMinLength($value);
186
                return;
187
            case "maxLength":
188
                $this->setMaxLength($value);
189
                return;
190
            case "minLength":
191
                $this->setMinLength($value);
192
                return;
193
            case "whiteSpaceHandle":
194
                $this->setWhiteSpaceHandle($value);
195
                return;
196
            case "pattern":
197
                $this->setPattern($value);
198
                return;
199
            case "fractionDigits":
200
                $this->setFractionDigits($value);
201
                return;
202
            case "minInclusive":
203
                $value--;
204
            // bump down by one to become MinExclusive
205
            case "minExclusive":
206
                $this->setMinExclusive($value);
207
                return;
208
            case "maxInclusive":
209
                $value++;
210
            // bump up by one to become MaxExclusive
211
            case "maxExclusive":
212
                $this->setMaxExclusive($value);
213
                return;
214
            default:
215
                throw new \InvalidArgumentException("Invalid parameters (facets) assignment for anyURI: " . __CLASS__);
216
        }
217
    }
218
219
    private function setEnumoration($value)
220
    {
221
        if (!is_array($value)) {
222
            throw new \InvalidArgumentException("enumoration values MUST be an array " . __CLASS__);
223
        }
224
        if (0 == count($value)) {
225
            throw new \InvalidArgumentException("enumoration values MUST have at least one value " . __CLASS__);
226
        }
227
        $this->enumeration = $value;
228
    }
229
230
    private function setMaxLength($value)
231
    {
232
        $this->checkLength($value);
233
        $this->maxLength = $value;
234
    }
235
236
    private function checkLength($value, $min = 0)
237
    {
238
        if (((int)$value) != $value) {
239
            throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__);
240
        }
241
        if ($min >= $value) {
242
            throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__);
243
        }
244
    }
245
246
    private function setMinLength($value)
247
    {
248
        $this->checkLength($value);
249
        $this->minLength = $value;
250
    }
251
252
    private function setWhiteSpaceHandle($value)
253
    {
254
        if (!in_array($value, ["preserve", "replace", "collapse"])) {
255
            throw new \InvalidArgumentException("Invalid white space handleing method " . __CLASS__);
256
        }
257
        $this->whiteSpaceHandle = $value;
258
    }
259
260
    private function setPattern($value)
261
    {
262
        if (!$this->checkRegexValidPattern($value)) {
263
            $value = "/" . $value . "/";
264
            if (!$this->checkRegexValidPattern($value)) {
265
                throw new \InvalidArgumentException("Invalid regex Pattern provided: " . __CLASS__);
266
            }
267
        }
268
        $this->pattern = $value;
269
    }
270
271
    private function checkRegexValidPattern($pattern)
272
    {
273
        return (@preg_match($pattern, null) === false);
274
    }
275
276
    private function setFractionDigits($value)
277
    {
278
        $this->checkLength($value);
279
        $this->fractionDigits = $value;
280
    }
281
282
    private function setMinExclusive($value)
283
    {
284
        $this->checkLength($value, -1);
285
        $this->minExclusive = $value;
286
    }
287
288
    private function setMaxExclusive($value)
289
    {
290
        $this->checkLength($value);
291
        $this->maxExclusive = $value;
292
    }
293
294
    /**
295
     * Gets a string value
296
     *
297
     * @return string
298
     */
299
    public function __toString()
300
    {
301
        return strval($this->__value);
302
    }
303
}
304