Passed
Push — master ( 2ea326...ab87be )
by Christopher
02:14
created

SimpleTypeBase::handleSetNumeric()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 17
nc 6
nop 2
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
        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
            default:
200
                $this->handleSetNumeric($name, $value);
201
        }
202
203
    }
204
205
    private function setEnumoration($value)
206
    {
207
        if (!is_array($value)) {
208
            throw new \InvalidArgumentException("enumoration values MUST be an array " . __CLASS__);
209
        }
210
        if (0 == count($value)) {
211
            throw new \InvalidArgumentException("enumoration values MUST have at least one value " . __CLASS__);
212
        }
213
        $this->enumeration = $value;
214
    }
215
216
    private function setMaxLength($value)
217
    {
218
        $this->checkLength($value);
219
        $this->maxLength = $value;
220
    }
221
222
    private function checkLength($value, $min = 0)
223
    {
224
        if (((int)$value) != $value) {
225
            throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__);
226
        }
227
        if ($min >= $value) {
228
            throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__);
229
        }
230
    }
231
232
    private function setMinLength($value)
233
    {
234
        $this->checkLength($value);
235
        $this->minLength = $value;
236
    }
237
238
    private function setWhiteSpaceHandle($value)
239
    {
240
        if (!in_array($value, ["preserve", "replace", "collapse"])) {
241
            throw new \InvalidArgumentException("Invalid white space handleing method " . __CLASS__);
242
        }
243
        $this->whiteSpace = $value;
244
    }
245
246
    private function setPattern($value)
247
    {
248
        if (!$this->checkRegexValidPattern($value)) {
249
            $value = "/" . $value . "/";
250
            if (!$this->checkRegexValidPattern($value)) {
251
                throw new \InvalidArgumentException("Invalid regex Pattern provided: " . __CLASS__);
252
            }
253
        }
254
        $this->pattern = $value;
255
    }
256
257
    private function checkRegexValidPattern($pattern)
258
    {
259
        return (@preg_match($pattern, null) === false);
260
    }
261
262
    private function handleSetNumeric($name, $value)
263
    {
264
        switch ($name) {
265
266
            case "fractionDigits":
267
                $this->setFractionDigits($value);
268
                return;
269
            case "minInclusive":
270
                $value--;
271
            // bump down by one to become MinExclusive
272
            case "minExclusive":
273
                $this->setMinExclusive($value);
274
                return;
275
            case "maxInclusive":
276
                $value++;
277
            // bump up by one to become MaxExclusive
278
            case "maxExclusive":
279
                $this->setMaxExclusive($value);
280
                return;
281
            default:
282
                throw new \InvalidArgumentException("Invalid parameters (facets) assignment for: " . __CLASS__);
283
        }
284
    }
285
286
    private function setFractionDigits($value)
287
    {
288
        $this->checkLength($value);
289
        $this->fractionDigits = $value;
290
    }
291
292
    private function setMinExclusive($value)
293
    {
294
        $this->checkLength($value, -1);
295
        $this->minExclusive = $value;
296
    }
297
298
    private function setMaxExclusive($value)
299
    {
300
        $this->checkLength($value);
301
        $this->maxExclusive = $value;
302
    }
303
304
    /**
305
     * Gets a string value
306
     *
307
     * @return string
308
     */
309
    public function __toString()
310
    {
311
        return strval($this->__value);
312
    }
313
}
314