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

Attribute::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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