SmartObject.php 2.8 KB

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