Completed
Push — master ( 1bc299...b12741 )
by Kirill
03:33
created

Attribute::addMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of Properties package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Serafim\Properties\Attribute;
11
12
/**
13
 * Class Attribute
14
 */
15
class Attribute implements AttributeInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @var int
24
     */
25
    private $type;
26
27
    /**
28
     * @var Matchable
29
     */
30
    private $matcher;
31
32
    /**
33
     * Attribute constructor.
34
     * @param string $name
35
     * @param int $type
36
     */
37
    public function __construct(string $name, int $type = self::TYPE_UNDEFINED)
38
    {
39
        $this->name = $name;
40
        $this->type = $type;
41
    }
42
43
    /**
44
     * @param Matchable $hint
45
     * @return Matchable
46
     */
47
    public function addMatcher(Matchable $hint): Matchable
48
    {
49
        $this->matcher = $hint;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isReadable(): bool
58
    {
59
        return $this->type === static::TYPE_READABLE || $this->type === static::TYPE_PROPERTY;
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function isWritable(): bool
66
    {
67
        return $this->type === static::TYPE_WRITABLE || $this->type === static::TYPE_PROPERTY;
68
    }
69
70
    /**
71
     * @param mixed $value
72
     * @return bool
73
     */
74
    public function match($value): bool
75
    {
76
        return $this->matcher && $this->matcher->match($value);
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getName(): string
83
    {
84
        return $this->name;
85
    }
86
}
87