Created
January 19, 2016 13:55
-
-
Save turboezh/a42322c254bf61fca966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\helpers; | |
use yii\base\InvalidCallException; | |
use yii\base\UnknownPropertyException; | |
trait MagicPropertiesTrait | |
{ | |
/** | |
* Returns the value of a property. | |
* This method will check in the following order and act accordingly: | |
* | |
* - a property defined by a getter: return the getter result | |
* | |
* Do not call this method directly as it is a PHP magic method that | |
* will be implicitly called when executing `$value = $component->property;`. | |
* | |
* @param string $name the property name | |
* | |
* @return mixed the property value or the value of a behavior's property | |
* @throws UnknownPropertyException if the property is not defined | |
* @throws InvalidCallException if the property is write-only. | |
* @see __set() | |
*/ | |
public function __get($name) | |
{ | |
$getter = 'get' . $name; | |
if (method_exists($this, $getter)) { | |
// read property, e.g. getName() | |
return $this->$getter(); | |
} | |
if (method_exists($this, 'set' . $name)) { | |
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); | |
} else { | |
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name); | |
} | |
} | |
/** | |
* Sets the value of a property. | |
* This method will check in the following order and act accordingly: | |
* | |
* - a property defined by a setter: set the property value | |
* | |
* Do not call this method directly as it is a PHP magic method that | |
* will be implicitly called when executing `$component->property = $value;`. | |
* | |
* @param string $name the property name or the event name | |
* @param mixed $value the property value | |
* | |
* @throws UnknownPropertyException if the property is not defined | |
* @throws InvalidCallException if the property is read-only. | |
* @see __get() | |
*/ | |
public function __set($name, $value) | |
{ | |
$setter = 'set' . $name; | |
if (method_exists($this, $setter)) { | |
// set property | |
$this->$setter($value); | |
return; | |
} | |
if (method_exists($this, 'get' . $name)) { | |
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); | |
} else { | |
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); | |
} | |
} | |
/** | |
* Checks if a property is set, i.e. defined and not null. | |
* This method will check in the following order and act accordingly: | |
* | |
* - a property defined by a setter: return whether the property is set | |
* - return `false` for non existing properties | |
* | |
* Do not call this method directly as it is a PHP magic method that | |
* will be implicitly called when executing `isset($component->property)`. | |
* | |
* @param string $name the property name or the event name | |
* | |
* @return boolean whether the named property is set | |
* @see http://php.net/manual/en/function.isset.php | |
*/ | |
public function __isset($name) | |
{ | |
$getter = 'get' . $name; | |
if (method_exists($this, $getter)) { | |
return $this->$getter() !== null; | |
} | |
return false; | |
} | |
/** | |
* Sets a component property to be null. | |
* This method will check in the following order and act accordingly: | |
* | |
* - a property defined by a setter: set the property value to be null | |
* | |
* Do not call this method directly as it is a PHP magic method that | |
* will be implicitly called when executing `unset($component->property)`. | |
* | |
* @param string $name the property name | |
* | |
* @throws InvalidCallException if the property is read only. | |
* @see http://php.net/manual/en/function.unset.php | |
*/ | |
public function __unset($name) | |
{ | |
$setter = 'set' . $name; | |
if (method_exists($this, $setter)) { | |
$this->$setter(null); | |
return; | |
} | |
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment