Skip to content

Commit 91f3dae

Browse files
committed
Initial Search code
1 parent 2b5cdd0 commit 91f3dae

File tree

2 files changed

+475
-0
lines changed

2 files changed

+475
-0
lines changed

src/Libraries/Search/Results.php

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
<?php
2+
3+
namespace BNETDocs\Libraries\Search;
4+
5+
use \BNETDocs\Libraries\Comment;
6+
use \BNETDocs\Libraries\Document;
7+
use \BNETDocs\Libraries\News\Post as NewsPost;
8+
use \BNETDocs\Libraries\Packet\Packet;
9+
use \BNETDocs\Libraries\Server\Server;
10+
use \BNETDocs\Libraries\User\User;
11+
use \UnexpectedValueException;
12+
13+
class Results implements \JsonSerializable
14+
{
15+
private array $comments = [];
16+
private array $documents = [];
17+
private array $news_posts = [];
18+
private array $packets = [];
19+
private array $servers = [];
20+
private array $users = [];
21+
22+
public function addComment(Comment $value, bool $check_for_duplicate = true): void
23+
{
24+
if (!$value || is_null($value->getId()))
25+
{
26+
throw new UnexpectedValueException('Comment id cannot be null');
27+
}
28+
29+
if ($check_for_duplicate)
30+
{
31+
foreach ($this->comments as $existing)
32+
{
33+
if ($existing === $value) return;
34+
}
35+
}
36+
37+
$this->comments[] = $value;
38+
}
39+
40+
public function addDocument(Document $value, bool $check_for_duplicate = true): void
41+
{
42+
if (!$value || is_null($value->getId()))
43+
{
44+
throw new UnexpectedValueException('Document id cannot be null');
45+
}
46+
47+
if ($check_for_duplicate)
48+
{
49+
foreach ($this->documents as $existing)
50+
{
51+
if ($existing === $value) return;
52+
}
53+
}
54+
55+
$this->documents[] = $value;
56+
}
57+
58+
public function addNewsPost(NewsPost $value, bool $check_for_duplicate = true): void
59+
{
60+
if (!$value || is_null($value->getId()))
61+
{
62+
throw new UnexpectedValueException('NewsPost id cannot be null');
63+
}
64+
65+
if ($check_for_duplicate)
66+
{
67+
foreach ($this->news_posts as $existing)
68+
{
69+
if ($existing === $value) return;
70+
}
71+
}
72+
73+
$this->news_posts[] = $value;
74+
}
75+
76+
public function addPacket(Packet $value, bool $check_for_duplicate = true): void
77+
{
78+
if (!$value || is_null($value->getId()))
79+
{
80+
throw new UnexpectedValueException('Packet id cannot be null');
81+
}
82+
83+
if ($check_for_duplicate)
84+
{
85+
foreach ($this->packets as $existing)
86+
{
87+
if ($existing === $value) return;
88+
}
89+
}
90+
91+
$this->packets[] = $value;
92+
}
93+
94+
public function addServer(Server $value, bool $check_for_duplicate = true): void
95+
{
96+
if (!$value || is_null($value->getId()))
97+
{
98+
throw new UnexpectedValueException('Server id cannot be null');
99+
}
100+
101+
if ($check_for_duplicate)
102+
{
103+
foreach ($this->servers as $existing)
104+
{
105+
if ($existing === $value) return;
106+
}
107+
}
108+
109+
$this->servers[] = $value;
110+
}
111+
112+
public function addUser(User $value, bool $check_for_duplicate = true): void
113+
{
114+
if (!$value || is_null($value->getId()))
115+
{
116+
throw new UnexpectedValueException('Server id cannot be null');
117+
}
118+
119+
if ($check_for_duplicate)
120+
{
121+
foreach ($this->users as $existing)
122+
{
123+
if ($existing === $value) return;
124+
}
125+
}
126+
127+
$this->users[] = $value;
128+
}
129+
130+
public function getComments(): array
131+
{
132+
return $this->comments;
133+
}
134+
135+
public function getDocuments(): array
136+
{
137+
return $this->documents;
138+
}
139+
140+
public function getNewsPosts(): array
141+
{
142+
return $this->news_posts;
143+
}
144+
145+
public function getPackets(): array
146+
{
147+
return $this->packets;
148+
}
149+
150+
public function getServers(): array
151+
{
152+
return $this->servers;
153+
}
154+
155+
public function getUsers(): array
156+
{
157+
return $this->users;
158+
}
159+
160+
public function jsonSerialize(): mixed
161+
{
162+
return [
163+
'comments' => $this->comments,
164+
'documents' => $this->documents,
165+
'news_posts' => $this->news_posts,
166+
'packets' => $this->packets,
167+
'servers' => $this->servers,
168+
'users' => $this->users,
169+
];
170+
}
171+
172+
public function removeComment(Comment $value): bool
173+
{
174+
foreach ($this->comments as $k => &$v)
175+
{
176+
if ($v === $value)
177+
{
178+
unset($k);
179+
return true;
180+
}
181+
}
182+
return false;
183+
}
184+
185+
public function removeDocument(Document $value): bool
186+
{
187+
foreach ($this->documents as $k => &$v)
188+
{
189+
if ($v === $value)
190+
{
191+
unset($k);
192+
return true;
193+
}
194+
}
195+
return false;
196+
}
197+
198+
public function removeNewsPost(NewsPost $value): bool
199+
{
200+
foreach ($this->news_posts as $k => &$v)
201+
{
202+
if ($v === $value)
203+
{
204+
unset($k);
205+
return true;
206+
}
207+
}
208+
return false;
209+
}
210+
211+
public function removePacket(Packet $value): bool
212+
{
213+
foreach ($this->packets as $k => &$v)
214+
{
215+
if ($v === $value)
216+
{
217+
unset($k);
218+
return true;
219+
}
220+
}
221+
return false;
222+
}
223+
224+
public function removeServer(Server $value): bool
225+
{
226+
foreach ($this->servers as $k => &$v)
227+
{
228+
if ($v === $value)
229+
{
230+
unset($k);
231+
return true;
232+
}
233+
}
234+
return false;
235+
}
236+
237+
public function removeUser(User $value): bool
238+
{
239+
foreach ($this->users as $k => &$v)
240+
{
241+
if ($v === $value)
242+
{
243+
unset($k);
244+
return true;
245+
}
246+
}
247+
return false;
248+
}
249+
250+
public function setComments(array $value): void
251+
{
252+
foreach ($value as $values)
253+
{
254+
if (!$values instanceof Comment)
255+
{
256+
throw new UnexpectedValueException('Expected Comment object, got something else');
257+
}
258+
}
259+
$this->comments = $value;
260+
}
261+
262+
public function setDocuments(array $value): void
263+
{
264+
foreach ($value as $values)
265+
{
266+
if (!$values instanceof Document)
267+
{
268+
throw new UnexpectedValueException('Expected Document object, got something else');
269+
}
270+
}
271+
$this->documents = $value;
272+
}
273+
274+
public function setNewsPosts(array $value): void
275+
{
276+
foreach ($value as $values)
277+
{
278+
if (!$values instanceof NewsPost)
279+
{
280+
throw new UnexpectedValueException('Expected NewsPost object, got something else');
281+
}
282+
}
283+
$this->news_posts = $value;
284+
}
285+
286+
public function setPackets(array $value): void
287+
{
288+
foreach ($value as $values)
289+
{
290+
if (!$values instanceof Packet)
291+
{
292+
throw new UnexpectedValueException('Expected Packet object, got something else');
293+
}
294+
}
295+
$this->packets = $value;
296+
}
297+
298+
public function setServers(array $value): void
299+
{
300+
foreach ($value as $values)
301+
{
302+
if (!$values instanceof Server)
303+
{
304+
throw new UnexpectedValueException('Expected Server object, got something else');
305+
}
306+
}
307+
$this->servers = $value;
308+
}
309+
310+
public function setUsers(array $value): void
311+
{
312+
foreach ($value as $values)
313+
{
314+
if (!$values instanceof User)
315+
{
316+
throw new UnexpectedValueException('Expected User object, got something else');
317+
}
318+
}
319+
$this->users = $value;
320+
}
321+
}

0 commit comments

Comments
 (0)