magic.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. //! PHP magic wrapper
  12. abstract class Magic implements ArrayAccess {
  13. /**
  14. * Return TRUE if key is not empty
  15. * @return bool
  16. * @param $key string
  17. **/
  18. abstract function exists($key);
  19. /**
  20. * Bind value to key
  21. * @return mixed
  22. * @param $key string
  23. * @param $val mixed
  24. **/
  25. abstract function set($key,$val);
  26. /**
  27. * Retrieve contents of key
  28. * @return mixed
  29. * @param $key string
  30. **/
  31. abstract function get($key);
  32. /**
  33. * Unset key
  34. * @return NULL
  35. * @param $key string
  36. **/
  37. abstract function clear($key);
  38. /**
  39. * Return TRUE if property has public/protected visibility
  40. * @return bool
  41. * @param $key string
  42. **/
  43. private function visible($key) {
  44. if (property_exists($this,$key)) {
  45. $ref=new ReflectionProperty(get_class($this),$key);
  46. $out=!$ref->isprivate();
  47. unset($ref);
  48. return $out;
  49. }
  50. return FALSE;
  51. }
  52. /**
  53. * Convenience method for checking property value
  54. * @return mixed
  55. * @param $key string
  56. **/
  57. function offsetexists($key) {
  58. return $this->visible($key)?isset($this->$key):$this->exists($key);
  59. }
  60. /**
  61. * Alias for offsetexists()
  62. * @return mixed
  63. * @param $key string
  64. **/
  65. function __isset($key) {
  66. return $this->offsetexists($key);
  67. }
  68. /**
  69. * Convenience method for assigning property value
  70. * @return mixed
  71. * @param $key string
  72. * @param $val scalar
  73. **/
  74. function offsetset($key,$val) {
  75. return $this->visible($key)?($this->key=$val):$this->set($key,$val);
  76. }
  77. /**
  78. * Alias for offsetset()
  79. * @return mixed
  80. * @param $key string
  81. * @param $val scalar
  82. **/
  83. function __set($key,$val) {
  84. return $this->offsetset($key,$val);
  85. }
  86. /**
  87. * Convenience method for retrieving property value
  88. * @return mixed
  89. * @param $key string
  90. **/
  91. function offsetget($key) {
  92. return $this->visible($key)?$this->$key:$this->get($key);
  93. }
  94. /**
  95. * Alias for offsetget()
  96. * @return mixed
  97. * @param $key string
  98. **/
  99. function __get($key) {
  100. return $this->offsetget($key);
  101. }
  102. /**
  103. * Convenience method for checking property value
  104. * @return NULL
  105. * @param $key string
  106. **/
  107. function offsetunset($key) {
  108. if ($this->visible($key))
  109. unset($this->$key);
  110. else
  111. $this->clear($key);
  112. }
  113. /**
  114. * Alias for offsetunset()
  115. * @return NULL
  116. * @param $key string
  117. **/
  118. function __unset($key) {
  119. $this->offsetunset($key);
  120. }
  121. }