Completed
Pull Request — master (#9)
by Jodie
02:25
created

AbstractDefinition::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Rexlabs\Smokescreen\Definition;
4
5
class AbstractDefinition
6
{
7
    /**
8
     * An optional key which identifies this definition
9
     *
10
     * @var string|null
11
     */
12
    protected $key;
13
14
    /**
15
     * An associative-array of definition settings.
16
     *
17
     * @var array
18
     */
19
    protected $definition;
20
21
    /**
22
     * AbstractDefinition constructor.
23
     *
24
     * @param string|null $key
25
     * @param array       $definition
26
     */
27
    public function __construct($key, array $definition = [])
28
    {
29
        $this->setKey($key);
30
        $this->setDefinition($definition);
31
    }
32
33
    /**
34
     * @param string|null $key
35
     *
36
     * @return $this
37
     */
38
    public function setKey($key)
39
    {
40
        $this->key = $key;
41
42
        return $this;
43
    }
44
45
    /**
46
     * @return null|string
47
     */
48
    public function key()
49
    {
50
        return $this->key;
51
    }
52
53
    /**
54
     * Replace the definition settings with the given associative array.
55
     * @param array $definition
56
     *
57
     * @return $this
58
     */
59
    public function setDefinition(array $definition)
60
    {
61
        $this->definition = $definition;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Returns true if the given directive exists (even if it is null)
68
     * @param string $directive
69
     *
70
     * @return bool
71
     */
72
    public function has(string $directive)
73
    {
74
        return array_key_exists($directive, $this->definition);
75
    }
76
77
    /**
78
     * Get the value for a directive if it exists, otherwise return a default.
79
     * @param string $directive
80
     * @param null   $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
81
     *
82
     * @return mixed|null
83
     */
84
    public function get(string $directive, $default = null)
85
    {
86
        return $this->has($directive) ? $this->definition[$directive] : $default;
87
    }
88
89
    /**
90
     * Set a value for a directive.
91
     * @param string $directive
92
     * @param        $value
93
     *
94
     * @return $this
95
     */
96
    public function set(string $directive, $value)
97
    {
98
        $this->definition[$directive] = $value;
99
100
        return $this;
101
    }
102
103
    /**
104
     * Get the underlying associative array definition.
105
     * @return array
106
     */
107
    public function toArray()
108
    {
109
        return $this->definition;
110
    }
111
}