Test Failed
Push — master ( 56388c...2301f2 )
by Jan
02:10
created

Attribute::canSkip()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kontrolio\Data;
4
5
use Kontrolio\Rules\Core\Sometimes;
6
use Kontrolio\Rules\RuleInterface;
7
8
final class Attribute
9
{
10
    private $value;
11
    private $attribute;
12
13
    /**
14
     * @param string $attribute
15
     * @param mixed $value
16
     */
17
    public function __construct($attribute, $value = null)
18
    {
19
        $this->attribute = $attribute;
20
        $this->value = $value;
21
    }
22
23
    public function getName()
24
    {
25
        return $this->attribute;
26
    }
27
28
    public function getValue()
29
    {
30
        return $this->value;
31
    }
32
33
    public function canSkip(RuleInterface $rule)
34
    {
35
        return $rule instanceof Sometimes && $this->isEmpty();
36
    }
37
38
    public function conformsTo(RuleInterface $rule)
39
    {
40
        return $rule->isValid($this->value) ||
41
               $rule->canSkipValidation($this->value) ||
42
               ($rule->emptyValueAllowed() && $this->isEmpty());
43
    }
44
45
    public function isEmpty()
46
    {
47
        return $this->value === null || $this->value === '';
48
    }
49
}
50