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
|
|
|
* |
56
|
|
|
* @param array $definition |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setDefinition(array $definition) |
61
|
|
|
{ |
62
|
|
|
$this->definition = $definition; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns true if the given directive exists (even if it is null). |
69
|
|
|
* |
70
|
|
|
* @param string $directive |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function has(string $directive) |
75
|
|
|
{ |
76
|
|
|
return array_key_exists($directive, $this->definition); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the value for a directive if it exists, otherwise return a default. |
81
|
|
|
* |
82
|
|
|
* @param string $directive |
83
|
|
|
* @param mixed|null $default |
84
|
|
|
* |
85
|
|
|
* @return mixed|null |
86
|
|
|
*/ |
87
|
|
|
public function get(string $directive, $default = null) |
88
|
|
|
{ |
89
|
|
|
return $this->has($directive) ? $this->definition[$directive] : $default; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set a value for a directive. |
94
|
|
|
* |
95
|
|
|
* @param string $directive |
96
|
|
|
* @param mixed $value |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function set(string $directive, $value) |
101
|
|
|
{ |
102
|
|
|
$this->definition[$directive] = $value; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the underlying associative array definition. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function toArray() |
113
|
|
|
{ |
114
|
|
|
return $this->definition; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|