Completed
Push — master ( 4fc579...31b3bf )
by Ben
9s
created

Property::setType()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 17
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Benrowe\Properties;
6
7
use Closure;
8
9
/**
10
 * Defines a unique property
11
 *
12
 * @package Benrowe\Properties
13
 * @todo add support for validating a property's value when being set
14
 */
15
class Property
16
{
17
    private $name;
18
    private $type = null;
19
    private $default = null;
20
    private $value = null;
21
22
    private $setter;
23
    private $getter;
24
25
    /**
26
     * Create a new Property Instance
27
     *
28
     * @param string $name
29
     * @param string $type
30
     * @param string $default
31
     */
32
    public function __construct(string $name, string $type = null, $default = null)
33
    {
34
        $this->setName($name);
35
        $this->setType($type);
36
        $this->setDefault($default);
37
    }
38
39
    /**
40
     * Set the property name
41
     * @param string $name the name of the property
42
     */
43
    public function setName(string $name)
44
    {
45
        // add name validation
46
        $this->name = $name;
47
    }
48
49
    /**
50
     * Get the property name
51
     *
52
     * @return string
53
     */
54
    public function getName(): string
55
    {
56
        return $this->name;
57
    }
58
59
    /**
60
     * Set the type for the property
61
     *
62
     * @param string $type
63
     * @todo add support for interface/class checking!
64
     */
65
    public function setType($type)
66
    {
67
        if ($type === null) {
68
            // no type set
69
            $this->type = null;
70
            return $this;
71
        }
72
73
        $types = [
74
            'string',
75
            'integer',
76
            'float',
77
            'boolean',
78
            'array',
79
            'object',
80
            'null',
81
            'resource',
82
        ];
83
        $type = strtolower($type);
84
        if (!in_array($type, $types, true)) {
85
            throw new PropertyException('Invalid type');
86
        }
87
        $this->type = $type;
88
    }
89
90
    /**
91
     * Get the property type
92
     * @return string|null
93
     */
94
    public function getType()
95
    {
96
        return $this->type;
97
    }
98
99
    /**
100
     * Set the default value of the property if nothing is explicitly set
101
     *
102
     * @param mixed $default
103
     */
104
    public function setDefault($default)
105
    {
106
        $this->default = $default;
107
    }
108
109
    /**
110
     * Get the default value
111
     *
112
     * @return mixed
113
     */
114
    public function getDefault()
115
    {
116
        return $this->default;
117
    }
118
119
    /**
120
     * Set the value against the property
121
     *
122
     * @param mixed $value
123
     */
124
    public function setValue($value)
125
    {
126
        if ($this->setter) {
127
            $value = call_user_func($this->setter, $value);
128
        }
129
        $this->value = $value;
130
    }
131
132
    /**
133
     * Get the currently set value, if no value is set the default is used
134
     *
135
     * @return mixed
136
     */
137
    public function getValue()
138
    {
139
        if ($this->value === null) {
140
            return $this->default;
141
        }
142
        $value = $this->value;
143
        if ($this->getter) {
144
            $value = call_user_func($this->getter, $value);
145
        }
146
147
        return $value;
148
    }
149
150
    /**
151
     * Inject a custom closure to handle the storage of the value when it is
152
     * set into the property
153
     * @param  Closure $setter the custom function to run when the value is
154
     * being set
155
     * @return self
156
     */
157
    public function setter(Closure $setter)
158
    {
159
        $this->setter = $setter;
160
161
        return $this;
162
    }
163
164
    /**
165
     * Specify a custom closer to handle the retreival of the value stored
166
     * against this property
167
     *
168
     * @param  Closure $getter [description]
169
     * @return self
170
     */
171
    public function getter(Closure $getter)
172
    {
173
        $this->getter = $getter;
174
175
        return $this;
176
    }
177
}
178