Просмотр файла news/libraries/magic.php

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