1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\base; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @author Robert Korulczyk <[email protected]> |
13
|
|
|
* @since 2.0.11 |
14
|
|
|
*/ |
15
|
|
|
trait MutatorsTrait |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns the value of an object property. |
20
|
|
|
* |
21
|
|
|
* Do not call this method directly as it is a PHP magic method that |
22
|
|
|
* will be implicitly called when executing `$value = $object->property;`. |
23
|
|
|
* @param string $name the property name |
24
|
|
|
* @return mixed the property value |
25
|
|
|
* @throws UnknownPropertyException if the property is not defined |
26
|
|
|
* @throws InvalidCallException if the property is write-only |
27
|
|
|
* @see __set() |
28
|
|
|
*/ |
29
|
|
|
public function __get($name) |
30
|
|
|
{ |
31
|
|
|
$getter = 'get' . $name; |
32
|
|
|
if (method_exists($this, $getter)) { |
33
|
|
|
return $this->$getter(); |
34
|
|
|
} elseif (method_exists($this, 'set' . $name)) { |
35
|
|
|
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); |
36
|
|
|
} else { |
37
|
|
|
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets value of an object property. |
43
|
|
|
* |
44
|
|
|
* Do not call this method directly as it is a PHP magic method that |
45
|
|
|
* will be implicitly called when executing `$object->property = $value;`. |
46
|
|
|
* @param string $name the property name or the event name |
47
|
|
|
* @param mixed $value the property value |
48
|
|
|
* @throws UnknownPropertyException if the property is not defined |
49
|
|
|
* @throws InvalidCallException if the property is read-only |
50
|
|
|
* @see __get() |
51
|
|
|
*/ |
52
|
|
|
public function __set($name, $value) |
53
|
|
|
{ |
54
|
|
|
$setter = 'set' . $name; |
55
|
|
|
if (method_exists($this, $setter)) { |
56
|
|
|
$this->$setter($value); |
57
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
58
|
|
|
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); |
59
|
|
|
} else { |
60
|
|
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks if a property is set, i.e. defined and not null. |
66
|
|
|
* |
67
|
|
|
* Do not call this method directly as it is a PHP magic method that |
68
|
|
|
* will be implicitly called when executing `isset($object->property)`. |
69
|
|
|
* |
70
|
|
|
* Note that if the property is not defined, false will be returned. |
71
|
|
|
* @param string $name the property name or the event name |
72
|
|
|
* @return boolean whether the named property is set (not null). |
73
|
|
|
* @see http://php.net/manual/en/function.isset.php |
74
|
|
|
*/ |
75
|
|
|
public function __isset($name) |
76
|
|
|
{ |
77
|
|
|
$getter = 'get' . $name; |
78
|
|
|
if (method_exists($this, $getter)) { |
79
|
|
|
return $this->$getter() !== null; |
80
|
|
|
} else { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets an object property to null. |
87
|
|
|
* |
88
|
|
|
* Do not call this method directly as it is a PHP magic method that |
89
|
|
|
* will be implicitly called when executing `unset($object->property)`. |
90
|
|
|
* |
91
|
|
|
* Note that if the property is not defined, this method will do nothing. |
92
|
|
|
* If the property is read-only, it will throw an exception. |
93
|
|
|
* @param string $name the property name |
94
|
|
|
* @throws InvalidCallException if the property is read only. |
95
|
|
|
* @see http://php.net/manual/en/function.unset.php |
96
|
|
|
*/ |
97
|
|
|
public function __unset($name) |
98
|
|
|
{ |
99
|
|
|
$setter = 'set' . $name; |
100
|
|
|
if (method_exists($this, $setter)) { |
101
|
|
|
$this->$setter(null); |
102
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
103
|
|
|
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Calls the named method which is not a class method. |
109
|
|
|
* |
110
|
|
|
* Do not call this method directly as it is a PHP magic method that |
111
|
|
|
* will be implicitly called when an unknown method is being invoked. |
112
|
|
|
* @param string $name the method name |
113
|
|
|
* @param array $params method parameters |
114
|
|
|
* @throws UnknownMethodException when calling unknown method |
115
|
|
|
* @return mixed the method return value |
116
|
|
|
*/ |
117
|
|
|
public function __call($name, $params) |
118
|
|
|
{ |
119
|
|
|
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns a value indicating whether a property is defined. |
124
|
|
|
* A property is defined if: |
125
|
|
|
* |
126
|
|
|
* - the class has a getter or setter method associated with the specified name |
127
|
|
|
* (in this case, property name is case-insensitive); |
128
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
129
|
|
|
* |
130
|
|
|
* @param string $name the property name |
131
|
|
|
* @param boolean $checkVars whether to treat member variables as properties |
132
|
|
|
* @return boolean whether the property is defined |
133
|
|
|
* @see canGetProperty() |
134
|
|
|
* @see canSetProperty() |
135
|
|
|
*/ |
136
|
|
|
public function hasProperty($name, $checkVars = true) |
137
|
|
|
{ |
138
|
|
|
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns a value indicating whether a property can be read. |
143
|
|
|
* A property is readable if: |
144
|
|
|
* |
145
|
|
|
* - the class has a getter method associated with the specified name |
146
|
|
|
* (in this case, property name is case-insensitive); |
147
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
148
|
|
|
* |
149
|
|
|
* @param string $name the property name |
150
|
|
|
* @param boolean $checkVars whether to treat member variables as properties |
151
|
|
|
* @return boolean whether the property can be read |
152
|
|
|
* @see canSetProperty() |
153
|
|
|
*/ |
154
|
|
|
public function canGetProperty($name, $checkVars = true) |
155
|
|
|
{ |
156
|
|
|
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns a value indicating whether a property can be set. |
161
|
|
|
* A property is writable if: |
162
|
|
|
* |
163
|
|
|
* - the class has a setter method associated with the specified name |
164
|
|
|
* (in this case, property name is case-insensitive); |
165
|
|
|
* - the class has a member variable with the specified name (when `$checkVars` is true); |
166
|
|
|
* |
167
|
|
|
* @param string $name the property name |
168
|
|
|
* @param boolean $checkVars whether to treat member variables as properties |
169
|
|
|
* @return boolean whether the property can be written |
170
|
|
|
* @see canGetProperty() |
171
|
|
|
*/ |
172
|
|
|
public function canSetProperty($name, $checkVars = true) |
173
|
|
|
{ |
174
|
|
|
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns a value indicating whether a method is defined. |
179
|
|
|
* |
180
|
|
|
* The default implementation is a call to php function `method_exists()`. |
181
|
|
|
* You may override this method when you implemented the php magic method `__call()`. |
182
|
|
|
* @param string $name the method name |
183
|
|
|
* @return boolean whether the method is defined |
184
|
|
|
*/ |
185
|
|
|
public function hasMethod($name) |
186
|
|
|
{ |
187
|
|
|
return method_exists($this, $name); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|