-
Notifications
You must be signed in to change notification settings - Fork 3
/
SearchEndpoint.php
117 lines (101 loc) · 3.1 KB
/
SearchEndpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace NklKst\TheSportsDb\Client\Endpoint;
use Exception;
use NklKst\TheSportsDb\Entity\Event\Event;
use NklKst\TheSportsDb\Entity\Player\Player;
use NklKst\TheSportsDb\Entity\Team;
use NklKst\TheSportsDb\Filter\SearchFilter;
/**
* Search endpoints.
*
* @author Nikolai Keist (github.com/nkl-kst)
*/
class SearchEndpoint extends AbstractEndpoint
{
private const ENDPOINT_EVENTS = 'searchevents.php';
private const ENDPOINT_FILE = 'searchfilename.php';
private const ENDPOINT_PLAYERS = 'searchplayers.php';
private const ENDPOINT_TEAMS = 'searchteams.php';
/**
* Find events by query, optionally filtered by season.
*
* @param string $eventQuery Event query
* @param ?string $season Season filter
*
* @return Event[] Found events
*
* @throws Exception
*/
public function events(string $eventQuery, ?string $season = null): array
{
$filter = (new SearchFilter())->setEventQuery($eventQuery);
if ($season) {
$filter->setSeason($season);
}
$this
->setFilter($filter)
->requestBuilder->setEndpoint(self::ENDPOINT_EVENTS);
return $this->serializer->serializeEvents($this->request());
}
/**
* Find event by file name.
*
* @param string $file File name
*
* @throws Exception
*/
public function eventFile(string $file): ?Event
{
$this
->setFilter((new SearchFilter())->setEventQuery($file))
->requestBuilder->setEndpoint(self::ENDPOINT_FILE);
return $this->getSingleEntity($this->serializer->serializeEvents($this->request()));
}
/**
* Find players, optionally filtered by player or team query.
*
* @param ?string $playerQuery Player query
* @param ?string $teamQuery Team query
*
* @return Player[] Found players
*
* @throws Exception
*/
public function players(?string $playerQuery = null, ?string $teamQuery = null): array
{
$filter = new SearchFilter();
if ($playerQuery) {
$filter->setPlayerQuery($playerQuery);
}
if ($teamQuery) {
$filter->setTeamQuery($teamQuery);
}
$this
->setFilter($filter)
->requestBuilder->setEndpoint(self::ENDPOINT_PLAYERS);
return $this->serializer->serializePlayers($this->request());
}
/**
* Find teams by name or shortname query.
*
* @param string $teamQuery Name or shortname query
* @param bool $isShortQuery Query by shortname
*
* @return Team[] Found teams
*
* @throws Exception
*/
public function teams(string $teamQuery, bool $isShortQuery = false): array
{
$filter = new SearchFilter();
if ($isShortQuery) {
$filter->setTeamShortQuery($teamQuery);
} else {
$filter->setTeamQuery($teamQuery);
}
$this
->setFilter($filter)
->requestBuilder->setEndpoint(self::ENDPOINT_TEAMS);
return $this->serializer->serializeTeams($this->request());
}
}