1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author UniKado <[email protected]> |
4
|
|
|
* @copyright (c) 2016, UniKado |
5
|
|
|
* @package UK\DynamicProperties; |
6
|
|
|
* @since 2016-04-01 |
7
|
|
|
* @version 0.1.0-dev (Tata43) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
declare( strict_types = 1 ); |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace UK\DynamicProperties; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* If you extend you're class from this class, you're class becomes a automatic working magic get + set method for |
19
|
|
|
* read + write accessing the values of all available getXyz + setXyz methods by dynamic properties. |
20
|
|
|
* |
21
|
|
|
* For example: If you're class defines the two public get methods 'getFoo()' and 'getFooBar()' and the public |
22
|
|
|
* set method 'setFooBar( $value ) you can also access it with the dynamic properties |
23
|
|
|
* $instance->foo [is readonly] and $instance->fooBar [read + write]. |
24
|
|
|
* |
25
|
|
|
* But do'nt forget to document you're dyn. class properties with the @ property annotation |
26
|
|
|
* |
27
|
|
|
* @since v0.1.0-dev (Tata43) |
28
|
|
|
*/ |
29
|
|
|
abstract class ExplicitGetterSetter extends ExplicitGetter |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
// <editor-fold desc="// = = = = P R O T E C T E D F I E L D S = = = = = = = = = = = = = = = = = = = = = = ="> |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The names of all set* methods (without the leading set) that should not be mapped as dynamic properties |
37
|
|
|
* |
38
|
|
|
* @type array |
39
|
|
|
*/ |
40
|
|
|
protected $ignoreSetProperties = []; |
41
|
|
|
|
42
|
|
|
// </editor-fold> |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
// <editor-fold desc="// = = = = P U B L I C M E T H O D S = = = = = = = = = = = = = = = = = = = = = = = = ="> |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The magic set method to let you access all setter methods by dynamic properties. |
49
|
|
|
* |
50
|
|
|
* @param string $name The name of the dynamic property. |
51
|
|
|
* @param mixed $value The value to set. |
52
|
|
|
* @throws \LogicException Is thrown if the property does not exist or if writing is requested but not allowed. |
53
|
|
|
*/ |
54
|
4 |
|
public function __set( string $name, $value ) |
55
|
|
|
{ |
56
|
|
|
|
57
|
4 |
|
if ( ! $this->hasWritableProperty( $name, $setterName ) ) |
58
|
|
|
{ |
59
|
1 |
|
throw new \LogicException( 'No such property: ' . __CLASS__ . '::$' . $name. '!' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
$this->$setterName( $value ); |
63
|
|
|
|
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Magic isset implementation. |
68
|
|
|
* |
69
|
|
|
* @param string $name The name of the required property. |
70
|
|
|
* @return boolean |
71
|
|
|
*/ |
72
|
3 |
|
public function __isset( string $name ) |
73
|
|
|
{ |
74
|
|
|
|
75
|
3 |
|
if ( $this->hasWritableProperty( $name, $setterName ) ) |
76
|
|
|
{ |
77
|
2 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return parent::__isset( $name ); |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns, if a property with the defined name exists for write access. |
86
|
|
|
* |
87
|
|
|
* @param string $name The name of the property. |
88
|
|
|
* @param string $setterName Returns the name of the associated set method, if method returns TRUE. |
89
|
|
|
* @return boolean |
90
|
|
|
*/ |
91
|
4 |
View Code Duplication |
public function hasWritableProperty( string $name, &$setterName ) : bool |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
|
94
|
4 |
|
if ( \in_array( $name, $this->ignoreSetProperties ) ) |
95
|
|
|
{ |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
$setterName = 'set' . \ucfirst( $name ); |
100
|
|
|
|
101
|
4 |
|
return \method_exists( $this, $setterName ); |
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// </editor-fold> |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.