Getters.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 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. public function __set($offset, $value)
  27. {
  28. $this->offsetSet($offset, $value);
  29. }
  30. /**
  31. * Magic getter method
  32. *
  33. * @param int|string $offset Medium name value
  34. * @return mixed Medium value
  35. */
  36. public function __get($offset)
  37. {
  38. return $this->offsetGet($offset);
  39. }
  40. /**
  41. * Magic method to determine if the attribute is set
  42. *
  43. * @param int|string $offset Medium name value
  44. * @return boolean True if the value is set
  45. */
  46. public function __isset($offset)
  47. {
  48. return $this->offsetExists($offset);
  49. }
  50. /**
  51. * Magic method to unset the attribute
  52. *
  53. * @param int|string $offset The name value to unset
  54. */
  55. public function __unset($offset)
  56. {
  57. $this->offsetUnset($offset);
  58. }
  59. /**
  60. * @param int|string $offset
  61. * @return bool
  62. */
  63. public function offsetExists($offset)
  64. {
  65. if ($this->gettersVariable) {
  66. $var = $this->gettersVariable;
  67. return isset($this->{$var}[$offset]);
  68. }
  69. return isset($this->{$offset});
  70. }
  71. /**
  72. * @param int|string $offset
  73. * @return mixed
  74. */
  75. public function offsetGet($offset)
  76. {
  77. if ($this->gettersVariable) {
  78. $var = $this->gettersVariable;
  79. return $this->{$var}[$offset] ?? null;
  80. }
  81. return $this->{$offset} ?? null;
  82. }
  83. /**
  84. * @param int|string $offset
  85. * @param mixed $value
  86. */
  87. public function offsetSet($offset, $value)
  88. {
  89. if ($this->gettersVariable) {
  90. $var = $this->gettersVariable;
  91. $this->{$var}[$offset] = $value;
  92. } else {
  93. $this->{$offset} = $value;
  94. }
  95. }
  96. /**
  97. * @param int|string $offset
  98. */
  99. public function offsetUnset($offset)
  100. {
  101. if ($this->gettersVariable) {
  102. $var = $this->gettersVariable;
  103. unset($this->{$var}[$offset]);
  104. } else {
  105. unset($this->{$offset});
  106. }
  107. }
  108. /**
  109. * @return int
  110. */
  111. public function count()
  112. {
  113. if ($this->gettersVariable) {
  114. $var = $this->gettersVariable;
  115. return count($this->{$var});
  116. }
  117. return count($this->toArray());
  118. }
  119. /**
  120. * Returns an associative array of object properties.
  121. *
  122. * @return array
  123. */
  124. public function toArray()
  125. {
  126. if ($this->gettersVariable) {
  127. $var = $this->gettersVariable;
  128. return $this->{$var};
  129. }
  130. $properties = (array)$this;
  131. $list = [];
  132. foreach ($properties as $property => $value) {
  133. if ($property[0] !== "\0") {
  134. $list[$property] = $value;
  135. }
  136. }
  137. return $list;
  138. }
  139. }