Getters.php 3.3 KB

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