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 bool |
34
|
|
|
*/ |
35
|
|
|
private $isAscending; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $sortField |
39
|
|
|
* @param bool $isAscending |
40
|
|
|
*/ |
41
|
17 |
|
public function __construct($sortField, $isAscending) |
42
|
|
|
{ |
43
|
17 |
|
is_string($sortField) === true ?: Exceptions::throwInvalidArgument('sortField', $sortField); |
44
|
17 |
|
is_bool($isAscending) === true ?: Exceptions::throwInvalidArgument('isAscending', $isAscending); |
45
|
|
|
|
46
|
17 |
|
$this->sortField = $sortField; |
47
|
17 |
|
$this->isAscending = $isAscending; |
48
|
17 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function __toString() |
54
|
|
|
{ |
55
|
1 |
|
$prefix = $this->isAscending() ? '' : '-'; |
56
|
|
|
|
57
|
1 |
|
return $prefix . $this->getField(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritdoc |
62
|
|
|
*/ |
63
|
5 |
|
public function getField() |
64
|
|
|
{ |
65
|
5 |
|
return $this->sortField; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
3 |
|
public function isAscending() |
72
|
|
|
{ |
73
|
3 |
|
return $this->isAscending; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
2 |
|
public function isDescending() |
80
|
|
|
{ |
81
|
2 |
|
return !$this->isAscending; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|