1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lampager\Idiorm; |
4
|
|
|
|
5
|
|
|
use IdiormResultSet; |
6
|
|
|
use Lampager\ArrayProcessor; |
7
|
|
|
use Lampager\Contracts\Formatter; |
8
|
|
|
use Lampager\Idiorm\Concerns\HasSnakeAliases; |
9
|
|
|
use Lampager\Query; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Processor |
13
|
|
|
* |
14
|
|
|
* @method static set_default_formatter(callable|Formatter|string $formatter) Override static default formatter. |
15
|
|
|
* @method static restore_default_formatter() Restore static default formatter. |
16
|
|
|
* @method $this use_formatter(callable|Formatter|string $formatter) Use custom formatter. |
17
|
|
|
* @method $this restore_formatter() Restore default formatter. |
18
|
|
|
* |
19
|
|
|
* @see AbstractProcessor, ArrayProcessor |
20
|
|
|
*/ |
21
|
|
|
class Processor extends ArrayProcessor |
22
|
|
|
{ |
23
|
|
|
use HasSnakeAliases; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Return comparable value from a row. |
27
|
|
|
* |
28
|
|
|
* @param mixed $row |
29
|
|
|
* @param string $column |
30
|
|
|
* @return int|string |
31
|
|
|
*/ |
32
|
21 |
|
protected function field($row, $column) |
33
|
|
|
{ |
34
|
21 |
|
return parent::field($row, static::dropTablePrefix($column)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Slice rows, like PHP function array_slice(). |
39
|
|
|
* |
40
|
|
|
* @param array[]|IdiormResultSet $rows |
41
|
|
|
* @param int $offset |
42
|
|
|
* @param null|int $length |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
21 |
|
protected function slice($rows, $offset, $length = null) |
46
|
|
|
{ |
47
|
21 |
|
$r = $rows instanceof IdiormResultSet ? $rows->get_results() : $rows; |
48
|
21 |
|
$r = array_slice($r, $offset, $length); |
49
|
21 |
|
return $rows instanceof IdiormResultSet ? new IdiormResultSet($r) : $r; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Reverse rows, like PHP function array_reverse(). |
54
|
|
|
* |
55
|
|
|
* @param array[]|IdiormResultSet $rows |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
8 |
|
protected function reverse($rows) |
59
|
|
|
{ |
60
|
8 |
|
$r = $rows instanceof IdiormResultSet ? $rows->get_results() : $rows; |
61
|
8 |
|
$r = array_reverse($r); |
62
|
8 |
|
return $rows instanceof IdiormResultSet ? new IdiormResultSet($r) : $r; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Format result. |
67
|
|
|
* |
68
|
|
|
* @param array[]|IdiormResultSet $rows |
69
|
|
|
* @param array $meta |
70
|
|
|
* @param Query $query |
71
|
|
|
* @return PaginationResult |
72
|
|
|
*/ |
73
|
21 |
|
protected function defaultFormat($rows, array $meta, Query $query) |
74
|
|
|
{ |
75
|
21 |
|
return new PaginationResult($rows, $meta); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Drop table prefix on column name. |
80
|
|
|
* |
81
|
|
|
* @param string $column |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
21 |
|
protected static function dropTablePrefix($column) |
85
|
|
|
{ |
86
|
21 |
|
$segments = explode('.', $column); |
87
|
|
|
|
88
|
21 |
|
return end($segments); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|