1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
// @codeCoverageIgnoreStart |
4
|
|
|
App::uses('ModelBehavior', 'Model'); |
5
|
|
|
App::uses('LampagerPaginator', 'Lampager.Model'); |
6
|
|
|
App::uses('LampagerArrayProcessor', 'Lampager.Model'); |
7
|
|
|
// @codeCoverageIgnoreEnd |
8
|
|
|
|
9
|
|
|
class LampagerBehavior extends ModelBehavior |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** @var string[] */ |
12
|
|
|
public $mapMethods = [ |
13
|
|
|
'/\b_findLampager\b/' => 'findLampager', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
32 |
|
public function setup(Model $model, $config = []) |
20
|
|
|
{ |
21
|
32 |
|
$model->findMethods['lampager'] = true; |
22
|
32 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
32 |
|
public function cleanup(Model $model) |
28
|
|
|
{ |
29
|
32 |
|
unset($model->findMethods['lampager']); |
30
|
32 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Hanldle the custom finder. Only called by Model::find(). |
34
|
|
|
* |
35
|
|
|
* @param Model $model Model. |
36
|
|
|
* @param string $method Method. |
37
|
|
|
* @param string $state Either "before" or "after" |
38
|
|
|
* @param array $query Query. |
39
|
|
|
* @param array $results Results. |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
32 |
|
public function findLampager(Model $model, $method, $state, array $query = [], array $results = []) |
|
|
|
|
43
|
|
|
{ |
44
|
32 |
|
return $this->{__FUNCTION__ . ucfirst($state)}($model, $query, $results); |
45
|
|
|
} |
46
|
|
|
|
47
|
32 |
|
protected function findLampagerBefore(Model $model, array $query = [], array $results = []) |
|
|
|
|
48
|
|
|
{ |
49
|
32 |
|
return LampagerPaginator::create($model, $query)->transformer->build(isset($query['cursor']) ? $query['cursor'] : []); |
50
|
|
|
} |
51
|
|
|
|
52
|
32 |
|
protected function findLampagerAfter(Model $model, array $query = [], array $results = []) |
53
|
|
|
{ |
54
|
32 |
|
return LampagerArrayProcessor::create($model)->process($query['config'], $results); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Paginate the Model. Only called by PaginatorComponent::paginate(). |
59
|
|
|
* |
60
|
|
|
* @param Model $model |
61
|
|
|
* @param array $conditions |
62
|
|
|
* @param array $fields |
63
|
|
|
* @param array $order |
64
|
|
|
* @param int $limit |
65
|
|
|
* @param int $page |
66
|
|
|
* @param int $recursive |
67
|
|
|
* @param array $extra |
68
|
|
|
*/ |
69
|
16 |
|
public function paginate(Model $model, $conditions, $fields, $order, $limit, $page = 1, $recursive = null, array $extra = []) |
70
|
|
|
{ |
71
|
|
|
/** |
72
|
|
|
* Extract extra parameters which may include |
73
|
|
|
* |
74
|
|
|
* @var bool $forward |
75
|
|
|
* @var bool $backward |
76
|
|
|
* @var bool $exclusive |
77
|
|
|
* @var bool $inclusive |
78
|
|
|
* @var bool $seekable |
79
|
|
|
* @var bool $unseekable |
80
|
|
|
* @var array $cursor |
81
|
|
|
*/ |
82
|
16 |
|
extract($extra, EXTR_SKIP); |
83
|
|
|
|
84
|
16 |
|
return $model->find('lampager', compact( |
85
|
16 |
|
'conditions', |
86
|
16 |
|
'fields', |
87
|
16 |
|
'order', |
88
|
16 |
|
'limit', |
89
|
16 |
|
'page', |
90
|
16 |
|
'recursive', |
91
|
16 |
|
'forward', |
92
|
16 |
|
'backward', |
93
|
16 |
|
'exclusive', |
94
|
16 |
|
'inclusive', |
95
|
16 |
|
'seekable', |
96
|
16 |
|
'unseekable', |
97
|
16 |
|
'cursor' |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.