Passed
Pull Request — master (#21)
by Christopher
01:58
created

LengthTrait::checkLength()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
namespace AlgoWeb\xsdTypes\Facets;
3
4
trait LengthTrait
5
{
6
    /**
7
     * @Exclude
8
     * @var integer Specifies the maximum number of characters or list items allowed.  Must be equal to or greater than zero
9
     */
10
    private $maxLength = null;
11
    /**
12
     * @Exclude
13
     * @var integer Specifies the minimum number of characters or list items allowed.  Must be equal to or greater than zero
14
     */
15
    private $minLength = null;
16
17
    /**
18
     * @param integer $value Specifies the exact number of characters or list items allowed.  Must be equal to or greater than zero
19
     */
20
    protected function setLengthFacet($value)
21
    {
22
        $this->setMinLengthFacet($value);
23
        $this->setMaxLengthFacet($value);
24
    }
25
26
    /**
27
     * @param integer $value Specifies the minimum number of characters or list items allowed.  Must be equal to or greater than zero
28
     */
29
    protected function setMinLengthFacet($value)
30
    {
31
        $this->checkValidMinMaxLength($value);
32
        $this->minLength = $value;
33
    }
34
35
    /**
36
     * @param integer $value
37
     */
38
    private function checkValidMinMaxLength($value, $min = 0)
39
    {
40
        if (((int)$value) != $value) {
41
            throw new \InvalidArgumentException("Length values MUST be castable to int " . __CLASS__);
42
        }
43
        if ($min >= $value) {
44
            throw new \InvalidArgumentException("Length values MUST be greater than 0 " . __CLASS__);
45
        }
46
    }
47
48
    /**
49
     * @param integer $value Specifies the maximum number of characters or list items allowed.  Must be equal to or greater than zero
50
     */
51
    protected function setMaxLengthFacet($value)
52
    {
53
        $this->checkValidMinMaxLength($value);
54
        $this->maxLength = $value;
55
    }
56
57
    private function checkLength($v)
58
    {
59
        if (null != $this->minLength) {
60
            $this->checkMinLength($v);
61
        }
62
        if (null != $this->maxLength) {
63
            $this->checkMaxLength($v);
64
        }
65
    }
66
67 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...
68
    {
69
        $len = $this->getLengthForValue($v);
70
        if ($len > $this->minLength) {
71
            throw new \InvalidArgumentException(
72
                "The provided value for " . __CLASS__ . " is too long - MinLength: "
73
                . $this->minLength
74
            );
75
        }
76
    }
77
78
    private function getLengthForValue($v)
79
    {
80
        if (is_array($v)) {
81
            return count($v);
82
        }
83
        return strlen($v);
84
    }
85
86 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...
87
    {
88
        $len = $this->getLengthForValue($v);
89
        if ($len < $this->maxLength) {
90
            throw new \InvalidArgumentException(
91
                "The provided value for " . __CLASS__ . " is too short - MaxLength: "
92
                . $this->maxLength
93
            );
94
        }
95
    }
96
}
97