KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
filter.php
Ir a la documentación de este archivo.
1 <?php
23 require_once CORE_PATH . 'libs/filter/filter_interface.php';
24 
31 class Filter
32 {
33 
42  public static function get($s, $filter, $options = array())
43  {
44  if (is_string($options)) {
45  $filters = func_get_args();
46  unset($filters[0]);
47 
48  $options = array();
49  foreach ($filters as $f) {
50  $filter_class = Util::camelcase($f) . 'Filter';
51  if (!class_exists($filter_class, false)) {
52  self::_load_filter($f);
53  }
54 
55  $s = call_user_func(array($filter_class, 'execute'), $s, $options);
56  }
57  } else {
58  $filter_class = Util::camelcase($filter) . 'Filter';
59  if (!class_exists($filter_class, false)) {
60  self::_load_filter($filter);
61  }
62  $s = call_user_func(array($filter_class, 'execute'), $s, $options);
63  }
64 
65  return $s;
66  }
67 
76  public static function get_array($array, $filter, $options = array())
77  {
78  $args = func_get_args();
79 
80  foreach ($array as $k => $v) {
81  $args[0] = $v;
82  $array[$k] = call_user_func_array(array('self', 'get'), $args);
83  }
84 
85  return $array;
86  }
87 
143  public static function data(array $data, array $fields, $filterAll = NULL)
144  {
145  $filtered = array(); //datos filtrados a devolver.
146  foreach ($fields as $index => $filters) {
147  if (is_numeric($index) && array_key_exists($filters, $data)) {
148  //si el indice es numerico, no queremos usar filtro para ese campo
149  $filtered[$filters] = $data[$filters];
150  continue;
151  } elseif (array_key_exists($index, $data)) {//verificamos de nuevo la existencia del indice en $data
152  $filters = explode('|',$filters);//convertimos el filtro en arreglo
153  array_unshift($filters, $data[$index]);
154  $filtered[$index] = call_user_func_array(array('self', 'get'), $filters);
155  //$filtered[$index] = self::get($data[$index], $filters); //por ahora sin opciones adicionales.
156  }
157  }
158  if ($filterAll) {
159  $filterAll = explode('|',$filterAll);
160  array_unshift($filterAll, $filtered);
161  return call_user_func_array(array('self', 'get_array'), $filterAll);
162  } else {
163  return $filtered;
164  }
165  }
166 
174  public static function get_object($object, $filter, $options = array())
175  {
176  $args = func_get_args();
177 
178  foreach ($object as $k => $v) {
179  $args[0] = $v;
180  $object->$k = call_user_func_array(array('self', 'get'), $args);
181  }
182 
183  return $object;
184  }
185 
192  protected static function _load_filter($filter)
193  {
194  $file = APP_PATH . "extensions/filters/{$filter}_filter.php";
195  if (!is_file($file)) {
196  $file = CORE_PATH . "libs/filter/base_filter/{$filter}_filter.php";
197  if (!is_file($file)) {
198  throw new KumbiaException("Filtro $filter no encontrado");
199  }
200  }
201 
202  include $file;
203  }
204 
205 }