1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Traits\DataProvider; |
15
|
|
|
|
16
|
|
|
use Maslosoft\EmbeDi\EmbeDi; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\PaginationInterface; |
18
|
|
|
use Maslosoft\Mangan\Pagination; |
19
|
|
|
use UnexpectedValueException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* PaginationTrait |
23
|
|
|
* |
24
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
25
|
|
|
*/ |
26
|
|
|
trait PaginationTrait |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Pagination instance |
31
|
|
|
* @var boolean|array|PaginationInterface |
32
|
|
|
*/ |
33
|
|
|
private $pagination = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the pagination object. |
37
|
|
|
* @param string $className the pagination object class name, use this param to override default pagination class. |
38
|
|
|
* @return PaginationInterface|Pagination|false the pagination object. If this is false, it means the pagination is disabled. |
39
|
|
|
*/ |
40
|
4 |
|
public function getPagination($className = Pagination::class) |
41
|
|
|
{ |
42
|
4 |
|
if ($this->pagination === false) |
43
|
|
|
{ |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
4 |
|
if ($this->pagination === null) |
47
|
|
|
{ |
48
|
2 |
|
$this->pagination = new $className; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// FIXME: Attach pagination options if it's array. |
52
|
|
|
// It might be array, when configured via constructor |
53
|
4 |
|
if (is_array($this->pagination)) |
54
|
|
|
{ |
55
|
|
|
if (empty($this->pagination['class'])) |
56
|
|
|
{ |
57
|
|
|
$this->pagination['class'] = $className; |
58
|
|
|
} |
59
|
|
|
if(isset($this->pagination['pageSize'])) |
60
|
|
|
{ |
61
|
|
|
$this->pagination['size'] = $this->pagination['pageSize']; |
62
|
|
|
unset($this->pagination['pageSize']); |
63
|
|
|
} |
64
|
|
|
$this->pagination = EmbeDi::fly()->apply($this->pagination); |
65
|
|
|
} |
66
|
4 |
|
return $this->pagination; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set pagination |
71
|
|
|
* @param boolean|array|PaginationInterface $pagination |
72
|
|
|
* @return static |
73
|
|
|
*/ |
74
|
2 |
|
public function setPagination($pagination) |
75
|
|
|
{ |
76
|
|
|
// Disable pagination completely |
77
|
2 |
|
if (false === $pagination) |
78
|
|
|
{ |
79
|
|
|
$this->pagination = false; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Configure from array |
84
|
2 |
|
if (is_array($pagination)) |
85
|
|
|
{ |
86
|
2 |
|
if (empty($pagination['class'])) |
87
|
|
|
{ |
88
|
2 |
|
$pagination['class'] = Pagination::class; |
89
|
|
|
} |
90
|
2 |
|
if(isset($pagination['pageSize'])) |
91
|
|
|
{ |
92
|
|
|
$pagination['size'] = $pagination['pageSize']; |
93
|
|
|
unset($pagination['pageSize']); |
94
|
|
|
} |
95
|
2 |
|
$this->pagination = EmbeDi::fly()->apply($pagination); |
96
|
2 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Set object instance |
100
|
|
|
if ($pagination instanceof PaginationInterface) |
101
|
|
|
{ |
102
|
|
|
$this->pagination = $pagination; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
throw new UnexpectedValueException(sprintf('Expected `false` or `array` or `%s`, got %s', PaginationInterface::class, is_object($pagination) ? get_class($pagination) : gettype($pagination))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|