/vendor/tipsyphp/tipsy/src/Looper.php

https://gitlab.com/tekoestudio/capacitacion-tipsy · PHP · 280 lines · 197 code · 42 blank · 41 comment · 45 complexity · 2d08c2cf0b23d8aa0ed5fea4e79954e1 MD5 · raw file

  1. <?php
  2. namespace Tipsy;
  3. class Looper implements \Iterator, \JsonSerializable {
  4. private $_items;
  5. private $_position;
  6. const DONE = 'looper\break';
  7. public function jsonSerialize() {
  8. foreach ($this->_items as $key => $item) {
  9. if (is_object($item) && method_exists($item, 'exports')) {
  10. $items[$key] = $item->exports();
  11. } else {
  12. $items[$key] = $item;
  13. }
  14. }
  15. return $items;
  16. }
  17. public function __construct() {
  18. $items = [];
  19. $args = func_get_args();
  20. $args = array_reverse($args);
  21. foreach ($args as $arg) {
  22. if (is_object($arg) && (get_class($arg) == 'Tipsy\Looper' || is_subclass_of($arg, 'Tipsy\Looper'))) {
  23. $arg = $arg->items();
  24. } elseif (is_object($arg)) {
  25. $arg = [$arg];
  26. }
  27. $items = array_merge((array)$arg, $items);
  28. }
  29. $this->_items = $items;
  30. $this->_position = 0;
  31. }
  32. // if anyone knows any way to pass func_get_args by reference i would love you. i want string manipulation
  33. public static function o() {
  34. $iterator = new \ReflectionClass(get_called_class());
  35. return $iterator->newInstanceArgs(func_get_args());
  36. }
  37. public function items() {
  38. return $this->_items;
  39. }
  40. public function get($index) {
  41. return $this->eq($index);
  42. }
  43. public function set($var, $val) {
  44. if ($var) {
  45. foreach ($this->_items as $item) {
  46. $item->{$var} = $val;
  47. };
  48. }
  49. return $this;
  50. }
  51. public function eq($pos) {
  52. $pos = $pos < 0 ? count($this->_items) - abs($pos) : $pos;
  53. return $this->_items[$pos];
  54. }
  55. public function remove($start) {
  56. unset($this->_items[$start]);
  57. return $this;
  58. }
  59. public function slice($start, $end = null) {
  60. $items = $this->_items;
  61. $items = array_slice($items, $start, $end);
  62. return $this->_returnItems($items);
  63. }
  64. public function not() {
  65. $items = call_user_func_array([$this, '_filter'], func_get_args());
  66. return $this->_returnItems($items['no']);
  67. }
  68. public function filter() {
  69. $items = call_user_func_array([$this, '_filter'], func_get_args());
  70. return $this->_returnItems($items['yes']);
  71. }
  72. public function each($func, $params = []) {
  73. foreach ($this->_items as $key => $item) {
  74. $func = $func->bindTo(!is_object($item) ? (object)$item : $item);
  75. $res = $func($key, $item);
  76. $this->_items[$key] = $item;
  77. if ($res == self::DONE) {
  78. break;
  79. }
  80. }
  81. }
  82. public function json($args = []) {
  83. return json_encode($this->jsonSerialize());
  84. }
  85. public function e($f) {
  86. self::each($f);
  87. }
  88. public function rewind() {
  89. $this->_position = 0;
  90. }
  91. public function current() {
  92. return $this->_items[$this->_position];
  93. }
  94. public function key() {
  95. return $this->_position;
  96. }
  97. public function next() {
  98. ++$this->_position;
  99. }
  100. public function valid() {
  101. return isset($this->_items[$this->_position]);
  102. }
  103. public function count() {
  104. return count($this->_items);
  105. }
  106. public function parent() {
  107. return $this->_parent;
  108. }
  109. private function _filter() {
  110. $items = $this->_items;
  111. $mismatch = [];
  112. $strict = false;
  113. if (func_num_args() == 1 && is_callable(func_get_arg(0))) {
  114. $func = func_get_arg(0);
  115. } elseif (func_num_args() == 2 && !is_array(func_get_arg(0)) && !is_array(func_get_arg(1))) {
  116. $filters[][func_get_arg(0)] = func_get_arg(1);
  117. } else {
  118. foreach (func_get_args() as $arg) {
  119. if (is_array($arg)) {
  120. $filters[] = $arg;
  121. }
  122. }
  123. }
  124. if ($filters) {
  125. foreach ($filters as $key => $set) {
  126. foreach ($items as $key => $item) {
  127. $mis = 0;
  128. foreach ($set as $k => $v) {
  129. if ($item->{$k} != $v) {
  130. $mis++;
  131. }
  132. }
  133. if (($strict && count($set) == $mis) || $mis) {
  134. $mismatch[$key]++;
  135. }
  136. }
  137. }
  138. }
  139. if ($func) {
  140. foreach ($items as $key => $item) {
  141. if (!$func($item,$key)) {
  142. $mismatch[$key] = $key;
  143. break;
  144. }
  145. }
  146. }
  147. foreach ($items as $key => $value) {
  148. if (array_key_exists($key, $mismatch) && ($func || $mismatch[$key] == count($filters))) {
  149. $trash[] = $items[$key];
  150. } else {
  151. $newitems[] = $items[$key];
  152. }
  153. }
  154. return ['yes' => $newitems,'no' => $trash];
  155. }
  156. private function _returnItems($items) {
  157. if (count($items) != count($this->_items)) {
  158. $return = new self($items);
  159. $return->_parent = $this;
  160. } else {
  161. $return = $this;
  162. }
  163. return $return;
  164. }
  165. public function __toString() {
  166. $print = '';
  167. foreach ($this->_items as $key => $item) {
  168. if (is_object($item) && method_exists($item,'__toString')) {
  169. $print .= $item->__toString();
  170. } elseif (is_string($item) || is_int($item)) {
  171. $print .= $item;
  172. }
  173. }
  174. return $print;
  175. }
  176. public function __call($name, $arguments) {
  177. foreach ($this->_items as $key => $item) {
  178. if (is_callable($item, $name) || method_exists($item, $name)) {
  179. $items[] = (new \ReflectionMethod($item, $name))->invokeArgs($item, $arguments);
  180. } else {
  181. // not callable
  182. }
  183. }
  184. return self::o($items);
  185. }
  186. public function &__get($name) {
  187. /** looper should never need this, but this might break stuff
  188. if (property_exists($this,$name)) {
  189. return $this->{$name};
  190. } else {
  191. if (isset($name{0}) && $name{0} == '_') {
  192. return $this->_items[0]->{$name};
  193. } else {
  194. return $this->_items[0]->_properties[$name];
  195. }
  196. }
  197. */
  198. if (isset($name{0}) && $name{0} == '_') {
  199. return $this->_items[0]->{$name};
  200. } else {
  201. return $this->_items[0]->_properties[$name];
  202. }
  203. }
  204. public function __set($name, $value) {
  205. /** looper should never need this, but this might break stuff
  206. if (property_exists($this,$name)) {
  207. $this->{$name} = $value;
  208. } else {
  209. foreach ($this->_items as $key => $item) {
  210. $this->_items[$key]->{$name} = $value;
  211. }
  212. }
  213. */
  214. foreach ($this->_items as $key => $item) {
  215. $this->_items[$key]->{$name} = $value;
  216. }
  217. return $value;
  218. }
  219. /** these appear to never have been implimented correctly
  220. public function __isset($property) {
  221. if (isset($property{0}) && $property{0} == '_') {
  222. return $this->_items[0]->{$property} ? true : false;
  223. } else {
  224. return $this->_items[0]->_properties[$property] ? true : false;
  225. }
  226. }
  227. public function __unset($property) {
  228. if (isset($property{0}) && $property{0} == '_') {
  229. unset($this->_items[0]->{$property});
  230. } else {
  231. unset($this->_items[0]->_properties[$property]);
  232. }
  233. return $this;
  234. }
  235. **/
  236. }