|
1
|
|
|
<?php namespace Neomerx\JsonApi\Encoder\Parameters; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use \Neomerx\JsonApi\Factories\Exceptions; |
|
20
|
|
|
use \Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @package Neomerx\JsonApi |
|
24
|
|
|
*/ |
|
25
|
|
|
class SortParameter implements SortParameterInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $sortField; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var null || string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sortRelationshipAttribute; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
private $isAscending; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $sortField |
|
45
|
|
|
* @param $isAscending |
|
46
|
|
|
* @param null $sortRelationAttribute |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
17 |
|
public function __construct($sortField, $isAscending, $sortRelationshipAttribute=null) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
17 |
|
is_string($sortField) === true ?: Exceptions::throwInvalidArgument('sortField', $sortField); |
|
51
|
17 |
|
is_string($sortRelationshipAttribute) === true || is_null($sortRelationshipAttribute) === true ?: Exceptions::throwInvalidArgument('sortRelationshipAttribute', $sortRelationshipAttribute); |
|
52
|
17 |
|
is_bool($isAscending) === true ?: Exceptions::throwInvalidArgument('isAscending', $isAscending); |
|
53
|
|
|
|
|
54
|
17 |
|
$this->sortField = $sortField; |
|
55
|
17 |
|
$this->sortRelationshipAttribute = $sortRelationshipAttribute; |
|
56
|
17 |
|
$this->isAscending = $isAscending; |
|
57
|
17 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function __toString() |
|
63
|
|
|
{ |
|
64
|
1 |
|
$prefix = $this->isAscending() ? '' : '-'; |
|
65
|
|
|
|
|
66
|
1 |
|
return $prefix . $this->getField(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @inheritdoc |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function getField() |
|
73
|
|
|
{ |
|
74
|
5 |
|
return $this->sortField; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return null || string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getRelationshipAttribute() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->sortRelationshipAttribute; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function isAscending() |
|
90
|
|
|
{ |
|
91
|
3 |
|
return $this->isAscending; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function isDescending() |
|
98
|
|
|
{ |
|
99
|
2 |
|
return !$this->isAscending; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.