Passed
Push — master ( d318b7...9b70cb )
by Christopher
01:39
created

LengthTrait::checkMinLengthArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 2
eloc 7
nc 2
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
    private function checkMaxLength($v)
47
    {
48
        if ($this->maxLength != null) {
49
            if ($this->minLength != null) {
50
                if (is_array($v)) {
51
                    $this->checkMaxLengthArray($v);
52
                } else {
53
                    $this->checkMaxLengthString($v);
54
                }
55
            }
56
        }
57
    }
58
59 View Code Duplication
    private function checkMaxLengthArray($v)
60
    {
61
        assert(is_array($v));
62
        $arrayLen = count($v);
63
        if ($arrayLen < $this->maxLength) {
64
            throw new \InvalidArgumentException(
65
                "the provided value for " . __CLASS__ . " is to short MaxLength: "
66
                . $this->maxLength
67
            );
68
        }
69
    }
70
71 View Code Duplication
    private function checkMaxLengthString($v)
0 ignored issues
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...
72
    {
73
        $stringLen = strlen($v);
74
        if ($stringLen < $this->maxLength) {
75
            throw new \InvalidArgumentException(
76
                "the provided value for " . __CLASS__ . " is to short MaxLength: "
77
                . $this->maxLength
78
            );
79
        }
80
    }
81
82
    private function checkMinLength($v)
83
    {
84
        if ($this->minLength != null) {
85
            if (is_array($v)) {
86
                $this->checkMinLengthArray($v);
87
            } else {
88
                $this->checkMinLengthString($v);
89
            }
90
        }
91
    }
92
93 View Code Duplication
    private function checkMinLengthArray($v)
94
    {
95
        assert(is_array($v));
96
        $arrayLen = count($v);
97
        if ($arrayLen > $this->minLength) {
98
            throw new \InvalidArgumentException(
99
                "the provided value for " . __CLASS__ . " is to long MinLength: "
100
                . $this->minLength
101
            );
102
        }
103
    }
104
105 View Code Duplication
    private function checkMinLengthString($v)
0 ignored issues
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...
106
    {
107
        $stringLen = strlen($v);
108
        if ($stringLen > $this->minLength) {
109
            throw new \InvalidArgumentException(
110
                "the provided value for " . __CLASS__ . " is to long MinLength: "
111
                . $this->minLength
112
            );
113
        }
114
    }
115
}
116