Passed
Push — master ( 63b2c7...9fa2c5 )
by Baptiste
53s
created

Assertion::minCount()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 4
1
<?php declare(strict_types=1);
2
namespace Behapi\Assert;
3
4
use Assert\Assertion as Beberlei;
5
6
use function sprintf;
7
use function mb_strpos;
8
9
/**
10
 * While the PR in links are not merged and released, let's use this assertion
11
 * class to satisfy our needs...
12
 *
13
 * @method static bool allEmpty(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty for all values.
14
 * @method static bool allIsCountable(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values.
15
 * @method static bool allMaxCount(array|\Countable $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable does not have more than $count elements for all values.
16
 * @method static bool allMinCount(array|\Countable $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable has at least $count elements for all values.
17
 * @method static bool allNotContains(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars for all values.
18
 * @method static bool nullOrEmpty(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty or that the value is null.
19
 * @method static bool nullOrIsCountable(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable or that the value is null.
20
 * @method static bool nullOrMaxCount(array|\Countable $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable does not have more than $count elements or that the value is null.
21
 * @method static bool nullOrMinCount(array|\Countable $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable has at least $count elements or that the value is null.
22
 * @method static bool nullOrNotContains(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string does not contains a sequence of chars or that the value is null.
23
 *
24
 * @link https://github.com/beberlei/assert/pull/264
25
 * @link https://github.com/beberlei/assert/pull/266
26
 * @link https://github.com/beberlei/assert/pull/267
27
 */
28
abstract class Assertion extends Beberlei
29
{
30
    const INVALID_STRING_NOT_CONTAINS = 226;
31
    const INVALID_COUNTABLE = 227;
32
    const INVALID_MIN_COUNT = 228;
33
    const INVALID_MAX_COUNT = 229;
34
35
    /**
36
     * Assert that string does not contains a sequence of chars.
37
     *
38
     * @param mixed                $string
39
     * @param string               $needle
40
     * @param string|callable|null $message
41
     * @param string|null          $propertyPath
42
     * @param string               $encoding
43
     *
44
     * @return bool
45
     */
46
    public static function notContains($string, $needle, $message = null, $propertyPath = null, $encoding = 'utf8')
47
    {
48
        static::string($string, $message, $propertyPath);
49
50
        if (false !== mb_strpos($string, $needle, null, $encoding)) {
51
            $message = sprintf(
52
                static::generateMessage($message ?: 'Value "%s" contains "%s".'),
53
                static::stringify($string),
54
                static::stringify($needle)
55
            );
56
57
            throw static::createException($string, $message, static::INVALID_STRING_NOT_CONTAINS, $propertyPath, ['needle' => $needle, 'encoding' => $encoding]);
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * Assert that value is countable.
65
     *
66
     * @param mixed                $value
67
     * @param string|callable|null $message
68
     * @param string|null          $propertyPath
69
     *
70
     * @return bool
71
     */
72
    public static function isCountable($value, $message = null, $propertyPath = null)
73
    {
74
        if (function_exists('is_countable')) {
75
            $assert = is_countable($value);
76
        } else {
77
            $assert = is_array($value) || $value instanceof \Countable;
78
        }
79
80
        if (!$assert) {
81
            $message = \sprintf(
82
                static::generateMessage($message ?: 'Value "%s" is not an array and does not implement Countable.'),
83
                static::stringify($value)
84
            );
85
86
            throw static::createException($value, $message, static::INVALID_COUNTABLE, $propertyPath);
87
        }
88
89
        return true;
90
    }
91
92
    /**
93
     * Assert that the countable has at least $count elements.
94
     *
95
     * @param array|\Countable $countable
96
     * @param int              $count
97
     * @param string|null      $message
98
     * @param string|null      $propertyPath
99
     *
100
     * @return bool
101
     */
102
    public static function minCount($countable, $count, $message = null, $propertyPath = null)
103
    {
104
        if ($count > \count($countable)) {
105
            $message = \sprintf(
106
                static::generateMessage($message ?: 'List should have at least %d elements, but only has %d elements.'),
107
                static::stringify($count),
108
                static::stringify(\count($countable))
109
            );
110
111
            throw static::createException($countable, $message, static::INVALID_MIN_COUNT, $propertyPath, ['count' => $count]);
112
        }
113
114
        return true;
115
    }
116
117
    /**
118
     * Assert that the countable does not have more than $count elements.
119
     *
120
     * @param array|\Countable $countable
121
     * @param int              $count
122
     * @param string|null      $message
123
     * @param string|null      $propertyPath
124
     *
125
     * @return bool
126
     */
127
    public static function maxCount($countable, $count, $message = null, $propertyPath = null)
128
    {
129
        if ($count < \count($countable)) {
130
            $message = \sprintf(
131
                static::generateMessage($message ?: 'List should have no more than %d elements, but has %d elements.'),
132
                static::stringify($count),
133
                static::stringify(\count($countable))
134
            );
135
136
            throw static::createException($countable, $message, static::INVALID_MAX_COUNT, $propertyPath, ['count' => $count]);
137
        }
138
139
        return true;
140
    }
141
142
    public static function empty($value, $message = null, $propertyPath = null)
143
    {
144
        parent::noContent($value, $message, $propertyPath);
145
    }
146
}
147