Passed
Push — master ( 45207e...ca5789 )
by Josh
14:32
created

HasValidFields::matches()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 6
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 7
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace JoshGaber\NovaUnit\Constraints;
4
5
use Laravel\Nova\Fields\Field;
6
use Laravel\Nova\Panel;
7
use PHPUnit\Framework\Constraint\Constraint;
8
9
class HasValidFields extends Constraint
10
{
11
    private $allowPanels;
12
13 5
    public function __construct(bool $allowPanels = false)
14
    {
15 5
        $this->allowPanels = $allowPanels;
16 5
    }
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function toString(): string
22
    {
23 2
        return 'contains valid fields only';
24
    }
25
26 5
    public function matches($other): bool
27
    {
28 5
        if (! \is_array($other)) {
29 1
            return false;
30
        }
31
32 4
        foreach ($other as $field) {
33 4
            if ($this->allowPanels && $field instanceof Panel) {
34 2
                if (! $this->matches($field->data)) {
35 2
                    return false;
36
                }
37 4
            } elseif (! $field instanceof Field) {
38 4
                return false;
39
            }
40
        }
41
42 3
        return true;
43
    }
44
}
45