1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soheilrt\AdobeConnectClient\Client; |
4
|
|
|
|
5
|
|
|
use Soheilrt\AdobeConnectClient\Client\Contracts\ArrayableInterface; |
6
|
|
|
use Soheilrt\AdobeConnectClient\Client\Helpers\StringCaseTransform as SCT; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Create valid sort using Fluent Interface. |
10
|
|
|
* |
11
|
|
|
* See {@link https://helpx.adobe.com/content/help/en/adobe-connect/webservices/sort-definition.html} |
12
|
|
|
*/ |
13
|
|
|
class Sorter implements ArrayableInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $sorts = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Prefix to use in sorts. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $prefix = 'sort'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return a new Sorter instance. |
29
|
|
|
* |
30
|
|
|
* @return Sorter |
31
|
|
|
*/ |
32
|
|
|
public static function instance(): Sorter |
33
|
|
|
{ |
34
|
|
|
return new static(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add an ASC sort. |
39
|
|
|
* |
40
|
|
|
* @param string $field |
41
|
|
|
* |
42
|
|
|
* @return Sorter |
43
|
|
|
*/ |
44
|
|
|
public function asc($field): Sorter |
45
|
|
|
{ |
46
|
|
|
$this->sorts[SCT::toHyphen($field)] = 'asc'; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add a DESC sort. |
53
|
|
|
* |
54
|
|
|
* @param string $field |
55
|
|
|
* |
56
|
|
|
* @return Sorter |
57
|
|
|
*/ |
58
|
|
|
public function desc($field): Sorter |
59
|
|
|
{ |
60
|
|
|
$this->sorts[SCT::toHyphen($field)] = 'desc'; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove item to sort. |
67
|
|
|
* |
68
|
|
|
* @param string $field |
69
|
|
|
* |
70
|
|
|
* @return Sorter |
71
|
|
|
*/ |
72
|
|
|
public function removeField($field): Sorter |
73
|
|
|
{ |
74
|
|
|
$field = SCT::toHyphen($field); |
75
|
|
|
|
76
|
|
|
if (isset($this->sorts[$field])) { |
77
|
|
|
unset($this->sorts[$field]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieves all not null attributes in an associative array. |
85
|
|
|
* |
86
|
|
|
* The keys in hash style: Ex: is-member |
87
|
|
|
* The values as string |
88
|
|
|
* |
89
|
|
|
* @return string[] |
90
|
|
|
*/ |
91
|
|
|
public function toArray(): array |
92
|
|
|
{ |
93
|
|
|
if (count($this->sorts) === 1) { |
94
|
|
|
$order = reset($this->sorts); |
95
|
|
|
$field = key($this->sorts); |
96
|
|
|
|
97
|
|
|
return [$this->prefix . '-' . SCT::toHyphen($field) => $order]; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$sorts = []; |
101
|
|
|
$i = 1; |
102
|
|
|
|
103
|
|
|
foreach (array_slice($this->sorts, 0, 2) as $field => $order) { |
104
|
|
|
$sorts[$this->prefix . $i . '-' . SCT::toHyphen($field)] = $order; |
105
|
|
|
$i++; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $sorts; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|