ConsistOfItems::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Deserializer\Condition;
5
6
use function is_iterable;
7
use Stratadox\Specification\Contract\Satisfiable;
8
use Stratadox\Specification\Contract\Specifies;
9
use Stratadox\Specification\Specifying;
10
11
/**
12
 * Condition that accepts data where all items satisfy a condition.
13
 *
14
 * Used to check that all elements in the input array satisfy a particular
15
 * condition.
16
 *
17
 * @author Stratadox
18
 */
19
final class ConsistOfItems implements Specifies
20
{
21
    use Specifying;
22
23
    /** @var Satisfiable */
24
    private $condition;
25
26
    private function __construct(Satisfiable $condition)
27
    {
28
        $this->condition = $condition;
29
    }
30
31
    /**
32
     * Produces a condition that checks if all items satisfy a condition.
33
     *
34
     * @param Satisfiable $passingTheCondition The condition that must pass on
35
     *                                         all items.
36
     * @return Specifies                       The condition to apply on the
37
     *                                         collection.
38
     */
39
    public static function that(Satisfiable $passingTheCondition): Specifies
40
    {
41
        return new self($passingTheCondition);
42
    }
43
44
    /** @inheritdoc */
45
    public function isSatisfiedBy($input): bool
46
    {
47
        if (!is_iterable($input)) {
48
            return false;
49
        }
50
        foreach ($input as $item) {
51
            if (!$this->condition->isSatisfiedBy($item)) {
52
                return false;
53
            }
54
        }
55
        return true;
56
    }
57
}
58