JsonApiPaginationDecorator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 140
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A jsonSerialize() 0 4 1
A getLinks() 0 17 4
A getMeta() 0 12 2
A buildField() 0 13 2
A buildUriString() 0 6 1
1
<?php
2
namespace Germania\Pagination;
3
4
use Psr\Http\Message\UriInterface;
5
6
class JsonApiPaginationDecorator extends PaginationDecoratorAbstract implements  \JsonSerializable
7
{
8
9
	/**
10
	 * @var UriInterface
11
	 */
12
	public $uri;
13
14
	/**
15
	 * @var array
16
	 */
17
	public $query_params = array();
18
19
	/**
20
	 * @var array
21
	 */
22
	public $json_result = array();
23
24
	/**
25
	 * @var array
26
	 */
27
	public $filter_result = false;
28
29
30
	/**
31
	 * @param PaginationInterface $pagination    The pagination to decorate
32
	 * @param UriInterface        $uri           PSR-7 URI instance
33
	 * @param array               $query_params  Optional: default query parameters for URIs
34
	 * @param bool                $filter_result Optional: Filter out null member values in jsonSerialize's result. 
35
	 *                                           Default: FALSE
36
	 */
37 48
	public function __construct( PaginationInterface $pagination, UriInterface $uri, array $query_params = array(), bool $filter_result = false )
38
	{
39 48
		$this->uri = $uri;
40 48
		$this->query_params = $query_params;
41 48
		$this->filter_result = $filter_result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filter_result of type boolean is incompatible with the declared type array of property $filter_result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
43
   
44 48
		$this->json_result['first'] = null;
45 48
		$this->json_result['prev']  = null;
46 48
		$this->json_result['next']  = null;
47 48
		$this->json_result['last']  = null;		
48
49 48
		parent::__construct($pagination);
50 48
	}
51
52
53
	/**
54
	 * JsonSerializable alias for getLinks()
55
	 * @return array
56
	 */
57 24
	public function jsonSerialize()
58
	{
59 24
		return $this->getLinks();
60
	}
61
62
63
	/**
64
	 * Returns an array with `first`, `last`, `next`, and `previous` elements
65
	 * according to the JsonAPI pagination specs.
66
	 * 
67
	 * @return array
68
	 * @see    https://jsonapi.org/format/#fetching-pagination
69
	 */
70 48
	public function getLinks() : array
71
	{
72 48
		if (!$this->pagination->isActive()):
73 24
			return $this->json_result;
74
		endif;
75
76 24
		$page_size = $this->isDefaultPageSize() ? null : $this->pagination->getPageSize( );
77
78 24
        $this->buildField('first', $this->pagination->getFirst(),    $page_size);
79 24
        $this->buildField('prev',  $this->pagination->getPrevious(), $page_size);
80 24
        $this->buildField('next',  $this->pagination->getNext(),     $page_size);
81 24
        $this->buildField('last',  $this->pagination->getLast(),     $page_size);
82
83 24
        return $this->filter_result 
84 12
        ? array_filter($this->json_result)
85 24
        : $this->json_result;		
86
	}
87
88
	/**
89
	 * Returns an array with non-standard meta information about the pagination
90
	 *
91
	 * - numberOfPages
92
	 * - currentPage
93
	 * - pageSize
94
	 * 
95
	 * @return array
96
	 */
97 48
	public function getMeta() : array
98
	{
99 48
		if (!$this->pagination->isActive()):
100 24
			return array();
101
		endif;
102
103
		return array(
104 24
        	'numberOfPages' => $this->pagination->getPagesCount(),
105 24
        	'currentPage'   => $this->pagination->getCurrent(),
106 24
        	'pageSize'      => $this->pagination->getPageSize()
107
		);
108
	}
109
110
111
112
113
	/**
114
	 * @param  string   $field     Link element field name
115
	 * @param  int      $page      Page number
116
	 * @param  int|null $page_size Page size
117
	 * @return void
118
	 */
119 18
	protected function buildField($field, $page, $page_size) {
120
121
		$notnullfilter = function($i) { return !is_null($i); };
122
123 24
		if (is_null($page)):
124 24
			$this->json_result[$field] = null;
125
		else:
126
			$pagination_query_params = array(
127 24
				'page' => array_filter(['size' => $page_size, 'number' => $page], $notnullfilter) 
128
			);
129 24
			$this->json_result[$field] = $this->buildUriString( $pagination_query_params );
130
		endif;
131 24
	}
132
133
134
    /**
135
     * @param  array $custom_params
136
     * @return string
137
     */
138 24
    protected function buildUriString( array $custom_params) : string
139
    {
140 24
    	$params = array_merge($this->query_params, $custom_params);
141 24
    	$query = http_build_query( $params );
142 24
        return (string) $this->uri->withQuery( $query );
143
    }
144
145
}