|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service\Action\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Event\Event; |
|
15
|
|
|
use Cake\Event\EventListenerInterface; |
|
16
|
|
|
use Cake\Log\Log; |
|
17
|
|
|
use Cake\ORM\Entity; |
|
18
|
|
|
use Cake\ORM\Table; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class FilterExtension |
|
22
|
|
|
* |
|
23
|
|
|
* @package CakeDC\Api\Service\Action\Extension |
|
24
|
|
|
*/ |
|
25
|
|
|
class FilterExtension extends Extension implements EventListenerInterface |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Returns a list of events this object is implementing. When the class is registered |
|
30
|
|
|
* in an event manager, each individual method will be associated with the respective event. |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function implementedEvents() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
1 |
|
'Action.Crud.onFindEntities' => 'findEntities', |
|
38
|
1 |
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* find entities |
|
43
|
|
|
* |
|
44
|
|
|
* @param Event $event An Event instance |
|
45
|
|
|
* @return Entity |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function findEntities(Event $event) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$action = $event->getSubject(); |
|
50
|
1 |
|
$query = $event->getData('query'); |
|
51
|
|
|
|
|
52
|
1 |
|
if ($event->result) { |
|
53
|
|
|
$query = $event->result; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* @var Table $table */ |
|
57
|
1 |
|
$table = $query->getRepository(); |
|
58
|
1 |
|
$schema = $table->getSchema(); |
|
59
|
1 |
|
$fields = $schema->columns(); |
|
60
|
1 |
|
$fields = array_flip($fields); |
|
61
|
1 |
|
$data = $action->getData(); |
|
62
|
1 |
|
$postfixDelimeter = '$'; |
|
63
|
|
|
$filterPostfixes = [ |
|
64
|
1 |
|
'' => '', |
|
65
|
1 |
|
'ge' => ' >=', |
|
66
|
1 |
|
'le' => ' <=', |
|
67
|
1 |
|
'gt' => ' >', |
|
68
|
1 |
|
'lt' => ' <', |
|
69
|
1 |
|
'llike' => ' LIKE', |
|
70
|
1 |
|
'rlike' => ' LIKE', |
|
71
|
1 |
|
'like' => ' LIKE', |
|
72
|
|
|
'ne' => ' !=' |
|
73
|
1 |
|
]; |
|
74
|
1 |
|
foreach ($filterPostfixes as $postfix => $rule) { |
|
75
|
1 |
|
$filter = collection($data) |
|
76
|
1 |
|
->filter(function ($item, $key) use ($fields, $postfix, $postfixDelimeter) { |
|
77
|
1 |
|
if ($postfix !== '') { |
|
78
|
1 |
|
if (strpos($key, $postfixDelimeter . $postfix) === false) { |
|
79
|
1 |
|
return false; |
|
80
|
|
|
} |
|
81
|
1 |
|
$key = str_replace($postfixDelimeter . $postfix, '', $key); |
|
82
|
1 |
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
return array_key_exists($key, $fields); |
|
85
|
1 |
|
}) |
|
86
|
1 |
|
->toArray(); |
|
87
|
|
|
|
|
88
|
1 |
|
if (!empty($filter)) { |
|
89
|
1 |
|
foreach ($filter as $field => $value) { |
|
90
|
1 |
|
if ($postfix == 'ge' || $postfix == 'ne') { |
|
91
|
|
|
unset($data[$field]); |
|
92
|
|
|
} |
|
93
|
1 |
|
if (is_array($value)) { |
|
94
|
|
|
if ($postfix == '') { |
|
95
|
|
|
$query->where([$field . ' IN' => $value]); |
|
96
|
|
|
} elseif ($postfix == 'ne') { |
|
97
|
|
|
$query->where([$field . ' NOT IN' => $value]); |
|
98
|
|
|
} |
|
99
|
|
|
} else { |
|
100
|
1 |
|
if ($postfix !== '') { |
|
101
|
1 |
|
$field = str_replace($postfixDelimeter . $postfix, '', $field) . $rule; |
|
102
|
1 |
|
if ($postfix == 'llike' || $postfix == 'like') { |
|
103
|
1 |
|
$value = '%' . $value; |
|
104
|
1 |
|
} |
|
105
|
1 |
|
if ($postfix == 'rlike' || $postfix == 'like') { |
|
106
|
1 |
|
$value = $value . '%'; |
|
107
|
1 |
|
} |
|
108
|
1 |
|
} |
|
109
|
1 |
|
$query->where([$field => $value]); |
|
110
|
|
|
} |
|
111
|
1 |
|
} |
|
112
|
1 |
|
} |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
return $query; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|