Completed
Push — master ( 8c65d7...1637a0 )
by Ben
15:52 queued 50s
created

Manager::allProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    /**
16
     * @var Property[]
17
     */
18
    private $properties = [];
19
20
    /**
21
     * Add a new property to the stack
22
     *
23
     * @param string $name the property name
24 18
     * @param string $type the data type for the property (string, int, bool, etc)
25
     * @param mixed $default the default value, until explicity assigned this is
26 18
     * the value for the property
27 18
     */
28
    public function addProperty($name, $type = null, $default = null): Property
29 18
    {
30
        $property = new Property($name, $type, $default);
31
        $this->properties[$name] = $property;
32
33
        return $property;
34
    }
35
36
    /**
37
     * Get the property by its name
38
     *
39 12
     * @param  string $name
40
     * @return Property
41 12
     * @throws Exception if the property doesn't exist
42 3
     */
43
    public function getProperty(string $name): Property
44 9
    {
45
        if (!$this->hasProperty($name)) {
46
            throw new PropertyException('Unknown property "'.$name.'"');
47
        }
48
        return $this->properties[$name];
49
    }
50
51
    /**
52
     * Does the manager contain an instance of the property
53 18
     * based on it's name
54
     * @param  string  $name property identifier
55 18
     * @return boolean
56
     */
57
    public function hasProperty(string $name): bool
58
    {
59
        return isset($this->properties[$name]);
60
    }
61
62
    /**
63 3
     * Get all of the properties registered by the manager
64
     *
65 3
     * @return Property[]
66 3
     */
67
    public function allProperties(): array
68 3
    {
69 3
        return $this->properties;
70
    }
71
72
    /**
73
     * Remove the property from the manager
74
     *
75
     * @param  string $name
76
     * @return bool
77
     */
78 9
    public function removeProperty(string $name): bool
79
    {
80 9
        if (!$this->hasProperty($name)) {
81
            return false;
82
        }
83
        unset($this->properties[$name]);
84
        return true;
85
    }
86
87
    /**
88
     * Get the value of the property, if it exists
89 6
     *
90
     * @param  string $name property name
91 6
     * @return mixed
92
     * @throws Exception if the property doesn't exist
93
     */
94
    public function getValue(string $name)
95
    {
96
        return $this->getProperty($name)->getValue();
97
    }
98
99
    /**
100
     * Set the value of the property, if it exists
101
     *
102
     * @param string $name  the property identifier
103
     * @param mixed $value the value to store against the property
104
     * @throws Exception if the property doesn't exist
105
     */
106
    public function setValue(string $name, $value)
107
    {
108
        return $this->getProperty($name)->setValue($value);
109
    }
110
}
111