TableSearch::order()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Soluble\Normalist\Synthetic;
3
4
use Soluble\Normalist\Synthetic\ResultSet\ResultSet; use Soluble\Db\Sql\Select;
5
use Zend\Db\Sql\Sql;
6
use Zend\Db\Sql\Where;
7
use Zend\Db\Sql\Predicate;
8
9
class TableSearch
10
{
11
    /**
12
     * @var Table
13
     */
14
    protected $table;
15
16
    /**
17
     *
18
     * @var Select
19
     */
20
    protected $select;
21
22
    /**
23
     * @var boolean
24
     */
25
    protected $has_modified_columns = false;
26
27
    /**
28
     *
29
     * @var array|string
30
     */
31
    protected $tableIdentifier;
32
33
    /**
34
     *
35
     * @param Select $select table name
36
     * @param Table $table
37
     */
38 26
    public function __construct(Select $select, Table $table)
39
    {
40 26
        $this->select = $select;
41 26
        $this->table = $table;
42 26
        $this->tableIdentifier = $this->select->getRawState(Select::TABLE);
43 26
    }
44
45
    /**
46
     * Limit the number of results
47
     *
48
     * @param int $limit
49
     * @return TableSearch
50
     */
51 14
    public function limit($limit)
52
    {
53 14
        $this->select->limit($limit);
54 14
        return $this;
55
    }
56
57
    /**
58
     * Set offset to use when a limit has been set.
59
     *
60
     * @param int $offset
61
     * @return TableSearch
62
     */
63 1
    public function offset($offset)
64
    {
65 1
        $this->select->offset($offset);
66 1
        return $this;
67
    }
68
69
70
    /**
71
     * Set the table columns to retrieve
72
     *
73
     * @param array $columns array list of columns, key are used as aliases
74
     * @param boolean $prefixColumnsWithTable
75
     * @return TableSearch
76
     */
77 4
    public function columns(array $columns, $prefixColumnsWithTable = true)
78
    {
79 4
        $this->has_modified_columns = true;
80 4
        $this->select->columns($columns, $prefixColumnsWithTable);
81 4
        return $this;
82
    }
83
84
    /**
85
     * Add table prefixed columns, will automatically
86
     * quote table parts identifiers found in the column name.
87
     * It provides an alternative for defining columns from multiple/joined
88
     * table in one go.
89
     *
90
     * @param array $columns
91
     */
92 3
    public function prefixedColumns(array $columns)
93
    {
94 3
        $this->has_modified_columns = true;
95 3
        $this->select->prefixedColumns($columns);
96 3
        return $this;
97
    }
98
99
    /**
100
     * Set a group option
101
     *
102
     * @param array|string $group a column or an array of columns to group by
103
     * @return TableSearch
104
     */
105 1
    public function group($group)
106
    {
107 1
        $this->select->group($group);
108 1
        return $this;
109
    }
110
111
    /**
112
     * Set a habing option
113
     *
114
     * @param  Where|\Closure|string|array $predicate
115
     * @param  string $combination One of the OP_* constants from Predicate\PredicateSet
116
     * @return Select
117
118
     * @return TableSearch
119
     */
120 1
    public function having($predicate, $combination = Predicate\PredicateSet::OP_AND)
121
    {
122 1
        $this->select->having($predicate, $combination);
123 1
        return $this;
124
    }
125
126
127
    /**
128
     * Set an order by clause
129
     *
130
     * @param string|array $order a columns or an array definition of columns
131
     * @return TableSearch
132
     */
133 5
    public function order($order)
134
    {
135 5
        $this->select->order($order);
136 5
        return $this;
137
    }
138
139
    /**
140
     * Add a where condition
141
     *
142
     * @param  Where|\Closure|string|array|Predicate\PredicateInterface $predicate
143
     * @param  string $combination One of the OP_* constants from Zend\Db\Sql\Predicate\PredicateSet
144
     * @throws Zend\Db\Sql\Exception\InvalidArgumentException
145
     * @return TableSearch
146
     */
147 9
    public function where($predicate, $combination = null)
148
    {
149 9
        $this->select->where($predicate, $combination);
150 9
        return $this;
151
    }
152
153
    /**
154
     * Add an orWhere condition to the search
155
     *
156
     * @param  Where|\Closure|string|array|Predicate\PredicateInterface $predicate
157
     * @throws Zend\Db\Sql\Exception\InvalidArgumentException
158
     * @return TableSearch
159
     */
160 1
    public function orWhere($predicate)
161
    {
162 1
        $this->select->orWhere($predicate);
163 1
        return $this;
164
    }
165
166
167
    /**
168
     * Add an inner table join to the search
169
     *
170
     * @param  string|array $table
171
     * @param  string $on
172
     * @param  string|array $columns by default won't retrieve any column from the joined table
173
     * @return TableSearch
174
     */
175 1
    public function join($table, $on, $columns = [])
176
    {
177 1
        $prefixed_table = $this->prefixTableJoinCondition($table);
178
179
180
        //$this->columns($this->getPrefixedColumns());
181
182 1
        $this->select->join($prefixed_table, $on, $columns, Select::JOIN_INNER);
183 1
        return $this;
184
    }
185
186
    /**
187
     * Add an left outer table join to the search
188
     *
189
     * @param  string|array $table
190
     * @param  string $on
191
     * @param  string|array $columns by default won't retrieve any column from the joined table
192
     * @return TableSearch
193
     */
194 3
    public function joinLeft($table, $on, $columns = [])
195
    {
196 3
        $prefixed_table = $this->prefixTableJoinCondition($table);
197 3
        $this->select->join($prefixed_table, $on, $columns, Select::JOIN_LEFT);
198 3
        return $this;
199
    }
200
201
202
203
    /**
204
     * Add an right outer table join to the search
205
     *
206
     * @param  string|array $table
207
     * @param  string $on
208
     * @param  string|array $columns by default won't retrieve any column from the joined table
209
     * @return TableSearch
210
     */
211 1
    public function joinRight($table, $on, $columns = [])
212
    {
213 1
        $prefixed_table = $this->prefixTableJoinCondition($table);
214 1
        $this->select->join($prefixed_table, $on, $columns, Select::JOIN_RIGHT);
215 1
        return $this;
216
    }
217
218
219
220
    /**
221
     * Return the underlying select
222
     *
223
     * @return Select
224
     */
225 1
    public function getSelect()
226
    {
227 1
        return $this->select;
228
    }
229
230
    /**
231
     * Return SQL string
232
     *
233
     * @return string
234
     */
235 4
    public function getSql()
236
    {
237 4
        $adapterPlatform = $this->table->getTableManager()->getDbAdapter()->getPlatform();
238 4
        return $this->select->getSqlString($adapterPlatform);
239
    }
240
241
242
    /**
243
     * Return a json version of the results
244
     *
245
     * @return string Json encoded
246
     */
247 1
    public function toJson()
248
    {
249 1
        return json_encode($this->select->execute()->toArray());
250
    }
251
252
    /**
253
     * Return an array version of the results
254
     *
255
     * @return array
256
     */
257 10
    public function toArray()
258
    {
259 10
        return $this->select->execute()->toArray();
260
    }
261
262
263
264
    /**
265
     * Return record as an array
266
     *
267
     * @return ResultSet
268
     */
269 9
    public function execute()
270
    {
271 9
        $rs = new ResultSet($this->select, $this->table, !$this->has_modified_columns);
272 9
        return $rs;
273
    }
274
275
276
277
    /**
278
     * Return an array indexed by $indexKey
279
     * useful for comboboxes...
280
     *
281
     * @param string $columnKey
282
     * @param string $indexKey
283
     * @return array
284
     */
285 1
    public function toArrayColumn($columnKey, $indexKey)
286
    {
287 1
        $select = clone $this->select;
288 1
        $select->reset($select::COLUMNS)->columns([$columnKey, $indexKey]);
289 1
        return array_column($select->execute()->toArray(), $columnKey, $indexKey);
290
    }
291
292
    /**
293
     *
294
     * @param array|string $tableSpec
295
296
297
    protected function getPrefixedColumns($tableSpec=null)
298
    {
299
300
        if ($tableSpec === null) $tableSpec = $this->tableIdentifier;
301
302
        if (is_array($tableSpec)) {
303
            $alias = key($this->tableIdentifier);
304
            $table = $this->tableIdentifier[$alias];
305
        } else {
306
            $alias = $this->tableIdentifier;
307
            $table = $alias;
308
        }
309
310
        $columns = array();
311
        $pf = $this->table->getTableManager()->getDbAdapter()->getPlatform();
312
        $cols = array_keys($this->table->getColumnsInformation());
313
        foreach($cols as $column) {
314
            //$columns["$alias.$column"] = $column;
315
            //$columns[$column] = new \Zend\Db\Sql\TableIdentifier($pf->quoteIdentifier($alias) . $pf->getIdentifierSeparator() . $pf->quoteIdentifier($column));
316
            //$columns[$column] =
317
            $columns[$column] = new Predicate\Expression($pf->quoteIdentifier($alias) . $pf->getIdentifierSeparator() . $pf->quoteIdentifier($column));
318
        }
319
        return $columns;
320
321
322
    }
323
    */
324
    /**
325
     * Prefix table join condition
326
     *
327
     * @param string|array $table
328
     * @return array|string
329
     */
330 5
    protected function prefixTableJoinCondition($table)
331
    {
332 5
        $tm = $this->table->getTableManager();
333 5
        if (is_array($table)) {
334 4
            $alias = key($table);
335 4
            $prefixed_table = $tm->getPrefixedTable($table[$alias]);
336 4
            $table = [$alias => $prefixed_table];
337 5
        } elseif (is_string($table)) {
338 1
            $prefixed_table = $tm->getPrefixedTable($table);
339 1
            $table = $prefixed_table;
340 1
        }
341 5
        return $table;
342
    }
343
}
344