1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Pager\Model\Factory; |
4
|
|
|
|
5
|
|
|
use BenTools\Pager\Contract\PageInterface; |
6
|
|
|
use BenTools\Pager\Contract\PagerFactoryInterface; |
7
|
|
|
use BenTools\Pager\Contract\PagerInterface; |
8
|
|
|
use BenTools\Pager\Contract\PageUrlBuilderInterface; |
9
|
|
|
use BenTools\Pager\Model\Pager; |
10
|
|
|
use function GuzzleHttp\Psr7\parse_query; |
|
|
|
|
11
|
|
|
use GuzzleHttp\Psr7\Uri; |
12
|
|
|
|
13
|
|
|
class PageParameterUrlBuilder implements PageUrlBuilderInterface, PagerFactoryInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $baseUrl; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $perPage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $pageNumberQueryParam; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* PageUrlBuilder constructor. |
32
|
|
|
* @param string $baseUrl |
33
|
|
|
* @param int $perPage |
34
|
|
|
* @param string $pageNumberQueryParam |
35
|
|
|
*/ |
36
|
|
|
public function __construct(string $baseUrl, int $perPage, string $pageNumberQueryParam = 'page') |
37
|
|
|
{ |
38
|
|
|
$this->baseUrl = $baseUrl; |
39
|
|
|
$this->perPage = $perPage; |
40
|
|
|
$this->pageNumberQueryParam = $pageNumberQueryParam; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the current page. |
45
|
|
|
* |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
|
|
private function getCurrentPageNumber(): int |
49
|
|
|
{ |
50
|
|
|
$parsedQuery = parse_query((new Uri($this->baseUrl))->getQuery()); |
51
|
|
|
return $parsedQuery[$this->pageNumberQueryParam] ?? 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
|
public function buildUrl(PagerInterface $pager, PageInterface $page): string |
58
|
|
|
{ |
59
|
|
|
return (string) Uri::withQueryValue(new Uri($this->baseUrl), $this->pageNumberQueryParam, $page->getPageNumber()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param int $perPage |
64
|
|
|
* @param string $pageNumberQueryParam |
65
|
|
|
* @return PageParameterUrlBuilder |
66
|
|
|
* @throws \RuntimeException |
67
|
|
|
*/ |
68
|
|
|
public static function fromRequestUri(int $perPage, string $pageNumberQueryParam = 'page'): self |
69
|
|
|
{ |
70
|
|
|
if (!isset($_SERVER['REQUEST_URI'])) { |
71
|
|
|
throw new \RuntimeException('$_SERVER[\'REQUEST_URI\'] is not set.'); |
72
|
|
|
} |
73
|
|
|
return new static($_SERVER['REQUEST_URI'], $perPage, $pageNumberQueryParam); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int|null $numFound |
78
|
|
|
* @return PagerInterface |
79
|
|
|
*/ |
80
|
|
|
public function createPager(int $numFound = null): PagerInterface |
81
|
|
|
{ |
82
|
|
|
$pager = new Pager($this->perPage, $this->getCurrentPageNumber(), $numFound); |
83
|
|
|
return $pager; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths