Passed
Push — master ( de4139...96da48 )
by Josh
06:18
created

HasValidFields::__construct()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 4
    public function matches($other): bool
27
    {
28 4
        foreach ($other as $field) {
29 4
            if ($this->allowPanels && $field instanceof Panel) {
30 2
                if (! $this->matches($field->data)) {
31 2
                    return false;
32
                }
33 4
            } elseif (! $field instanceof Field) {
34 4
                return false;
35
            }
36
        }
37
38 3
        return true;
39
    }
40
}
41