|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Controller\Endpoint\Search; |
|
4
|
|
|
|
|
5
|
|
|
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController; |
|
6
|
|
|
use Ps2alerts\Api\Repository\Metrics\OutfitTotalRepository; |
|
7
|
|
|
use Ps2alerts\Api\Repository\Metrics\PlayerTotalRepository; |
|
8
|
|
|
use Ps2alerts\Api\Transformer\Search\OutfitSearchTransformer; |
|
9
|
|
|
use Ps2alerts\Api\Transformer\Search\PlayerSearchTransformer; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
class SearchEndpointController extends AbstractEndpointController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Construct |
|
17
|
|
|
* |
|
18
|
|
|
* @param OutfitSearchTransformer $outfitSearchTransformer |
|
19
|
|
|
* @param OutfitTotalRepository $outfitTotalRepo |
|
20
|
|
|
* @param PlayerSearchTransformer $playerSearchTransformer |
|
21
|
|
|
* @param PlayerTotalRepository $playerTotalRepo |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct( |
|
24
|
|
|
OutfitTotalRepository $outfitTotalRepo, |
|
25
|
|
|
PlayerTotalRepository $playerTotalRepo, |
|
26
|
|
|
OutfitSearchTransformer $outfitSearchTransformer, |
|
27
|
|
|
PlayerSearchTransformer $playerSearchTransformer |
|
28
|
|
|
) { |
|
29
|
|
|
$this->playerRepository = $playerTotalRepo; |
|
|
|
|
|
|
30
|
|
|
$this->outfitRepository = $outfitTotalRepo; |
|
|
|
|
|
|
31
|
|
|
$this->playerSearchTransformer = $playerSearchTransformer; |
|
|
|
|
|
|
32
|
|
|
$this->outfitSearchTransformer = $outfitSearchTransformer; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Endpoint to return potential players based on search term |
|
37
|
|
|
* |
|
38
|
|
|
* @param ServerRequestInterface $request |
|
39
|
|
|
* @param ResponseInterface $response |
|
40
|
|
|
* @param array $args |
|
41
|
|
|
* |
|
42
|
|
|
* @return ResponseInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getPlayersByTerm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
// If a valid player name we're searching on |
|
47
|
|
|
$this->parsePlayerName($args['term']); |
|
48
|
|
|
$players = $this->searchForPlayer($args['term']); |
|
49
|
|
|
|
|
50
|
|
|
if (! empty($players)) { |
|
51
|
|
|
return $this->respond( |
|
52
|
|
|
'collection', |
|
53
|
|
|
$players, |
|
54
|
|
|
$this->playerSearchTransformer |
|
|
|
|
|
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->respondWithError('Player could not be found', self::CODE_EMPTY); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Endpoint to return potential players based on search term |
|
63
|
|
|
* |
|
64
|
|
|
* @param ServerRequestInterface $request |
|
65
|
|
|
* @param ResponseInterface $response |
|
66
|
|
|
* @param array $args |
|
67
|
|
|
* |
|
68
|
|
|
* @return ResponseInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getOutfitsByTerm(ServerRequestInterface $request, ResponseInterface $response, array $args) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$name = urldecode($args['term']); // Spaces will have to URL encoded |
|
73
|
|
|
|
|
74
|
|
|
$this->parseOutfitName($name); |
|
75
|
|
|
|
|
76
|
|
|
if (! empty($outfits)) { |
|
|
|
|
|
|
77
|
|
|
return $this->respond( |
|
78
|
|
|
'collection', |
|
79
|
|
|
$outfits, |
|
80
|
|
|
$this->outfitSearchTransformer |
|
|
|
|
|
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->respondWithError('Outfit could not be found', self::CODE_EMPTY); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Takes a player name and searches for it |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $term |
|
91
|
|
|
* |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function searchForPlayer($term) |
|
95
|
|
|
{ |
|
96
|
|
|
$query = $this->playerRepository->newQuery(); |
|
|
|
|
|
|
97
|
|
|
$query->cols(['*']); |
|
98
|
|
|
$query->where('playerName LIKE :term'); |
|
99
|
|
|
$query->bindValue('term', "%{$term}%"); |
|
100
|
|
|
|
|
101
|
|
|
return $this->playerRepository->fireStatementAndReturn($query); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Takes a outfit name and searches for it |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $term |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function searchForOutfit($term) |
|
112
|
|
|
{ |
|
113
|
|
|
$query = $this->outfitRepository->newQuery(); |
|
|
|
|
|
|
114
|
|
|
$query->cols(['*']); |
|
115
|
|
|
$query->where("outfitTag LIKE :term"); |
|
116
|
|
|
$query->bindValue('term', "%{$term}%"); |
|
117
|
|
|
|
|
118
|
|
|
$data = $this->outfitRepository->fireStatementAndReturn($query); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
if (empty($data)) { |
|
121
|
|
|
$query = $this->outfitRepository->newQuery(); |
|
|
|
|
|
|
122
|
|
|
$query->cols(['*']); |
|
123
|
|
|
$query->where("outfitName LIKE :term"); |
|
124
|
|
|
$query->bindValue('term', "%{$term}%"); |
|
125
|
|
|
|
|
126
|
|
|
$data = $this->outfitRepository->fireStatementAndReturn($query); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $data; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Parses a player name and makes sure it's valid |
|
134
|
|
|
* |
|
135
|
|
|
* @param String $name |
|
136
|
|
|
* |
|
137
|
|
|
* @return ResponseInterface|boolean |
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
public function parsePlayerName($name) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
if (empty($name)) { |
|
142
|
|
|
return $this->respondWithError('Player name needs to be present.', self::CODE_WRONG_ARGS); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
if (strlen($name) > 24) { |
|
146
|
|
|
return $this->respondWithError('Player names cannot be longer than 24 characters.', self::CODE_WRONG_ARGS); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return true; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Parses a outfit name and makes sure it's valid |
|
154
|
|
|
* |
|
155
|
|
|
* @param String $name |
|
156
|
|
|
* |
|
157
|
|
|
* @return ResponseInterface|boolean |
|
158
|
|
|
*/ |
|
159
|
|
View Code Duplication |
public function parseOutfitName($name) |
|
|
|
|
|
|
160
|
|
|
{ |
|
161
|
|
|
if (empty($name)) { |
|
162
|
|
|
return $this->respondWithError('Outfit name needs to be present.', self::CODE_WRONG_ARGS); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
if (strlen($name) > 32) { |
|
166
|
|
|
return $this->respondWithError('Outfit names cannot be longer than 32 characters.', self::CODE_WRONG_ARGS); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Runs checks on the player ID |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $id |
|
176
|
|
|
* |
|
177
|
|
|
* @return ResponseInterface|boolean |
|
178
|
|
|
*/ |
|
179
|
|
|
public function parsePlayerID($id) |
|
180
|
|
|
{ |
|
181
|
|
|
if (empty($id)) { |
|
182
|
|
|
return $this->respondWithError('Player ID needs to be present.', self::CODE_WRONG_ARGS); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (strlen($id > 19)) { |
|
186
|
|
|
return $this->respondWithError('Player ID cannot be longer than 19 characters.', self::CODE_WRONG_ARGS); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
if (! is_numeric($id)) { |
|
190
|
|
|
return $this->respondWithError('Player ID must be numeric.', self::CODE_WRONG_ARGS); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.