Passed
Push — master ( 8bdcbb...696730 )
by Zbigniew
03:20
created

IdValidator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 79
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isNull() 0 4 1
A assertIsNull() 0 6 2
A isValidIdString() 0 4 3
A assertIsValidIdString() 0 6 2
B isValidIdArray() 0 15 5
A assertIsValidIdArray() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the zibios/wrike-php-library package.
5
 *
6
 * (c) Zbigniew Ślązak
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zibios\WrikePhpLibrary\Validator;
13
14
/**
15
 * Request Id Validator.
16
 */
17
class IdValidator
18
{
19
    /**
20
     * @param mixed $value
21
     *
22
     * @return bool
23
     */
24 15
    public static function isNull($value)
25
    {
26 15
        return $value === null;
27
    }
28
29
    /**
30
     * @param mixed $value
31
     *
32
     * @throws \InvalidArgumentException
33
     */
34 15
    public static function assertIsNull($value)
35
    {
36 15
        if (self::isNull($value) === false) {
37 3
            throw new \InvalidArgumentException('Null expected!');
38
        }
39 12
    }
40
41
    /**
42
     * @param mixed $value
43
     *
44
     * @return bool
45
     */
46 104
    public static function isValidIdString($value)
47
    {
48 104
        return is_string($value) && trim($value) !== '' && strlen($value) < 16;
49
    }
50
51
    /**
52
     * @param mixed $value
53
     *
54
     * @throws \InvalidArgumentException
55
     */
56 90
    public static function assertIsValidIdString($value)
57
    {
58 90
        if (self::isValidIdString($value) === false) {
59 6
            throw new \InvalidArgumentException(sprintf('Invalid Id, should be not empty string!'));
60
        }
61 84
    }
62
63
    /**
64
     * @param mixed $value
65
     *
66
     * @return bool
67
     */
68 18
    public static function isValidIdArray($value)
69
    {
70 18
        if (is_array($value) === false || count($value) === 0) {
71 4
            return false;
72
        }
73
74
        /** @var array $value */
75 14
        foreach ($value as $id) {
76 14
            if (self::isValidIdString($id) === false) {
77 14
                return false;
78
            }
79
        }
80
81 9
        return true;
82
    }
83
84
    /**
85
     * @param mixed $value
86
     *
87
     * @throws \InvalidArgumentException
88
     */
89 18
    public static function assertIsValidIdArray($value)
90
    {
91 18
        if (self::isValidIdArray($value) === false) {
92 9
            throw new \InvalidArgumentException();
93
        }
94 9
    }
95
}
96