Completed
Pull Request — master (#5)
by Ben
33:05 queued 31:24
created

Manager::getProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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