IdValidator::isValidIdArray()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

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