Passed
Pull Request — master (#2)
by Christopher
03:56 queued 01:47
created

SimpleTypeBase::setWhiteSpaceHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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
        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
    private function setEnumoration($value)
205
    {
206
        if (!is_array($value)) {
207
            throw new \InvalidArgumentException("enumoration values MUST be an array " . __CLASS__);
208
        }
209
        if (0 == count($value)) {
210
            throw new \InvalidArgumentException("enumoration values MUST have at least one value " . __CLASS__);
211
        }
212
        $this->enumeration = $value;
213
    }
214
215
    private function setMaxLength($value)
216
    {
217
        $this->checkLength($value);
218
        $this->maxLength = $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 setMinLength($value)
232
    {
233
        $this->checkLength($value);
234
        $this->minLength = $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 handleSetNumeric($name, $value)
262
    {
263
        switch ($name) {
264
265
            case "fractionDigits":
266
                $this->setFractionDigits($value);
267
                return;
268
            case "minInclusive":
269
                $value--;
270
            // bump down by one to become MinExclusive
271
            case "minExclusive":
272
                $this->setMinExclusive($value);
273
                return;
274
            case "maxInclusive":
275
                $value++;
276
            // bump up by one to become MaxExclusive
277
            case "maxExclusive":
278
                $this->setMaxExclusive($value);
279
                return;
280
            default:
281
                throw new \InvalidArgumentException("Invalid parameters (facets) assignment for: " . __CLASS__);
282
        }
283
    }
284
285
    private function setFractionDigits($value)
286
    {
287
        $this->checkLength($value);
288
        $this->fractionDigits = $value;
289
    }
290
291
    private function setMinExclusive($value)
292
    {
293
        $this->checkLength($value, -1);
294
        $this->minExclusive = $value;
295
    }
296
297
    private function setMaxExclusive($value)
298
    {
299
        $this->checkLength($value);
300
        $this->maxExclusive = $value;
301
    }
302
303
    /**
304
     * Gets a string value
305
     *
306
     * @return string
307
     */
308
    public function __toString()
309
    {
310
        return strval($this->__value);
311
    }
312
}
313