1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CloudPlayDev\ConfluenceClient\Entity; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use CloudPlayDev\ConfluenceClient\Exception\HydrationException; |
8
|
|
|
use Webmozart\Assert\Assert; |
9
|
|
|
use function count; |
10
|
|
|
|
11
|
|
|
class ContentSearchResult implements Hydratable |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private int $size = 0; |
15
|
|
|
|
16
|
|
|
private int $start = 0; |
17
|
|
|
|
18
|
|
|
private int $limit = 0; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AbstractContent[] |
22
|
|
|
*/ |
23
|
|
|
private array $results = []; |
24
|
|
|
|
25
|
|
|
private bool $lastPage = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed[] $data |
29
|
|
|
* @return ContentSearchResult |
30
|
|
|
* @throws HydrationException |
31
|
|
|
*/ |
32
|
|
|
public static function load(array $data): self |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
$searchResult = new self; |
36
|
|
|
|
37
|
|
|
Assert::true(isset($data['results'], $data['size'])); |
38
|
|
|
Assert::isArray($data['results']); |
39
|
|
|
Assert::integer($data['size']); |
40
|
|
|
|
41
|
|
|
$searchResult->setSize($data['size']); |
42
|
|
|
|
43
|
|
|
if(isset($data['start']) && isset($data['limit'])) { |
44
|
|
|
Assert::integer($data['start']); |
45
|
|
|
Assert::integer($data['limit']); |
46
|
|
|
$searchResult->setStart($data['start']); |
47
|
|
|
$searchResult->setLimit($data['limit']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($data['size'] >= 1 && count($data['results']) >= 1) { |
51
|
|
|
|
52
|
|
|
foreach ($data['results'] as $resultEntity) { |
53
|
|
|
Assert::isArray($resultEntity); |
54
|
|
|
$content = AbstractContent::load($resultEntity); |
55
|
|
|
$searchResult->addResult($content); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* if there is a next link, then it is not the last page */ |
60
|
|
|
if(isset($data['_links']['next'])) { |
61
|
|
|
$searchResult->setLastPage(false); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $searchResult; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param AbstractContent[] $results |
69
|
|
|
* @return ContentSearchResult |
70
|
|
|
*/ |
71
|
|
|
public function setResults(array $results): ContentSearchResult |
72
|
|
|
{ |
73
|
|
|
$this->results = $results; |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int $size |
79
|
|
|
* @return ContentSearchResult |
80
|
|
|
*/ |
81
|
|
|
public function setSize(int $size): ContentSearchResult |
82
|
|
|
{ |
83
|
|
|
$this->size = $size; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function getSize(): int |
91
|
|
|
{ |
92
|
|
|
return $this->size; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return AbstractContent[] |
97
|
|
|
*/ |
98
|
|
|
public function getResults(): array |
99
|
|
|
{ |
100
|
|
|
return $this->results; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getResultAt(int $position): ?AbstractContent |
104
|
|
|
{ |
105
|
|
|
return $this->results[$position] ?? null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param AbstractContent $content |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
public function addResult(AbstractContent $content): self |
113
|
|
|
{ |
114
|
|
|
$this->results[] = $content; |
115
|
|
|
return $this; |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isLastPage(): bool |
120
|
|
|
{ |
121
|
|
|
return $this->lastPage; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setLastPage(bool $lastPage): void |
125
|
|
|
{ |
126
|
|
|
$this->lastPage = $lastPage; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getStart(): int |
130
|
|
|
{ |
131
|
|
|
return $this->start; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setStart(int $start): void |
135
|
|
|
{ |
136
|
|
|
$this->start = $start; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getLimit(): int |
140
|
|
|
{ |
141
|
|
|
return $this->limit; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function setLimit(int $limit): void |
145
|
|
|
{ |
146
|
|
|
$this->limit = $limit; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|