1 | <?php |
||
36 | class Translator extends BaseObject |
||
37 | { |
||
38 | private $_where; |
||
39 | private $_params = []; |
||
40 | private $_operators; |
||
41 | |||
42 | /** |
||
43 | * Constructors. |
||
44 | * @param array $data Rules configuraion |
||
45 | * @param array $config the configuration array to be applied to this object. |
||
46 | */ |
||
47 | 1 | public function __construct($data, $config = []) |
|
52 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | 1 | public function init() |
|
57 | { |
||
58 | 1 | $this->_operators = [ |
|
59 | 1 | 'equal' => '= ?', |
|
60 | 1 | 'not_equal' => '<> ?', |
|
61 | 'in' => ['op' => 'IN (?)', 'list' => true, 'sep' => ', ' ], |
||
62 | 'not_in' => ['op' => 'NOT IN (?)', 'list' => true, 'sep' => ', '], |
||
63 | 1 | 'less' => '< ?', |
|
64 | 1 | 'less_or_equal' => '<= ?', |
|
65 | 1 | 'greater' => '> ?', |
|
66 | 1 | 'greater_or_equal' => '>= ?', |
|
67 | 'between' => ['op' => 'BETWEEN ?', 'list' => true, 'sep' => ' AND '], |
||
68 | 'begins_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "$value%"; } ], |
||
69 | 'not_begins_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "$value%"; } ], |
||
70 | 'contains' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value%"; } ], |
||
71 | 'not_contains' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value%"; } ], |
||
72 | 'ends_with' => ['op' => 'LIKE ?', 'fn' => function($value){ return "%$value"; } ], |
||
73 | 'not_ends_with' => ['op' => 'NOT LIKE ?', 'fn' => function($value){ return "%$value"; } ], |
||
74 | 1 | 'is_empty' => '= ""', |
|
75 | 1 | 'is_not_empty' => '<> ""', |
|
76 | 1 | 'is_null' => 'IS NULL', |
|
77 | 1 | 'is_not_null' => 'IS NOT NULL' |
|
78 | ]; |
||
79 | 1 | } |
|
80 | |||
81 | |||
82 | /** |
||
83 | * Encodes filter rule into SQL condition |
||
84 | * @param string $field field name |
||
85 | * @param string|array $type operator type |
||
86 | * @param string|array $params query parameters |
||
87 | * @return string encoded rule |
||
88 | */ |
||
89 | 1 | protected function encodeRule($field, $type, $params) |
|
113 | |||
114 | /** |
||
115 | * @param array $data rules configuration |
||
116 | * @return string the WHERE clause |
||
117 | */ |
||
118 | 1 | protected function buildWhere($data) |
|
153 | |||
154 | /** |
||
155 | * Returns query WHERE condition. |
||
156 | * @return string |
||
157 | */ |
||
158 | 1 | public function where() |
|
162 | |||
163 | /** |
||
164 | * Returns the parameters to be bound to the query. |
||
165 | * @return array |
||
166 | */ |
||
167 | 1 | public function params() |
|
171 | } |