1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Teto\Object; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Typed property function for class |
7
|
|
|
* |
8
|
|
|
* @author USAMI Kenta <[email protected]> |
9
|
|
|
* @copyright 2016 Baguette HQ |
10
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
*/ |
12
|
|
|
trait TypedProperty |
13
|
|
|
{ |
14
|
|
|
use TypeAssert; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
private $properties = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set property (magic method) |
21
|
|
|
* |
22
|
|
|
* @param string $name |
23
|
|
|
* @param mixed $value |
24
|
|
|
* @throws \OutOfRangeException If you set to undefined property |
25
|
|
|
* @throws \InvalidArgumentException If differed from the defined type |
26
|
|
|
* @throws \RangeException If differed from the defined length |
27
|
|
|
* @see http://php.net/manual/language.oop5.magic.php |
28
|
|
|
* @suppress PhanUndeclaredStaticProperty |
29
|
|
|
*/ |
30
|
25 |
|
public function __set($name, $value) |
31
|
|
|
{ |
32
|
25 |
|
if (!isset(self::$property_types[$name])) { |
|
|
|
|
33
|
1 |
|
throw new \OutOfRangeException("Unexpected key:'$name'"); |
34
|
|
|
} |
35
|
|
|
|
36
|
24 |
|
$type = TypeDefinition::parse(self::$property_types[$name]); |
37
|
|
|
|
38
|
24 |
|
if ($type->is_array) { |
39
|
13 |
|
self::assertArrayOrObject($value); |
40
|
12 |
|
$values = $value; |
41
|
|
|
} else { |
42
|
11 |
|
$values = [$value]; |
43
|
|
|
} |
44
|
|
|
|
45
|
23 |
|
foreach ($values as $v) { |
46
|
20 |
|
self::assertValue($type->expected, $name, $v, $type->is_nullable); |
47
|
|
|
} |
48
|
17 |
|
if ($type->len !== null && count($value) !== $type->len) { |
49
|
2 |
|
throw new \RangeException("Unexpected length:{$name} (expects {$type->len})"); |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
$this->properties[$name] = $value; |
53
|
15 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get property (magic method) |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* @return mixed |
60
|
|
|
* @see http://php.net/manual/language.oop5.magic.php |
61
|
|
|
* @suppress PhanUndeclaredStaticProperty |
62
|
|
|
*/ |
63
|
15 |
|
public function __get($name) |
64
|
|
|
{ |
65
|
15 |
|
if (!isset(self::$property_types[$name])) { |
|
|
|
|
66
|
|
|
throw new \OutOfRangeException("Unexpected key:'$name'"); |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
return isset($this->properties[$name]) ? $this->properties[$name] : null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $name |
74
|
|
|
* @return bool |
75
|
|
|
* @see http://php.net/manual/language.oop5.magic.php |
76
|
|
|
*/ |
77
|
15 |
|
public function __isset($name) |
78
|
|
|
{ |
79
|
15 |
|
return isset($this->properties[$name]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* @see http://php.net/manual/language.oop5.magic.php |
85
|
|
|
* @suppress PhanUndeclaredStaticProperty |
86
|
|
|
*/ |
87
|
|
|
public function __unset($name) |
88
|
|
|
{ |
89
|
|
|
if (isset(self::$property_types[$name])) { |
|
|
|
|
90
|
|
|
$this->properties[$name] = null; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array |
96
|
|
|
* @suppress PhanUndeclaredStaticProperty |
97
|
|
|
*/ |
98
|
|
|
private function setProperties(array $properties) |
99
|
|
|
{ |
100
|
|
|
foreach (self::$property_types as $name => $_) { |
|
|
|
|
101
|
|
|
if (isset($properties[$name])) { |
102
|
|
|
$this->$name = $properties[$name]; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|