Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components\Filters;
 13 
 14 /**
 15  * Builds filter condition.
 16  *
 17  * @package     Grido
 18  * @subpackage  Components\Filters
 19  * @author      Petr Bugyík
 20  *
 21  * @property-read callable $callback
 22  * @property-write array $column
 23  * @property-write array $condition
 24  * @property-write array $value
 25  */
 26 class Condition extends \Nette\Object
 27 {
 28     const OPERATOR_OR = 'OR';
 29     const OPERATOR_AND = 'AND';
 30 
 31     /** @var array */
 32     protected $column;
 33 
 34     /** @var array */
 35     protected $condition;
 36 
 37     /** @var array */
 38     protected $value;
 39 
 40     /** @var callable */
 41     protected $callback;
 42 
 43     /**
 44      * @param mixed $column
 45      * @param mixed $condition
 46      * @param mixed $value
 47      */
 48     public function __construct($column, $condition, $value = NULL)
 49     {
 50         $this->setColumn($column);
 51         $this->setCondition($condition);
 52         $this->setValue($value);
 53     }
 54 
 55     /**
 56      * @param mixed $column
 57      * @throws \InvalidArgumentException
 58      * @return Condition
 59      */
 60     public function setColumn($column)
 61     {
 62         if (is_array($column)) {
 63             $count = count($column);
 64 
 65             //check validity
 66             if ($count % 2 === 0) {
 67                 throw new \InvalidArgumentException('Count of column must be odd.');
 68             }
 69 
 70             for ($i = 0; $i < $count; $i++) {
 71                 $item = $column[$i];
 72                 if ($i & 1 && !self::isOperator($item)) {
 73                     $msg = "The even values of column must be 'AND' or 'OR', '$item' given.";
 74                     throw new \InvalidArgumentException($msg);
 75                 }
 76             }
 77         } else {
 78             $column = (array) $column;
 79         }
 80 
 81         $this->column = $column;
 82         return $this;
 83     }
 84 
 85     /**
 86      * @param mixed $condition
 87      * @return Condition
 88      */
 89     public function setCondition($condition)
 90     {
 91         $this->condition = (array) $condition;
 92         return $this;
 93     }
 94 
 95     /**
 96      * @param mixed $value
 97      * @return Condition
 98      */
 99     public function setValue($value)
100     {
101         $this->value = (array) $value;
102         return $this;
103     }
104 
105     /**********************************************************************************************/
106 
107     /**
108      * @return array
109      */
110     public function getColumn()
111     {
112         return $this->column;
113     }
114 
115     /**
116      * @return array
117      */
118     public function getCondition()
119     {
120         return $this->condition;
121     }
122 
123     /**
124      * @return array
125      */
126     public function getValue()
127     {
128         return $this->value;
129     }
130 
131     /**
132      * @return array
133      */
134     public function getValueForColumn()
135     {
136         if (count($this->condition) > 1) {
137             return $this->value;
138         }
139 
140         $values = array();
141         foreach ($this->getColumn() as $column) {
142             if (!self::isOperator($column)) {
143                 foreach ($this->getValue() as $val) {
144                     $values[] = $val;
145                 }
146             }
147         }
148 
149         return $values;
150     }
151 
152     /**
153      * @return array
154      */
155     public function getColumnWithoutOperator()
156     {
157         $columns = array();
158         foreach ($this->column as $column) {
159             if (!self::isOperator($column)) {
160                 $columns[] = $column;
161             }
162         }
163 
164         return $columns;
165     }
166 
167     /**
168      * @return callable
169      */
170     public function getCallback()
171     {
172         return $this->callback;
173     }
174 
175     /**********************************************************************************************/
176 
177     /**
178      * Returns TRUE if $item is Condition:OPERATOR_AND or Condition:OPERATOR_OR else FALSE.
179      * @param string $item
180      * @return bool
181      */
182     public static function isOperator($item)
183     {
184         return in_array(strtoupper($item), array(self::OPERATOR_AND, self::OPERATOR_OR));
185     }
186 
187     /**
188      * @param mixed $column
189      * @param string $condition
190      * @param mixed $value
191      * @return Condition
192      */
193     public static function setup($column, $condition, $value)
194     {
195         return new self($column, $condition, $value);
196     }
197 
198     /**
199      * @return Condition
200      */
201     public static function setupEmpty()
202     {
203         return new self(NULL, '0 = 1');
204     }
205 
206     /**
207      * @param array $condition
208      * @throws \InvalidArgumentException
209      * @return Condition
210      */
211     public static function setupFromArray(array $condition)
212     {
213         if (count($condition) !== 3) {
214             throw new \InvalidArgumentException("Condition array must contain 3 items.");
215         }
216 
217         return new self($condition[0], $condition[1], $condition[2]);
218     }
219 
220     /**
221      * @param callable $callback
222      * @param string $value
223      * @return Condition
224      */
225     public static function setupFromCallback($callback, $value)
226     {
227         $self = new self(NULL, NULL);
228         $self->value = $value;
229         $self->callback = $callback;
230 
231         return $self;
232     }
233 
234     /**********************************************************************************************/
235 
236     /**
237      * @param string $prefix - column prefix
238      * @param string $suffix - column suffix
239      * @param bool $brackets - add brackets when multiple where
240      * @throws \InvalidArgumentException
241      * @return array
242      */
243     public function __toArray($prefix = NULL, $suffix = NULL, $brackets = TRUE)
244     {
245         $condition = array();
246         $addBrackets = $brackets && count($this->column) > 1;
247 
248         if ($addBrackets) {
249             $condition[] = '(';
250         }
251 
252         $i = 0;
253         foreach ($this->column as $column) {
254             if (self::isOperator($column)) {
255                 $operator = strtoupper($column);
256                 $condition[] = " $operator ";
257 
258             } else {
259                 $i = count($this->condition) > 1 ? $i : 0;
260                 $condition[] = "{$prefix}$column{$suffix} {$this->condition[$i]}";
261 
262                 $i++;
263             }
264         }
265 
266         if ($addBrackets) {
267             $condition[] = ')';
268         }
269 
270         return $condition
271             ? array_values(array_merge(array(implode('', $condition)), $this->getValueForColumn()))
272             : $this->condition;
273     }
274 }
275 
Grido@master API documentation generated by ApiGen