PluginProbe
Packeta / 1.6.4
Packeta v1.6.4
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / utils / src / SmartObject.php

SmartObject.php in Packeta 1.6.4, at deps/nette/utils/src/SmartObject.php

108 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 declare (strict_types=1);
8 namespace Packetery\Nette;
9
10 use Packetery\Nette\Utils\ObjectHelpers;
11 /**
12 * Strict class for better experience.
13 * - 'did you mean' hints
14 * - access to undeclared members throws exceptions
15 * - support for @property annotations
16 * - support for calling event handlers stored in $onEvent via onEvent()
17 */
18 trait SmartObject
19 {
20 /**
21 * @throws MemberAccessException
22 */
23 public function __call(string $name, array $args)
24 {
25 $class = static::class;
26 if (ObjectHelpers::hasProperty($class, $name) === 'event') {
27 // calling event handlers
28 $handlers = $this->{$name} ?? null;
29 if (\is_iterable($handlers)) {
30 foreach ($handlers as $handler) {
31 $handler(...$args);
32 }
33 } elseif ($handlers !== null) {
34 throw new UnexpectedValueException("Property {$class}::\${$name} must be iterable or null, " . \gettype($handlers) . ' given.');
35 }
36 } else {
37 ObjectHelpers::strictCall($class, $name);
38 }
39 }
40 /**
41 * @throws MemberAccessException
42 */
43 public static function __callStatic(string $name, array $args)
44 {
45 ObjectHelpers::strictStaticCall(static::class, $name);
46 }
47 /**
48 * @return mixed
49 * @throws MemberAccessException if the property is not defined.
50 */
51 public function &__get(string $name)
52 {
53 $class = static::class;
54 if ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) {
55 // property getter
56 if (!($prop & 0b1)) {
57 throw new MemberAccessException("Cannot read a write-only property {$class}::\${$name}.");
58 }
59 $m = ($prop & 0b10 ? 'get' : 'is') . $name;
60 if ($prop & 0b100) {
61 // return by reference
62 return $this->{$m}();
63 } else {
64 $val = $this->{$m}();
65 return $val;
66 }
67 } else {
68 ObjectHelpers::strictGet($class, $name);
69 }
70 }
71 /**
72 * @param mixed $value
73 * @return void
74 * @throws MemberAccessException if the property is not defined or is read-only
75 */
76 public function __set(string $name, $value)
77 {
78 $class = static::class;
79 if (ObjectHelpers::hasProperty($class, $name)) {
80 // unsetted property
81 $this->{$name} = $value;
82 } elseif ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) {
83 // property setter
84 if (!($prop & 0b1000)) {
85 throw new MemberAccessException("Cannot write to a read-only property {$class}::\${$name}.");
86 }
87 $this->{'set' . $name}($value);
88 } else {
89 ObjectHelpers::strictSet($class, $name);
90 }
91 }
92 /**
93 * @return void
94 * @throws MemberAccessException
95 */
96 public function __unset(string $name)
97 {
98 $class = static::class;
99 if (!ObjectHelpers::hasProperty($class, $name)) {
100 throw new MemberAccessException("Cannot unset the property {$class}::\${$name}.");
101 }
102 }
103 public function __isset(string $name) : bool
104 {
105 return isset(ObjectHelpers::getMagicProperties(static::class)[$name]);
106 }
107 }
108