Passed
Push — master ( db02fd...873e90 )
by Christopher
02:22 queued 48s
created

LengthTrait::checkMinLength()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 10
Ratio 83.33 %

Importance

Changes 0
Metric Value
dl 10
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace AlgoWeb\xsdTypes\Facets;
4
5
trait LengthTrait
6
{
7
    /**
8
     * @Exclude
9
     * @var integer Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
10
     */
11
    private $maxLength = null;
12
    /**
13
     * @Exclude
14
     * @var integer Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
15
     */
16
    private $minLength = null;
17
18
    protected function setLengthFacet($value)
19
    {
20
        $this->setMinLengthFacet($value);
21
        $this->setMaxLengthFacet($value);
22
    }
23
24
    protected function setMinLengthFacet($value)
25
    {
26
        $this->checkValidMinMaxLength($value);
27
        $this->minLength = $value;
28
    }
29
30
    private function checkValidMinMaxLength($value, $min = 0)
31
    {
32
        if (((int)$value) != $value) {
33
            throw new \InvalidArgumentException("length values MUST be castable to int " . __CLASS__);
34
        }
35
        if ($min >= $value) {
36
            throw new \InvalidArgumentException("length values MUST be greater then 0 " . __CLASS__);
37
        }
38
    }
39
40
    protected function setMaxLengthFacet($value)
41
    {
42
        $this->checkValidMinMaxLength($value);
43
        $this->maxLength = $value;
44
    }
45
46 View Code Duplication
    private function checkMaxLength($v)
47
    {
48
        $stringLen = strlen($v);
49
        if ($this->maxLength != null) {
50
            if ($stringLen < $this->maxLength) {
51
                throw new \InvalidArgumentException(
52
                    "the provided value for " . __CLASS__ . " is to short MaxLength: "
53
                    . $this->maxLength
54
                );
55
            }
56
        }
57
    }
58
59 View Code Duplication
    private function checkMinLength($v)
60
    {
61
        $stringLen = strlen($v);
62
        if ($this->minLength != null) {
63
            if ($stringLen > $this->minLength) {
64
                throw new \InvalidArgumentException(
65
                    "the provided value for " . __CLASS__ . " is to long minLength: "
66
                    . $this->minLength
67
                );
68
            }
69
        }
70
    }
71
}
72