Skip to content

Commit

Permalink
Adding ability to use classes as event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Watts committed Jun 15, 2019
1 parent 7aa7869 commit 86f8ccb
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ public static function __strictCall(string $class, string $method, array $additi
* __getMagicProperties
*
* Returns array of magic properties defined by annotation @property
@author Luke Watts <[email protected]>
*
* @since 1.0.0
* @author Luke Watts <[email protected]>
*
* @since 1.0.0
*
* @param string $class
*
* @return array of [name => bit mask]
*/
public static function __getMagicProperties(string $class): array
Expand Down Expand Up @@ -316,13 +315,15 @@ public static function __hasProperty(string $class, string $name)
*/
public function __call(string $name, array $args)
{
$class = get_class($this);
$class = get_class($this);

// calling event handlers
if (self::__isEventProperty($class, $name)) {
if (is_iterable($this->$name)) {
foreach ($this->$name as $handler) {
$handler(...$args);
if ($handler !== null) {
$handler(...$args);
}
}
} elseif ($this->$name !== null) {
throw new \Error("Property $class::$$name must be iterable or null, " . gettype($this->$name) . ' given.');
Expand Down Expand Up @@ -399,7 +400,7 @@ public function &__get(string $name)
*/
public function __set(string $name, $value)
{
$class = get_class($this);
$class = get_class($this);

if (self::__hasProperty($class, $name)) {
$this->$name = $value;
Expand Down

0 comments on commit 86f8ccb

Please sign in to comment.