Manager::addProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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
     * @return void
103
     * @throws Exception if the property doesn't exist
104
     */
105
    public function setValue(string $name, $value)
106
    {
107
        $this->getProperty($name)->setValue($value);
108
    }
109
110
    /**
111
     * Get all the values from the properties
112
     */
113
    public function allValues(): array
114
    {
115
        $values = [];
116
        foreach (array_keys($this->properties) as $key) {
117
            $values[$key] = $this->getValue($key);
118
        }
119
        return $values;
120
    }
121
}
122