magic.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 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):$this->exists($key);
  50. }
  51. /**
  52. * Convenience method for assigning property value
  53. * @return mixed
  54. * @param $key string
  55. * @param $val scalar
  56. **/
  57. function offsetset($key,$val) {
  58. return Base::instance()->visible($this,$key)?
  59. ($this->$key=$val):$this->set($key,$val);
  60. }
  61. /**
  62. * Convenience method for retrieving property value
  63. * @return mixed
  64. * @param $key string
  65. **/
  66. function &offsetget($key) {
  67. if (Base::instance()->visible($this,$key))
  68. $val=&$this->$key;
  69. else
  70. $val=&$this->get($key);
  71. return $val;
  72. }
  73. /**
  74. * Convenience method for removing property value
  75. * @return NULL
  76. * @param $key string
  77. **/
  78. function offsetunset($key) {
  79. if (Base::instance()->visible($this,$key))
  80. unset($this->$key);
  81. else
  82. $this->clear($key);
  83. }
  84. /**
  85. * Alias for offsetexists()
  86. * @return mixed
  87. * @param $key string
  88. **/
  89. function __isset($key) {
  90. return $this->offsetexists($key);
  91. }
  92. /**
  93. * Alias for offsetset()
  94. * @return mixed
  95. * @param $key string
  96. * @param $val scalar
  97. **/
  98. function __set($key,$val) {
  99. return $this->offsetset($key,$val);
  100. }
  101. /**
  102. * Alias for offsetget()
  103. * @return mixed
  104. * @param $key string
  105. **/
  106. function &__get($key) {
  107. $val=&$this->offsetget($key);
  108. return $val;
  109. }
  110. /**
  111. * Alias for offsetunset()
  112. * @return NULL
  113. * @param $key string
  114. **/
  115. function __unset($key) {
  116. $this->offsetunset($key);
  117. }
  118. }