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

HasField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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 HasField extends Constraint
10
{
11
    private $fieldName;
12
    private $allowPanels;
13
14 10
    public function __construct(string $fieldName, bool $allowPanels = false)
15
    {
16 10
        $this->fieldName = $fieldName;
17 10
        $this->allowPanels = $allowPanels;
18 10
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    public function toString(): string
24
    {
25 4
        return \sprintf('contains field "%s"', $this->fieldName);
26
    }
27
28 10
    public function matches($other): bool
29
    {
30 10
        foreach ($other as $field) {
31 10
            if ($this->allowPanels && $field instanceof Panel) {
32 2
                if ($this->matches($field->data)) {
33 2
                    return true;
34
                }
35 10
            } elseif ($field instanceof Field) {
36
                if (
37 10
                    $field->attribute === $this->fieldName ||
38 10
                    \mb_strtolower($field->name) === \mb_strtolower($this->fieldName)
39
                ) {
40 10
                    return true;
41
                }
42
            }
43
        }
44
45 4
        return false;
46
    }
47
}
48