Attribute::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Kontrolio\Data;
4
5
use Kontrolio\Rules\Core\Sometimes;
6
use Kontrolio\Rules\RuleInterface;
7
8
/**
9
 * Validated attribute.
10
 */
11
final class Attribute
12
{
13
    private $value;
14
    private $attribute;
15
16
    /**
17
     * @param string $attribute
18
     * @param mixed $value
19
     */
20
    public function __construct($attribute, $value = null)
21
    {
22
        $this->attribute = $attribute;
23
        $this->value = $value;
24
    }
25
26
    public function getName()
27
    {
28
        return $this->attribute;
29
    }
30
31
    public function getValue()
32
    {
33
        return $this->value;
34
    }
35
36
    /**
37
     * Indicates whether the attribute can skip validation by the given rule.
38
     *
39
     * @param RuleInterface $rule
40
     *
41
     * @return bool
42
     */
43
    public function canSkip(RuleInterface $rule)
44
    {
45
        return $rule instanceof Sometimes && $this->isEmpty();
46
    }
47
48
    /**
49
     * Indicates whether the attribute is valid according to the given rule.
50
     *
51
     * @param RuleInterface $rule
52
     *
53
     * @return bool
54
     */
55
    public function conformsTo(RuleInterface $rule)
56
    {
57
        return $rule->isValid($this->value) ||
58
               $rule->canSkipValidation($this->value) ||
59
               ($rule->emptyValueAllowed() && $this->isEmpty());
60
    }
61
62
    public function isEmpty()
63
    {
64
        return $this->value === null || $this->value === '';
65
    }
66
}
67