| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace Nette; |
| 11 |
|
| 12 |
use Nette\Utils\ObjectHelpers; |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Strict class for better experience. |
| 17 |
* - 'did you mean' hints |
| 18 |
* - access to undeclared members throws exceptions |
| 19 |
* - support for @property annotations |
| 20 |
* - support for calling event handlers stored in $onEvent via onEvent() |
| 21 |
*/ |
| 22 |
trait SmartObject |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @throws MemberAccessException |
| 26 |
*/ |
| 27 |
public function __call(string $name, array $args) |
| 28 |
{ |
| 29 |
$class = static::class; |
| 30 |
|
| 31 |
if (ObjectHelpers::hasProperty($class, $name) === 'event') { // calling event handlers |
| 32 |
$handlers = $this->$name ?? null; |
| 33 |
if (is_iterable($handlers)) { |
| 34 |
foreach ($handlers as $handler) { |
| 35 |
$handler(...$args); |
| 36 |
} |
| 37 |
} elseif ($handlers !== null) { |
| 38 |
throw new UnexpectedValueException("Property $class::$$name must be iterable or null, " . gettype($handlers) . ' given.'); |
| 39 |
} |
| 40 |
} else { |
| 41 |
ObjectHelpers::strictCall($class, $name); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* @throws MemberAccessException |
| 48 |
*/ |
| 49 |
public static function __callStatic(string $name, array $args) |
| 50 |
{ |
| 51 |
ObjectHelpers::strictStaticCall(static::class, $name); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* @return mixed |
| 57 |
* @throws MemberAccessException if the property is not defined. |
| 58 |
*/ |
| 59 |
public function &__get(string $name) |
| 60 |
{ |
| 61 |
$class = static::class; |
| 62 |
|
| 63 |
if ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) { // property getter |
| 64 |
if (!($prop & 0b0001)) { |
| 65 |
throw new MemberAccessException("Cannot read a write-only property $class::\$$name."); |
| 66 |
} |
| 67 |
|
| 68 |
$m = ($prop & 0b0010 ? 'get' : 'is') . ucfirst($name); |
| 69 |
if ($prop & 0b10000) { |
| 70 |
$trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call() |
| 71 |
$loc = isset($trace['file'], $trace['line']) |
| 72 |
? " in $trace[file] on line $trace[line]" |
| 73 |
: ''; |
| 74 |
trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED); |
| 75 |
} |
| 76 |
|
| 77 |
if ($prop & 0b0100) { // return by reference |
| 78 |
return $this->$m(); |
| 79 |
} else { |
| 80 |
$val = $this->$m(); |
| 81 |
return $val; |
| 82 |
} |
| 83 |
} else { |
| 84 |
ObjectHelpers::strictGet($class, $name); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @param mixed $value |
| 91 |
* @return void |
| 92 |
* @throws MemberAccessException if the property is not defined or is read-only |
| 93 |
*/ |
| 94 |
public function __set(string $name, $value) |
| 95 |
{ |
| 96 |
$class = static::class; |
| 97 |
|
| 98 |
if (ObjectHelpers::hasProperty($class, $name)) { // unsetted property |
| 99 |
$this->$name = $value; |
| 100 |
|
| 101 |
} elseif ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) { // property setter |
| 102 |
if (!($prop & 0b1000)) { |
| 103 |
throw new MemberAccessException("Cannot write to a read-only property $class::\$$name."); |
| 104 |
} |
| 105 |
|
| 106 |
$m = 'set' . ucfirst($name); |
| 107 |
if ($prop & 0b10000) { |
| 108 |
$trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call() |
| 109 |
$loc = isset($trace['file'], $trace['line']) |
| 110 |
? " in $trace[file] on line $trace[line]" |
| 111 |
: ''; |
| 112 |
trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED); |
| 113 |
} |
| 114 |
|
| 115 |
$this->$m($value); |
| 116 |
|
| 117 |
} else { |
| 118 |
ObjectHelpers::strictSet($class, $name); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
/** |
| 124 |
* @return void |
| 125 |
* @throws MemberAccessException |
| 126 |
*/ |
| 127 |
public function __unset(string $name) |
| 128 |
{ |
| 129 |
$class = static::class; |
| 130 |
if (!ObjectHelpers::hasProperty($class, $name)) { |
| 131 |
throw new MemberAccessException("Cannot unset the property $class::\$$name."); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
public function __isset(string $name): bool |
| 137 |
{ |
| 138 |
return isset(ObjectHelpers::getMagicProperties(static::class)[$name]); |
| 139 |
} |
| 140 |
} |
| 141 |
|