Getters.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use ArrayAccess;
  10. use Countable;
  11. use function count;
  12. /**
  13. * Class Getters
  14. * @package Grav\Common
  15. */
  16. abstract class Getters implements ArrayAccess, Countable
  17. {
  18. /** @var string Define variable used in getters. */
  19. protected $gettersVariable = null;
  20. /**
  21. * Magic setter method
  22. *
  23. * @param int|string $offset Medium name value
  24. * @param mixed $value Medium value
  25. */
  26. #[\ReturnTypeWillChange]
  27. public function __set($offset, $value)
  28. {
  29. $this->offsetSet($offset, $value);
  30. }
  31. /**
  32. * Magic getter method
  33. *
  34. * @param int|string $offset Medium name value
  35. * @return mixed Medium value
  36. */
  37. #[\ReturnTypeWillChange]
  38. public function __get($offset)
  39. {
  40. return $this->offsetGet($offset);
  41. }
  42. /**
  43. * Magic method to determine if the attribute is set
  44. *
  45. * @param int|string $offset Medium name value
  46. * @return boolean True if the value is set
  47. */
  48. #[\ReturnTypeWillChange]
  49. public function __isset($offset)
  50. {
  51. return $this->offsetExists($offset);
  52. }
  53. /**
  54. * Magic method to unset the attribute
  55. *
  56. * @param int|string $offset The name value to unset
  57. */
  58. #[\ReturnTypeWillChange]
  59. public function __unset($offset)
  60. {
  61. $this->offsetUnset($offset);
  62. }
  63. /**
  64. * @param int|string $offset
  65. * @return bool
  66. */
  67. #[\ReturnTypeWillChange]
  68. public function offsetExists($offset)
  69. {
  70. if ($this->gettersVariable) {
  71. $var = $this->gettersVariable;
  72. return isset($this->{$var}[$offset]);
  73. }
  74. return isset($this->{$offset});
  75. }
  76. /**
  77. * @param int|string $offset
  78. * @return mixed
  79. */
  80. #[\ReturnTypeWillChange]
  81. public function offsetGet($offset)
  82. {
  83. if ($this->gettersVariable) {
  84. $var = $this->gettersVariable;
  85. return $this->{$var}[$offset] ?? null;
  86. }
  87. return $this->{$offset} ?? null;
  88. }
  89. /**
  90. * @param int|string $offset
  91. * @param mixed $value
  92. */
  93. #[\ReturnTypeWillChange]
  94. public function offsetSet($offset, $value)
  95. {
  96. if ($this->gettersVariable) {
  97. $var = $this->gettersVariable;
  98. $this->{$var}[$offset] = $value;
  99. } else {
  100. $this->{$offset} = $value;
  101. }
  102. }
  103. /**
  104. * @param int|string $offset
  105. */
  106. #[\ReturnTypeWillChange]
  107. public function offsetUnset($offset)
  108. {
  109. if ($this->gettersVariable) {
  110. $var = $this->gettersVariable;
  111. unset($this->{$var}[$offset]);
  112. } else {
  113. unset($this->{$offset});
  114. }
  115. }
  116. /**
  117. * @return int
  118. */
  119. #[\ReturnTypeWillChange]
  120. public function count()
  121. {
  122. if ($this->gettersVariable) {
  123. $var = $this->gettersVariable;
  124. return count($this->{$var});
  125. }
  126. return count($this->toArray());
  127. }
  128. /**
  129. * Returns an associative array of object properties.
  130. *
  131. * @return array
  132. */
  133. public function toArray()
  134. {
  135. if ($this->gettersVariable) {
  136. $var = $this->gettersVariable;
  137. return $this->{$var};
  138. }
  139. $properties = (array)$this;
  140. $list = [];
  141. foreach ($properties as $property => $value) {
  142. if ($property[0] !== "\0") {
  143. $list[$property] = $value;
  144. }
  145. }
  146. return $list;
  147. }
  148. }