magic.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //! PHP magic wrapper
  16. abstract class Magic implements ArrayAccess {
  17. /**
  18. * Return TRUE if key is not empty
  19. * @return bool
  20. * @param $key string
  21. **/
  22. abstract function exists($key);
  23. /**
  24. * Bind value to key
  25. * @return mixed
  26. * @param $key string
  27. * @param $val mixed
  28. **/
  29. abstract function set($key,$val);
  30. /**
  31. * Retrieve contents of key
  32. * @return mixed
  33. * @param $key string
  34. **/
  35. abstract function &get($key);
  36. /**
  37. * Unset key
  38. * @return NULL
  39. * @param $key string
  40. **/
  41. abstract function clear($key);
  42. /**
  43. * Convenience method for checking property value
  44. * @return mixed
  45. * @param $key string
  46. **/
  47. function offsetexists($key) {
  48. return Base::instance()->visible($this,$key)?
  49. isset($this->$key):
  50. ($this->exists($key) && $this->get($key)!==NULL);
  51. }
  52. /**
  53. * Convenience method for assigning property value
  54. * @return mixed
  55. * @param $key string
  56. * @param $val mixed
  57. **/
  58. function offsetset($key,$val) {
  59. return Base::instance()->visible($this,$key)?
  60. ($this->$key=$val):$this->set($key,$val);
  61. }
  62. /**
  63. * Convenience method for retrieving property value
  64. * @return mixed
  65. * @param $key string
  66. **/
  67. function &offsetget($key) {
  68. if (Base::instance()->visible($this,$key))
  69. $val=&$this->$key;
  70. else
  71. $val=&$this->get($key);
  72. return $val;
  73. }
  74. /**
  75. * Convenience method for removing property value
  76. * @return NULL
  77. * @param $key string
  78. **/
  79. function offsetunset($key) {
  80. if (Base::instance()->visible($this,$key))
  81. unset($this->$key);
  82. else
  83. $this->clear($key);
  84. }
  85. /**
  86. * Alias for offsetexists()
  87. * @return mixed
  88. * @param $key string
  89. **/
  90. function __isset($key) {
  91. return $this->offsetexists($key);
  92. }
  93. /**
  94. * Alias for offsetset()
  95. * @return mixed
  96. * @param $key string
  97. * @param $val mixed
  98. **/
  99. function __set($key,$val) {
  100. return $this->offsetset($key,$val);
  101. }
  102. /**
  103. * Alias for offsetget()
  104. * @return mixed
  105. * @param $key string
  106. **/
  107. function &__get($key) {
  108. $val=&$this->offsetget($key);
  109. return $val;
  110. }
  111. /**
  112. * Alias for offsetunset()
  113. * @param $key string
  114. **/
  115. function __unset($key) {
  116. $this->offsetunset($key);
  117. }
  118. }