Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

ContainsAtLeastOne::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Constraint;
6
7
use VGirol\JsonApiStructure\Messages;
8
9
class ContainsAtLeastOne extends Constraint
10
{
11
    /**
12
     * @var array
13
     */
14
    private $members;
15
16
    /**
17
     * Class constructor.
18
     *
19
     * @param array $members
20
     */
21 270
    public function __construct(array $members)
22
    {
23 270
        $this->members = $members;
24 270
    }
25
26
    /**
27
     * Returns a string representation of the constraint.
28
     */
29 21
    public function default(): string
30
    {
31 21
        return sprintf(Messages::CONTAINS_AT_LEAST_ONE, implode(', ', $this->members));
32
    }
33
34
    /**
35
     * Evaluates the constraint for parameter $inspected. Returns true if the constraint is met, false otherwise.
36
     *
37
     * @param mixed  $inspected value or object to evaluate
38
     *
39
     * @return boolean
40
     */
41 270
    protected function handle($inspected): bool
42
    {
43 270
        if (!\is_array($inspected)) {
44 3
            return false;
45
        }
46
47 267
        foreach ($this->members as $member) {
48 267
            if (\array_key_exists($member, $inspected)) {
49 243
                return true;
50
            }
51
        }
52
53 42
        return false;
54
    }
55
}
56