|
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 CakeDC\Api\Service\Action\Action; |
|
15
|
|
|
use Cake\Event\Event; |
|
16
|
|
|
use Cake\Event\EventListenerInterface; |
|
17
|
|
|
use Cake\ORM\Entity; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class SortExtension |
|
21
|
|
|
* |
|
22
|
|
|
* @package CakeDC\Api\Service\Action\Extension |
|
23
|
|
|
*/ |
|
24
|
|
|
class SortExtension extends Extension implements EventListenerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_defaultConfig = [ |
|
31
|
|
|
'sortField' => 'sort', |
|
32
|
|
|
'directionField' => 'direction', |
|
33
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns a list of events this object is implementing. When the class is registered |
|
37
|
|
|
* in an event manager, each individual method will be associated with the respective event. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public function implementedEvents() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
6 |
|
'Action.Crud.onFindEntities' => 'findEntities', |
|
45
|
6 |
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* find entities |
|
50
|
|
|
* |
|
51
|
|
|
* @param Event $event An Event instance |
|
52
|
|
|
* @return Entity |
|
53
|
|
|
*/ |
|
54
|
5 |
|
public function findEntities(Event $event) |
|
55
|
|
|
{ |
|
56
|
5 |
|
$action = $event->getSubject(); |
|
57
|
5 |
|
$query = $event->getData('query'); |
|
58
|
5 |
|
if ($event->result) { |
|
59
|
|
|
$query = $event->result; |
|
60
|
|
|
} |
|
61
|
5 |
|
$data = $action->getData(); |
|
62
|
5 |
|
$direction = 'asc'; |
|
63
|
5 |
|
$sort = null; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
5 |
|
$directionField = $this->getConfig('directionField'); |
|
66
|
5 |
|
$sortField = $this->getConfig('sortField'); |
|
67
|
5 |
|
if (!empty($data[$directionField])) { |
|
68
|
2 |
|
$direction = $data[$directionField]; |
|
69
|
2 |
|
} |
|
70
|
5 |
|
if (!empty($data[$sortField])) { |
|
71
|
4 |
|
$sort = $data[$sortField]; |
|
72
|
4 |
|
$query->order([$sort => $direction]); |
|
73
|
4 |
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
return $query; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.