-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaginator.php
More file actions
168 lines (145 loc) · 4.58 KB
/
Copy pathPaginator.php
File metadata and controls
168 lines (145 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace FDevs\Pagination;
use FDevs\Pagination\Exception\TargetNotSupportException;
use FDevs\Pagination\Extension\ExtensionInterface;
use FDevs\Pagination\Model\Pagination;
use FDevs\Pagination\Model\PaginationInterface;
use FDevs\Pagination\Type\PaginateInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class Paginator implements PaginatorInterface
{
/**
* @var array|PaginateInterface[]
*/
private $typeList = [];
/**
* @var ExtensionRegistry
*/
private $extensionRegistry;
/**
* @var array|string[]
*/
private $typeExtension = [];
/**
* @var string
*/
private $paginationClass;
/**
* Paginator constructor.
*
* @param array|PaginateInterface[] $typeList
* @param ExtensionRegistry|null $extensionRegistry
* @param $paginationClass
*/
public function __construct(array $typeList = [], ExtensionRegistry $extensionRegistry = null, $paginationClass = Pagination::class)
{
$this->extensionRegistry = $extensionRegistry ?: new ExtensionRegistry();
$this->paginationClass = $paginationClass;
foreach ($typeList as $type) {
$this->addType($type);
}
}
/**
* {@inheritdoc}
*/
public function paginate($target, array $options = [], PaginationInterface $pagination = null)
{
$pagination = $pagination ?: new $this->paginationClass();
$paginator = $this->getType($target);
$resolver = new OptionsResolver();
$paginator->configureOptions($resolver);
$extList = $this->getExtensionList(get_class($paginator), $pagination);
foreach ($extList as $ext) {
$ext->configureOptions($resolver);
}
$options = $resolver->resolve($options);
$target = $paginator->prepareTarget($target, $options);
foreach ($extList as $ext) {
$target = $ext->prepareTarget($target, $options, $pagination);
}
return $paginator->paginate($target, $pagination, $options);
}
/**
* @param PaginateInterface $type
* @param array $extensions
*
* @return $this
*/
public function addType(PaginateInterface $type, array $extensions = [])
{
$this->typeList[get_class($type)] = $type;
foreach ($extensions as $extension) {
$this->addTypeExtension($type, $extension);
}
return $this;
}
/**
* @param string|ExtensionInterface $extension
* @param int $priority
*
* @return $this
*/
public function addTypeExtension($type, $extension, $priority = 0)
{
if ($extension instanceof ExtensionInterface) {
$this->extensionRegistry->addExtension($extension);
$this->typeExtension[$type][$priority][get_class($extension)] = true;
} elseif (is_string($extension)) {
$this->typeExtension[$type][$priority][$extension] = true;
}
return $this;
}
/**
* @param string $type
* @param PaginationInterface $pagination
*
* @return ExtensionInterface[]
*/
private function getExtensionList($type, PaginationInterface $pagination)
{
$extList = [];
if (isset($this->typeExtension[$type])) {
ksort($this->typeExtension[$type]);
$typeExt = call_user_func_array('array_merge', $this->typeExtension[$type]);
foreach ($typeExt as $name => $item) {
$extList = $this->getExtList($name, $pagination, $extList);
}
}
return $extList;
}
/**
* @param string $name
* @param PaginationInterface $pagination
* @param array $extList
*
* @return array
*/
private function getExtList($name, PaginationInterface $pagination, array $extList = [])
{
$ext = $this->extensionRegistry->getExtension($name);
$deps = $ext->getDependency();
foreach ($deps as $dep) {
$extList = $this->getExtList($dep, $pagination, $extList);
}
if ($ext->supportPagination($pagination)) {
$extList[$name] = $ext;
}
return $extList;
}
/**
* @param mixed $target
*
* @return PaginateInterface
*
* @throws TargetNotSupportException
*/
private function getType($target)
{
foreach ($this->typeList as $type) {
if ($type->support($target)) {
return $type;
}
}
throw new TargetNotSupportException($target);
}
}