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

HasValidFields   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 34
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toString() 0 3 1
A __construct() 0 3 1
B matches() 0 17 7
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