1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Benrowe\Properties; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The property manager class is a collection container for managing |
9
|
|
|
* serveral properties |
10
|
|
|
* |
11
|
|
|
* @package Benrowe\Properties |
12
|
|
|
*/ |
13
|
|
|
class Manager |
14
|
|
|
{ |
15
|
|
|
private $properties = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Add a new property to the stack |
19
|
|
|
* @param string $name the property name |
20
|
|
|
* @param string $type the data type for the property (string, int, bool, etc) |
21
|
|
|
* @param mixed $default the default value, until explicity assigned this is |
22
|
|
|
* the value for the property |
23
|
|
|
*/ |
24
|
|
|
public function addProperty($name, $type = null, $default = null): Property |
25
|
|
|
{ |
26
|
|
|
$property = new Property($name, $type, $default); |
27
|
|
|
$this->properties[$name] = $property; |
28
|
|
|
|
29
|
|
|
return $property; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* [getProperty description] |
34
|
|
|
* @param [type] $name [description] |
|
|
|
|
35
|
|
|
* @return [type] [description] |
|
|
|
|
36
|
|
|
* @throws Exception if the property doesn't exist |
37
|
|
|
*/ |
38
|
|
|
public function getProperty($name): Property |
39
|
|
|
{ |
40
|
|
|
if (!$this->hasProperty($name)) { |
41
|
|
|
throw new Exception('Unknown property "'.$name.'"'); |
42
|
|
|
} |
43
|
|
|
return $this->properties[$name]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Does the manager contain an instance of the property |
48
|
|
|
* based on it's name |
49
|
|
|
* @param string $name property identifier |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
|
|
public function hasProperty($name): bool |
53
|
|
|
{ |
54
|
|
|
return isset($this->properties[$name]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Remove the property from the manager |
59
|
|
|
* @param string $name |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function removeProperty($name): bool |
63
|
|
|
{ |
64
|
|
|
if (!$this->hasProperty($name)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
unset($this->properties[$name]); |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the value of the property, if it exists |
73
|
|
|
* @param [type] $name [description] |
|
|
|
|
74
|
|
|
* @return [type] [description] |
|
|
|
|
75
|
|
|
* @throws Exception if the property doesn't exist |
76
|
|
|
*/ |
77
|
|
|
public function getValue($name) |
78
|
|
|
{ |
79
|
|
|
return $this->getProperty($name)->getValue(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set the value of the property, if it exists |
84
|
|
|
* @param [type] $name [description] |
|
|
|
|
85
|
|
|
* @param [type] $value [description] |
|
|
|
|
86
|
|
|
* @throws Exception if the property doesn't exist |
87
|
|
|
*/ |
88
|
|
|
public function setValue($name, $value) |
89
|
|
|
{ |
90
|
|
|
return $this->getProperty($name)->setValue($value); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.