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 = {"offset", "limit", "total"}) |
||
12 | * |
||
13 | * @Hateoas\Relation( |
||
14 | * "first", |
||
15 | * href = @Hateoas\Route( |
||
16 | * "expr(object.getRoute())", |
||
17 | * parameters = "expr(object.getParameters(0))", |
||
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.getTotal() - 1) - (object.getTotal() - 1) % object.getLimit()))", |
||
26 | * absolute = "expr(object.isAbsolute())" |
||
27 | * ), |
||
28 | * exclusion = @Hateoas\Exclusion( |
||
29 | * excludeIf = "expr(object.getTotal() === null)" |
||
30 | * ) |
||
31 | * ) |
||
32 | * @Hateoas\Relation( |
||
33 | * "next", |
||
34 | * href = @Hateoas\Route( |
||
35 | * "expr(object.getRoute())", |
||
36 | * parameters = "expr(object.getParameters(object.getOffset() + object.getLimit()))", |
||
37 | * absolute = "expr(object.isAbsolute())" |
||
38 | * ), |
||
39 | * exclusion = @Hateoas\Exclusion( |
||
40 | * excludeIf = "expr(object.getTotal() !== null && (object.getOffset() + object.getLimit()) >= object.getTotal())" |
||
41 | * ) |
||
42 | * ) |
||
43 | * @Hateoas\Relation( |
||
44 | * "previous", |
||
45 | * href = @Hateoas\Route( |
||
46 | * "expr(object.getRoute())", |
||
47 | * parameters = "expr(object.getParameters((object.getOffset() > object.getLimit()) ? object.getOffset() - object.getLimit() : 0))", |
||
48 | * absolute = "expr(object.isAbsolute())" |
||
49 | * ), |
||
50 | * exclusion = @Hateoas\Exclusion( |
||
51 | * excludeIf = "expr(! object.getOffset())" |
||
52 | * ) |
||
53 | * ) |
||
54 | * |
||
55 | * @author Premi Giorgio <[email protected]> |
||
56 | */ |
||
57 | class OffsetRepresentation extends AbstractSegmentedRepresentation |
||
58 | { |
||
59 | /** |
||
60 | * @var int |
||
61 | * |
||
62 | * @Serializer\Expose |
||
63 | * @Serializer\XmlAttribute |
||
64 | */ |
||
65 | private $offset; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $offsetParameterName; |
||
71 | |||
72 | /** |
||
73 | * @param CollectionRepresentation $inline |
||
74 | * @param string $route |
||
75 | * @param integer|null $offset |
||
76 | * @param integer $limit |
||
77 | * @param integer $total |
||
78 | */ |
||
79 | View Code Duplication | public function __construct( |
|
0 ignored issues
–
show
|
|||
80 | $inline, |
||
81 | $route, |
||
82 | array $parameters = array(), |
||
83 | $offset, |
||
84 | $limit, |
||
85 | $total = null, |
||
86 | $offsetParameterName = null, |
||
87 | $limitParameterName = null, |
||
88 | $absolute = false |
||
89 | ) { |
||
90 | parent::__construct($inline, $route, $parameters, $limit, $total, $limitParameterName, $absolute); |
||
91 | |||
92 | $this->offset = $offset; |
||
93 | $this->offsetParameterName = $offsetParameterName ?: 'offset'; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return int |
||
98 | */ |
||
99 | public function getOffset() |
||
100 | { |
||
101 | return $this->offset; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param null $offset |
||
106 | * @param null $limit |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getParameters($offset = null, $limit = null) |
||
110 | { |
||
111 | $parameters = parent::getParameters($limit); |
||
112 | |||
113 | unset($parameters[$this->offsetParameterName]); |
||
114 | |||
115 | if (null === $offset) { |
||
116 | $offset = $this->getOffset(); |
||
117 | } |
||
118 | |||
119 | if ($offset) { |
||
0 ignored issues
–
show
The expression
$offset of type integer|null is loosely compared to true ; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
Loading history...
|
|||
120 | $parameters[$this->offsetParameterName] = $offset; |
||
121 | $this->moveParameterToEnd($parameters, $this->getLimitParameterName()); |
||
122 | } |
||
123 | |||
124 | return $parameters; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getOffsetParameterName() |
||
131 | { |
||
132 | return $this->offsetParameterName; |
||
133 | } |
||
134 | } |
||
135 |
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.