These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Representation; |
||
4 | |||
5 | use Hateoas\Configuration\Annotation as Hateoas; |
||
6 | use JMS\Serializer\Annotation as Serializer; |
||
7 | |||
8 | /** |
||
9 | * @Serializer\ExclusionPolicy("all") |
||
10 | * @Serializer\XmlRoot("collection") |
||
11 | * @Serializer\AccessorOrder("custom", custom = {"page", "limit", "pages", "total"}) |
||
12 | * |
||
13 | * @Hateoas\Relation( |
||
14 | * "first", |
||
15 | * href = @Hateoas\Route( |
||
16 | * "expr(object.getRoute())", |
||
17 | * parameters = "expr(object.getParameters(1))", |
||
18 | * absolute = "expr(object.isAbsolute())" |
||
19 | * ) |
||
20 | * ) |
||
21 | * @Hateoas\Relation( |
||
22 | * "last", |
||
23 | * href = @Hateoas\Route( |
||
24 | * "expr(object.getRoute())", |
||
25 | * parameters = "expr(object.getParameters(object.getPages()))", |
||
26 | * absolute = "expr(object.isAbsolute())" |
||
27 | * ), |
||
28 | * exclusion = @Hateoas\Exclusion( |
||
29 | * excludeIf = "expr(object.getPages() === null)" |
||
30 | * ) |
||
31 | * ) |
||
32 | * @Hateoas\Relation( |
||
33 | * "next", |
||
34 | * href = @Hateoas\Route( |
||
35 | * "expr(object.getRoute())", |
||
36 | * parameters = "expr(object.getParameters(object.getPage() + 1))", |
||
37 | * absolute = "expr(object.isAbsolute())" |
||
38 | * ), |
||
39 | * exclusion = @Hateoas\Exclusion( |
||
40 | * excludeIf = "expr(object.getPages() !== null && (object.getPage() + 1) > object.getPages())" |
||
41 | * ) |
||
42 | * ) |
||
43 | * @Hateoas\Relation( |
||
44 | * "previous", |
||
45 | * href = @Hateoas\Route( |
||
46 | * "expr(object.getRoute())", |
||
47 | * parameters = "expr(object.getParameters(object.getPage() - 1))", |
||
48 | * absolute = "expr(object.isAbsolute())" |
||
49 | * ), |
||
50 | * exclusion = @Hateoas\Exclusion( |
||
51 | * excludeIf = "expr((object.getPage() - 1) < 1)" |
||
52 | * ) |
||
53 | * ) |
||
54 | * |
||
55 | * @author Adrien Brault <[email protected]> |
||
56 | */ |
||
57 | class PaginatedRepresentation extends AbstractSegmentedRepresentation |
||
58 | { |
||
59 | /** |
||
60 | * @var int |
||
61 | * |
||
62 | * @Serializer\Expose |
||
63 | * @Serializer\Type("integer") |
||
64 | * @Serializer\XmlAttribute |
||
65 | */ |
||
66 | private $page; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | * |
||
71 | * @Serializer\Expose |
||
72 | * @Serializer\Type("integer") |
||
73 | * @Serializer\XmlAttribute |
||
74 | */ |
||
75 | private $pages; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | private $pageParameterName; |
||
81 | |||
82 | View Code Duplication | public function __construct( |
|
0 ignored issues
–
show
|
|||
83 | $inline, |
||
84 | $route, |
||
85 | array $parameters = array(), |
||
86 | $page, |
||
87 | $limit, |
||
88 | $pages, |
||
89 | $pageParameterName = null, |
||
90 | $limitParameterName = null, |
||
91 | $absolute = false, |
||
92 | $total = null |
||
93 | ) { |
||
94 | parent::__construct($inline, $route, $parameters, $limit, $total, $limitParameterName, $absolute); |
||
95 | |||
96 | $this->page = $page; |
||
97 | $this->pages = $pages; |
||
98 | $this->pageParameterName = $pageParameterName ?: 'page'; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return int |
||
103 | */ |
||
104 | public function getPage() |
||
105 | { |
||
106 | return $this->page; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param null $page |
||
111 | * @param null $limit |
||
112 | * @return array |
||
113 | */ |
||
114 | public function getParameters($page = null, $limit = null) |
||
115 | { |
||
116 | $parameters = parent::getParameters($limit); |
||
117 | |||
118 | unset($parameters[$this->pageParameterName]); |
||
119 | $parameters[$this->pageParameterName] = null === $page ? $this->getPage() : $page; |
||
120 | |||
121 | $this->moveParameterToEnd($parameters, $this->getLimitParameterName()); |
||
122 | |||
123 | return $parameters; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return int |
||
128 | */ |
||
129 | public function getPages() |
||
130 | { |
||
131 | return $this->pages; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getPageParameterName() |
||
138 | { |
||
139 | return $this->pageParameterName; |
||
140 | } |
||
141 | } |
||
142 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.