1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of disabled property overloading. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\DataStructures; |
8
|
|
|
|
9
|
|
|
use \CryptoManana\Core\Interfaces\DataStructures\PropertyOverloadingInterface as PropertyOverloading; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait DisablePropertyOverloadingTrait - Reusable implementation of disabling property overloading. |
13
|
|
|
* |
14
|
|
|
* @see \CryptoManana\Core\Interfaces\DataStructures\PropertyOverloadingInterface The abstract specification. |
15
|
|
|
* |
16
|
|
|
* @package CryptoManana\Core\Traits\DataStructures |
17
|
|
|
* |
18
|
|
|
* @mixin PropertyOverloading |
19
|
|
|
*/ |
20
|
|
|
trait DisablePropertyOverloadingTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Magic method invoked when attempting to get the value of an inaccessible or a non-existent property. |
24
|
|
|
* |
25
|
|
|
* @param mixed $name The property name. |
26
|
|
|
* |
27
|
|
|
* @return mixed The property value. |
28
|
|
|
*/ |
29
|
562 |
|
public function __get($name) |
30
|
|
|
{ |
31
|
562 |
|
if (property_exists($this, $name)) { |
32
|
562 |
|
return $this->$name; |
33
|
|
|
} else { |
34
|
|
|
throw new \OutOfBoundsException('The property does not exist.'); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Magic method invoked when attempting to create or modify of an inaccessible or a non-existent property. |
40
|
|
|
* |
41
|
|
|
* @param mixed $name The property name. |
42
|
|
|
* @param mixed $value The property value. |
43
|
|
|
* |
44
|
|
|
* @throws \Exception|\InvalidArgumentException The property does not exist or is from another type. |
45
|
|
|
*/ |
46
|
50 |
|
public function __set($name, $value) |
47
|
|
|
{ |
48
|
50 |
|
if (property_exists($this, $name) && gettype($this->$name) === gettype($value)) { |
49
|
50 |
|
$this->$name = $value; |
50
|
|
|
} else { |
51
|
|
|
throw new \InvalidArgumentException('The property does not exist or is of an invalid type.'); |
52
|
|
|
} |
53
|
50 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Magic method invoked when checking of an inaccessible or a non-existent property is set. |
57
|
|
|
* |
58
|
|
|
* @param mixed $name The property name. |
59
|
|
|
* |
60
|
|
|
* @return bool The existence check result. |
61
|
|
|
*/ |
62
|
2 |
|
public function __isset($name) |
63
|
|
|
{ |
64
|
2 |
|
return isset($this->$name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Magic method invoked when attempting to unset an inaccessible or a non-existent property. |
69
|
|
|
* |
70
|
|
|
* @param mixed $name The property name. |
71
|
|
|
* |
72
|
|
|
* @throws \Exception|\LogicException The object does not allow dynamic property removal. |
73
|
|
|
*/ |
74
|
|
|
public function __unset($name) |
75
|
|
|
{ |
76
|
|
|
throw new \LogicException('This data structure does not allow deletion of properties.'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|