|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Benrowe\Properties; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The property manager class is a collection container for managing |
|
7
|
|
|
* serveral properties |
|
8
|
|
|
* |
|
9
|
|
|
* @package Benrowe\Properties |
|
10
|
|
|
*/ |
|
11
|
|
|
class Manager |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Property[] |
|
15
|
|
|
*/ |
|
16
|
|
|
private $properties = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add a new property to the stack |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $name the property name |
|
22
|
|
|
* @param string $type the data type for the property (string, int, bool, etc) |
|
23
|
|
|
* @param mixed $default the default value, until explicity assigned this is |
|
24
|
18 |
|
* the value for the property |
|
25
|
|
|
*/ |
|
26
|
18 |
|
public function addProperty($name, $type = null, $default = null): Property |
|
27
|
18 |
|
{ |
|
28
|
|
|
$property = new Property($name, $type, $default); |
|
29
|
18 |
|
$this->properties[$name] = $property; |
|
30
|
|
|
|
|
31
|
|
|
return $property; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the property by its name |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @return Property |
|
39
|
12 |
|
* @throws Exception if the property doesn't exist |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function getProperty(string $name): Property |
|
42
|
3 |
|
{ |
|
43
|
|
|
if (!$this->hasProperty($name)) { |
|
44
|
9 |
|
throw new PropertyException('Unknown property "'.$name.'"'); |
|
45
|
|
|
} |
|
46
|
|
|
return $this->properties[$name]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Does the manager contain an instance of the property |
|
51
|
|
|
* based on it's name |
|
52
|
|
|
* @param string $name property identifier |
|
53
|
18 |
|
* @return boolean |
|
54
|
|
|
*/ |
|
55
|
18 |
|
public function hasProperty(string $name): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return isset($this->properties[$name]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get all of the properties registered by the manager |
|
62
|
|
|
* |
|
63
|
3 |
|
* @return Property[] |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function allProperties(): array |
|
66
|
3 |
|
{ |
|
67
|
|
|
return $this->properties; |
|
68
|
3 |
|
} |
|
69
|
3 |
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Remove the property from the manager |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $name |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
public function removeProperty(string $name): bool |
|
77
|
|
|
{ |
|
78
|
9 |
|
if (!$this->hasProperty($name)) { |
|
79
|
|
|
return false; |
|
80
|
9 |
|
} |
|
81
|
|
|
unset($this->properties[$name]); |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the value of the property, if it exists |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name property name |
|
89
|
6 |
|
* @return mixed |
|
90
|
|
|
* @throws Exception if the property doesn't exist |
|
91
|
6 |
|
*/ |
|
92
|
|
|
public function getValue(string $name) |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->getProperty($name)->getValue(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the value of the property, if it exists |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name the property identifier |
|
101
|
|
|
* @param mixed $value the value to store against the property |
|
102
|
|
|
* @throws Exception if the property doesn't exist |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setValue(string $name, $value) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->getProperty($name)->setValue($value); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get all the values from the properties |
|
111
|
|
|
*/ |
|
112
|
|
|
public function allValues(): array |
|
113
|
|
|
{ |
|
114
|
|
|
$values = []; |
|
115
|
|
|
foreach (array_keys($this->properties) as $key) { |
|
116
|
|
|
$values[$key] = $this->getValue($key); |
|
117
|
|
|
} |
|
118
|
|
|
return $values; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|