1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lampager\Cake; |
4
|
|
|
|
5
|
|
|
use Cake\ORM\Entity; |
6
|
|
|
use Lampager\ArrayProcessor as BaseArrayProcessor; |
7
|
|
|
use Lampager\Cake\ORM\Query; |
8
|
|
|
use Lampager\Query as LampagerQuery; |
9
|
|
|
|
10
|
|
|
class ArrayProcessor extends BaseArrayProcessor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritDoc} |
14
|
|
|
*/ |
15
|
|
|
protected function defaultFormat($rows, array $meta, LampagerQuery $query) |
16
|
|
|
{ |
17
|
|
|
return new PaginationResult($rows, $meta); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Remove alias if prexied. |
22
|
|
|
* e.g., Foos.bar -> bar |
23
|
|
|
* |
24
|
|
|
* @param string $column Current column |
25
|
|
|
* @param string $alias Current model alias |
26
|
|
|
* @return string Unaliased column where applicable |
27
|
|
|
*/ |
28
|
|
|
protected function removeAlias($column, $alias) |
29
|
|
|
{ |
30
|
|
|
if (strpos($column, "{$alias}.") !== 0) { |
31
|
|
|
return $column; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return substr($column, strlen("{$alias}.")); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
protected function field($row, $column) |
41
|
|
|
{ |
42
|
|
|
if (!isset($row[$column]) && $row instanceof Entity) { |
43
|
|
|
$unaliased_column = $this->removeAlias($column, $row->getSource()); |
44
|
|
|
|
45
|
|
|
if (isset($row[$unaliased_column])) { |
46
|
|
|
return $row[$unaliased_column]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return parent::field($row, $column); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
protected function makeCursor(LampagerQuery $query, $row) |
57
|
|
|
{ |
58
|
|
|
/** @var Query $builder */ |
59
|
|
|
$builder = $query->builder(); |
60
|
|
|
$alias = $builder->getRepository()->getAlias(); |
61
|
|
|
|
62
|
|
|
/** @var string[] $cursor */ |
63
|
|
|
$cursor = []; |
64
|
|
|
|
65
|
|
|
foreach ($query->orders() as $order) { |
66
|
|
|
if (isset($row[$order->column()])) { |
67
|
|
|
$cursor[$order->column()] = $row[$order->column()]; |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$column = $this->removeAlias($order->column(), $alias); |
72
|
|
|
|
73
|
|
|
if (isset($row[$column])) { |
74
|
|
|
$cursor[$order->column()] = $row[$column]; |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $cursor; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|