SmartObject.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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') . ucfirst($name);
  56. if ($prop & 0b10000) {
  57. $trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
  58. $loc = isset($trace['file'], $trace['line'])
  59. ? " in $trace[file] on line $trace[line]"
  60. : '';
  61. trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
  62. }
  63. if ($prop & 0b0100) { // return by reference
  64. return $this->$m();
  65. } else {
  66. $val = $this->$m();
  67. return $val;
  68. }
  69. } else {
  70. ObjectHelpers::strictGet($class, $name);
  71. }
  72. }
  73. /**
  74. * @param mixed $value
  75. * @return void
  76. * @throws MemberAccessException if the property is not defined or is read-only
  77. */
  78. public function __set(string $name, $value)
  79. {
  80. $class = static::class;
  81. if (ObjectHelpers::hasProperty($class, $name)) { // unsetted property
  82. $this->$name = $value;
  83. } elseif ($prop = ObjectHelpers::getMagicProperties($class)[$name] ?? null) { // property setter
  84. if (!($prop & 0b1000)) {
  85. throw new MemberAccessException("Cannot write to a read-only property $class::\$$name.");
  86. }
  87. $m = 'set' . ucfirst($name);
  88. if ($prop & 0b10000) {
  89. $trace = debug_backtrace(0, 1)[0]; // suppose this method is called from __call()
  90. $loc = isset($trace['file'], $trace['line'])
  91. ? " in $trace[file] on line $trace[line]"
  92. : '';
  93. trigger_error("Property $class::\$$name is deprecated, use $class::$m() method$loc.", E_USER_DEPRECATED);
  94. }
  95. $this->$m($value);
  96. } else {
  97. ObjectHelpers::strictSet($class, $name);
  98. }
  99. }
  100. /**
  101. * @return void
  102. * @throws MemberAccessException
  103. */
  104. public function __unset(string $name)
  105. {
  106. $class = static::class;
  107. if (!ObjectHelpers::hasProperty($class, $name)) {
  108. throw new MemberAccessException("Cannot unset the property $class::\$$name.");
  109. }
  110. }
  111. public function __isset(string $name): bool
  112. {
  113. return isset(ObjectHelpers::getMagicProperties(static::class)[$name]);
  114. }
  115. }