Getters.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Grav\Common;
  3. /**
  4. * Abstract class to implement magic __get(), __set(), __isset() and __unset().
  5. * Also implements ArrayAccess.
  6. *
  7. * @author RocketTheme
  8. * @license MIT
  9. */
  10. abstract class Getters implements \ArrayAccess, \Countable
  11. {
  12. /**
  13. * Define variable used in getters.
  14. *
  15. * @var string
  16. */
  17. protected $gettersVariable = null;
  18. /**
  19. * Magic setter method
  20. *
  21. * @param mixed $offset Medium name value
  22. * @param mixed $value Medium value
  23. */
  24. public function __set($offset, $value)
  25. {
  26. $this->offsetSet($offset, $value);
  27. }
  28. /**
  29. * Magic getter method
  30. *
  31. * @param mixed $offset Medium name value
  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. * @return boolean True if the value is set
  43. */
  44. public function __isset($offset)
  45. {
  46. return $this->offsetExists($offset);
  47. }
  48. /**
  49. * Magic method to unset the attribute
  50. *
  51. * @param mixed $offset The name value to unset
  52. */
  53. public function __unset($offset)
  54. {
  55. $this->offsetUnset($offset);
  56. }
  57. /**
  58. * @param mixed $offset
  59. * @return bool
  60. */
  61. public function offsetExists($offset)
  62. {
  63. if ($this->gettersVariable) {
  64. $var = $this->gettersVariable;
  65. return isset($this->{$var}[$offset]);
  66. } else {
  67. return isset($this->{$offset});
  68. }
  69. }
  70. /**
  71. * @param mixed $offset
  72. * @return mixed
  73. */
  74. public function offsetGet($offset)
  75. {
  76. if ($this->gettersVariable) {
  77. $var = $this->gettersVariable;
  78. return isset($this->{$var}[$offset]) ? $this->{$var}[$offset] : null;
  79. } else {
  80. return isset($this->{$offset}) ? $this->{$offset} : null;
  81. }
  82. }
  83. /**
  84. * @param mixed $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 mixed $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. count($this->{$var});
  116. } else {
  117. count($this->toArray());
  118. }
  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. } else {
  131. $properties = (array) $this;
  132. $list = array();
  133. foreach ($properties as $property => $value) {
  134. if ($property[0] != "\0") $list[$property] = $value;
  135. }
  136. return $list;
  137. }
  138. }
  139. }