Getters.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 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. } else {
  69. return isset($this->{$offset});
  70. }
  71. }
  72. /**
  73. * @param mixed $offset
  74. *
  75. * @return mixed
  76. */
  77. public function offsetGet($offset)
  78. {
  79. if ($this->gettersVariable) {
  80. $var = $this->gettersVariable;
  81. return isset($this->{$var}[$offset]) ? $this->{$var}[$offset] : null;
  82. } else {
  83. return isset($this->{$offset}) ? $this->{$offset} : null;
  84. }
  85. }
  86. /**
  87. * @param mixed $offset
  88. * @param mixed $value
  89. */
  90. public function offsetSet($offset, $value)
  91. {
  92. if ($this->gettersVariable) {
  93. $var = $this->gettersVariable;
  94. $this->{$var}[$offset] = $value;
  95. } else {
  96. $this->{$offset} = $value;
  97. }
  98. }
  99. /**
  100. * @param mixed $offset
  101. */
  102. public function offsetUnset($offset)
  103. {
  104. if ($this->gettersVariable) {
  105. $var = $this->gettersVariable;
  106. unset($this->{$var}[$offset]);
  107. } else {
  108. unset($this->{$offset});
  109. }
  110. }
  111. /**
  112. * @return int
  113. */
  114. public function count()
  115. {
  116. if ($this->gettersVariable) {
  117. $var = $this->gettersVariable;
  118. count($this->{$var});
  119. } else {
  120. count($this->toArray());
  121. }
  122. }
  123. /**
  124. * Returns an associative array of object properties.
  125. *
  126. * @return array
  127. */
  128. public function toArray()
  129. {
  130. if ($this->gettersVariable) {
  131. $var = $this->gettersVariable;
  132. return $this->{$var};
  133. } else {
  134. $properties = (array)$this;
  135. $list = [];
  136. foreach ($properties as $property => $value) {
  137. if ($property[0] != "\0") {
  138. $list[$property] = $value;
  139. }
  140. }
  141. return $list;
  142. }
  143. }
  144. }