1
|
|
|
<?php |
2
|
|
|
namespace Germania\Pagination; |
3
|
|
|
|
4
|
|
|
class PaginationFactory |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var int |
10
|
|
|
*/ |
11
|
|
|
public $default_page_size = 25; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Default class FQDN for creating the Pagination instance |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $default_pagination_class; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int|integer $default_page_size Default: 25 |
24
|
|
|
* @param string|null $php_class Default: \Germania\Pagination\Pagination |
25
|
|
|
*/ |
26
|
88 |
|
public function __construct( int $default_page_size = 25, string $php_class = null ) |
27
|
|
|
{ |
28
|
88 |
|
$this->default_page_size = $default_page_size; |
29
|
88 |
|
$this->default_pagination_class = $php_class ?: Pagination::class; |
30
|
88 |
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $items The items to paginate: int, array or Traversable (countable) |
35
|
|
|
* @param int|array $pagination_data The data to construct the pagination with |
36
|
|
|
* @param string $php_class Optional: Custom pagination class FQDN |
|
|
|
|
37
|
|
|
* @return |
38
|
|
|
*/ |
39
|
48 |
|
public function __invoke( $items, $pagination_data, string $custom_php_class = null ) |
40
|
|
|
{ |
41
|
48 |
|
$items_count = $this->countItems( $items ); |
42
|
|
|
|
43
|
44 |
|
$klasse = $custom_php_class ?: $this->default_pagination_class; |
44
|
44 |
|
$pagination = new $klasse( $items_count, $this->default_page_size ); |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
// Eval user data |
48
|
44 |
|
if (is_numeric($pagination_data)): |
49
|
20 |
|
$pagination->setCurrent( $pagination_data ); |
50
|
|
|
|
51
|
24 |
|
elseif (is_array($pagination_data)): |
52
|
20 |
|
if (!empty($pagination_data['size'])): |
53
|
8 |
|
$pagination->setPageSize( $pagination_data['size'] ); |
54
|
|
|
endif; |
55
|
|
|
|
56
|
20 |
|
$pagination->setCurrent( $pagination_data['number'] ?? 0); |
57
|
|
|
|
58
|
|
|
else: |
59
|
4 |
|
throw new PaginationInvalidArgumentException("Integer or Array expected"); |
60
|
|
|
|
61
|
|
|
endif; |
62
|
40 |
|
return $pagination; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Counts the items to determine the $items_count parameter for Pagination class. |
68
|
|
|
* |
69
|
|
|
* @param mixed $items |
70
|
|
|
* @return int |
71
|
|
|
* @throws PaginationInvalidArgumentException |
72
|
|
|
*/ |
73
|
48 |
|
protected function countItems( $items ) |
74
|
|
|
{ |
75
|
48 |
|
if (is_array($items)): |
76
|
8 |
|
return count($items); |
77
|
|
|
|
78
|
40 |
|
elseif ($items instanceOf \Countable): |
79
|
28 |
|
return count($items); |
80
|
|
|
|
81
|
12 |
|
elseif ($items instanceOf \Traversable): |
82
|
4 |
|
return iterator_count($items); |
83
|
|
|
|
84
|
8 |
|
elseif (is_int($items)): |
85
|
4 |
|
return $items; |
86
|
|
|
|
87
|
|
|
else: |
88
|
4 |
|
throw new PaginationInvalidArgumentException("Countable, Traversable or integer expected"); |
89
|
|
|
endif; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.