/fsn-site-central/library/Yab/Object.php

https://gitlab.com/team_fsn/fsn-php · PHP · 399 lines · 202 code · 186 blank · 11 comment · 22 complexity · 1d6a10f13a4e302049f051dc12acf3be MD5 · raw file

  1. <?php
  2. /**
  3. * Yab Framework
  4. *
  5. * @category Yab
  6. * @package Yab_Object
  7. * @author Yann BELLUZZI
  8. * @copyright (c) 2010 YBellu
  9. * @license http://www.ybellu.com/yab-framework/license.html
  10. * @link http://www.ybellu.com/yab-framework
  11. */
  12. class Yab_Object implements ArrayAccess, Iterator, Countable {
  13. protected $_attributes = array();
  14. public function __construct($mixed = null) {
  15. if($mixed === null)
  16. return $this;
  17. return $this->populate(is_array($mixed) ? $mixed : array($mixed));
  18. }
  19. public function __set($key, $value) {
  20. return $this->set($key, $value);
  21. }
  22. public function __isset($key) {
  23. return $this->has($key);
  24. }
  25. public function __unset($key) {
  26. return $this->rem($key);
  27. }
  28. public function __get($key) {
  29. return $this->get($key);
  30. }
  31. public function shift() {
  32. return array_shift($this->_attributes);
  33. }
  34. public function pop() {
  35. return array_pop($this->_attributes);
  36. }
  37. public function map($callback) {
  38. $this->_attributes = array_map($callback, $this->_attributes);
  39. return $this;
  40. }
  41. public function clear() {
  42. $this->_attributes = array();
  43. return $this;
  44. }
  45. public function feed(array $attributes, $prefix = '') {
  46. return $this->clear()->populate($attributes, $prefix);
  47. }
  48. public function bind(array &$attributes) {
  49. $this->_attributes = &$attributes;
  50. return $this;
  51. }
  52. public function populate(array $attributes, $prefix = '') {
  53. foreach($attributes as $key => $value)
  54. $this->set($prefix.$key, $value);
  55. return $this;
  56. }
  57. public function set($key, $value) {
  58. $key = (string) $key;
  59. $this->_attributes[$key] = $value;
  60. return $this;
  61. }
  62. public function get($key, $filters = null) {
  63. $key = (string) $key;
  64. if(!$this->has($key))
  65. throw new Yab_Exception($key.' attribute has not been set'); //return null;
  66. if($filters === null)
  67. return $this->_attributes[$key];
  68. if($filters instanceof Yab_Filter_Abstract)
  69. return $filters->filter($this->_attributes[$key]);
  70. return Yab_Loader::getInstance('Yab_Filter_Factory')->feed($this->_attributes)->set('filters', $filters)->filter($this->_attributes[$key]);
  71. }
  72. public function expression($expression, array $additionnal_attributes = array()) {
  73. if(array_key_exists($expression, $this->_attributes))
  74. return $this->_attributes[$expression];
  75. preg_match_all('#:([a-zA-Z0-9_]+)([^a-zA-Z0-9_]|$)#i', $expression, $matches1);
  76. foreach($matches1[1] as $match1) {
  77. $filters = array();
  78. if(preg_match('#'.preg_quote(':'.$match1, '#').'\[([^\]]+)\]#i', $expression, $matches2)) {
  79. $filters = preg_split('#[^a-zA-Z0-9_\-]+#', $matches2[1]);
  80. $expression = preg_replace('#'.preg_quote($matches2[0], '#').'#', ':'.$match1, $expression, 1);
  81. }
  82. if(array_key_exists($match1, $this->_attributes)) {
  83. if(count($filters))
  84. $this->_attributes[$match1] = Yab_Loader::getInstance('Yab_Filter_Factory')->feed($this->_attributes + $additionnal_attributes)->set('filters', $filters)->filter($this->_attributes[$match1]);
  85. $expression = str_replace(':'.$match1, $this->_attributes[$match1], $expression);
  86. }
  87. if(array_key_exists($match1, $additionnal_attributes)) {
  88. if(count($filters))
  89. $additionnal_attributes[$match1] = Yab_Loader::getInstance('Yab_Filter_Factory')->feed($this->_attributes + $additionnal_attributes)->set('filters', $filters)->filter($additionnal_attributes[$match1]);
  90. $expression = str_replace(':'.$match1, $additionnal_attributes[$match1], $expression);
  91. }
  92. }
  93. return $expression;
  94. }
  95. public function flash($key, $filters = null) {
  96. $value = $this->get($key, $filters);
  97. $this->rem($key);
  98. return $value;
  99. }
  100. public function inc($key, $step = 1, $max = null) {
  101. $step = (int) $step;
  102. $value = $this->has($key) ? $this->get($key, 'Int') : 0;
  103. return $this->set($key, is_numeric($max) ? min($max, $value + $step) : $value + $step);
  104. }
  105. public function dec($key, $step = 1, $min = null) {
  106. $step = (int) $step;
  107. $value = $this->has($key) ? $this->get($key, 'Int') : 0;
  108. return $this->set($key, is_numeric($min) ? max($min, $value - $step) : $value - $step);
  109. }
  110. public function prepend($key, $value) {
  111. $old_value = $this->get($key);
  112. if(!is_string($old_value) || !is_string($value))
  113. throw new Yab_Exception('can not prepend the value to "'.$key.'" strings are needed');
  114. return $this->set($key, $value.$old_value);
  115. }
  116. public function append($key, $value) {
  117. $old_value = $this->get($key);
  118. if(!is_string($old_value) || !is_string($value))
  119. throw new Yab_Exception('can not append the value to "'.$key.'" strings are needed');
  120. return $this->set($key, $old_value.$value);
  121. }
  122. public function has($key) {
  123. return array_key_exists($key, $this->_attributes);
  124. }
  125. public function rem($key) {
  126. if(!$this->has($key))
  127. throw new Yab_Exception($key.' attribute has not been set');
  128. unset($this->_attributes[$key]);
  129. return $this;
  130. }
  131. public function getAttributes(array $name_attributes = array()) {
  132. if(!count($name_attributes))
  133. return $this->_attributes;
  134. $attributes = array();
  135. foreach($name_attributes as $name_attribute) {
  136. if(!$this->has($name_attribute))
  137. continue;
  138. $attributes[$name_attribute] = $this->get($name_attribute);
  139. }
  140. return $attributes;
  141. }
  142. public function count() {
  143. return count($this->_attributes);
  144. }
  145. public function offsetGet($offset) {
  146. return $this->get($offset);
  147. }
  148. public function offsetSet($offset, $value) {
  149. return $this->set($offset, $value);
  150. }
  151. public function offsetExists($offset) {
  152. return $this->has($offset);
  153. }
  154. public function offsetUnset($offset) {
  155. return $this->rem($offset);
  156. }
  157. public function valid() {
  158. return array_key_exists(key($this->_attributes), $this->_attributes);
  159. }
  160. public function next() {
  161. next($this->_attributes);
  162. return $this->current();
  163. }
  164. public function rewind() {
  165. reset($this->_attributes);
  166. return $this->current();
  167. }
  168. public function key() {
  169. return key($this->_attributes);
  170. }
  171. public function current() {
  172. return current($this->_attributes);
  173. }
  174. public function join($pattern = '') {
  175. return new self(implode($pattern, $this->_attributes));
  176. }
  177. public function split($pattern = '') {
  178. if(!$pattern)
  179. return new self(str_split($this->toString()));
  180. if(strlen($pattern) < 3 || substr($pattern, 0, 1) !== substr($pattern, -1, 1))
  181. $pattern = '#'.preg_quote($pattern, '#').'#';
  182. return new self(preg_split($pattern, $this->toString()));
  183. }
  184. public function slice($start, $offset) {
  185. return new self(array_slice($this->toArray(), $start, $offset));
  186. }
  187. public function cast($key) {
  188. $value = $this->get($key);
  189. if(is_object($value))
  190. return $value;
  191. if(is_array($value)) {
  192. $cast = new self();
  193. return $cast->bind($this->_attributes[$key]);
  194. }
  195. return new self($value);
  196. }
  197. public function toString() {
  198. return implode('', $this->_attributes);
  199. }
  200. public function toArray() {
  201. return $this->_attributes;
  202. }
  203. public function toInt() {
  204. return intval($this->toString());
  205. }
  206. public function toFloat() {
  207. return floatval($this->toString());
  208. }
  209. public function __toString() {
  210. return $this->toString();
  211. }
  212. }
  213. // Do not clause PHP tags unless it is really necessary