|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Temitope Olotin <[email protected]> |
|
4
|
|
|
* @license <https://opensource.org/license/MIT> MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Laztopaz\EmojiRestfulAPI; |
|
7
|
|
|
|
|
8
|
|
|
use Firebase\JWT\JWT; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
12
|
|
|
|
|
13
|
|
|
class EmojiController |
|
14
|
|
|
{ |
|
15
|
|
|
private $auth; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Oauth $auth) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->auth = $auth; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* This method list all emojis. |
|
24
|
|
|
* |
|
25
|
|
|
* @param $response |
|
26
|
|
|
* |
|
27
|
|
|
* @return json $emojis |
|
28
|
|
|
*/ |
|
29
|
|
|
public function listAllEmoji(Response $response) |
|
30
|
|
|
{ |
|
31
|
|
|
$emojis = Emoji::with('keywords', 'category', 'created_by')->get(); |
|
32
|
|
|
$emojis = $emojis->toArray(); |
|
33
|
|
|
|
|
34
|
|
|
if (count($emojis) > 0) { |
|
35
|
|
|
return $response |
|
36
|
|
|
->withJson($this->formatEmoji($emojis), 200); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $response->withJson(['status'], 404); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* This method get a single emoji. |
|
44
|
|
|
* |
|
45
|
|
|
* @param $response |
|
46
|
|
|
* @param $args |
|
47
|
|
|
* |
|
48
|
|
|
* @return json $emoji |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getSingleEmoji(Response $response, $args) |
|
51
|
|
|
{ |
|
52
|
|
|
$id = $args['id']; |
|
53
|
|
|
|
|
54
|
|
|
$emoji = Emoji::where('id', '=', $id)->with('keywords', 'category', 'created_by')->get(); |
|
55
|
|
|
$emoji = $emoji->toArray(); |
|
56
|
|
|
|
|
57
|
|
|
if (count($emoji) > 0) { |
|
58
|
|
|
return $response |
|
59
|
|
|
->withJson($this->formatEmoji($emoji), 200); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $response->withJson(['message' => 'Emoji not found'], 404); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* This method creates a new emoji. |
|
67
|
|
|
* |
|
68
|
|
|
* @param $args |
|
69
|
|
|
* |
|
70
|
|
|
* @return json $response; |
|
71
|
|
|
*/ |
|
72
|
|
|
public function createEmoji(Request $request, Response $response) |
|
73
|
|
|
{ |
|
74
|
|
|
$requestParams = $request->getParsedBody(); |
|
75
|
|
|
|
|
76
|
|
|
$emojiKeyword = $requestParams['keywords']; |
|
77
|
|
|
|
|
78
|
|
|
$userId = $this->getCurrentUserId($request, $response); |
|
79
|
|
|
|
|
80
|
|
|
if (is_array($requestParams)) { |
|
81
|
|
|
$created_at = date('Y-m-d h:i:s'); |
|
82
|
|
|
|
|
83
|
|
|
if (! $this->checkForDuplicateEmoji($requestParams['name'])) { |
|
84
|
|
|
$emoji = Emoji::create( |
|
85
|
|
|
[ |
|
86
|
|
|
'name' => $requestParams['name'], |
|
87
|
|
|
'char' => $requestParams['char'], |
|
88
|
|
|
'created_at' => $created_at, |
|
89
|
|
|
'category' => $requestParams['category'], |
|
90
|
|
|
'created_by' => $userId, |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
|
|
if ($emoji->id) { |
|
|
|
|
|
|
94
|
|
|
$createdKeyword = $this->createEmojiKeywords($emoji->id, $emojiKeyword); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $response->withJson($emoji->toArray(), 201); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $response->withJson(['message' => 'Emoji cannot be duplicated'], 400); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* This method updates an emoji. |
|
106
|
|
|
* |
|
107
|
|
|
* @param $request |
|
108
|
|
|
* @param $response |
|
109
|
|
|
* |
|
110
|
|
|
* @return json |
|
111
|
|
|
*/ |
|
112
|
|
|
public function updateEmojiByPutVerb(Request $request, Response $response, $args) |
|
113
|
|
|
{ |
|
114
|
|
|
$upateParams = $request->getParsedBody(); |
|
115
|
|
|
|
|
116
|
|
|
if (is_array($upateParams)) { |
|
117
|
|
|
$id = $args['id']; |
|
118
|
|
|
|
|
119
|
|
|
$emoji = Emoji::find($id); |
|
120
|
|
|
|
|
121
|
|
|
if ($emoji->id) { |
|
122
|
|
|
$emoji->name = $upateParams['name']; |
|
123
|
|
|
$emoji->char = $upateParams['char']; |
|
124
|
|
|
$emoji->category = $upateParams['category']; |
|
125
|
|
|
$emoji->updated_at = date('Y-m-d h:i:s'); |
|
126
|
|
|
$emoji->save(); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
return $response->withJson(['message' => 'Record updated successfully'], 201); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $response->withJson(['message' => 'Record cannot be updated'], 404); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* This method updates an emoji partially. |
|
137
|
|
|
* |
|
138
|
|
|
* @param $request |
|
139
|
|
|
* @param $response |
|
140
|
|
|
* |
|
141
|
|
|
* @return json |
|
142
|
|
|
*/ |
|
143
|
|
|
public function updateEmojiByPatchVerb(Request $request, Response $response, $args) |
|
144
|
|
|
{ |
|
145
|
|
|
$upateParams = $request->getParsedBody(); |
|
146
|
|
|
|
|
147
|
|
|
if (is_array($upateParams)) { |
|
148
|
|
|
$id = $args['id']; |
|
149
|
|
|
|
|
150
|
|
|
$emoji = Emoji::find($id); |
|
151
|
|
|
if ($emoji->id) { |
|
152
|
|
|
$emoji->name = $upateParams['name']; |
|
153
|
|
|
$emoji->updated_at = date('Y-m-d h:i:s'); |
|
154
|
|
|
$emoji->save(); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
return $response->withJson($emoji->toArray(), 201); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $response->withJson(['message' => 'No record to update'], 404); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* This method deletes an emoji. |
|
165
|
|
|
* |
|
166
|
|
|
* @param $request |
|
167
|
|
|
* @param $response |
|
168
|
|
|
* @param $args |
|
169
|
|
|
* |
|
170
|
|
|
* @return json |
|
171
|
|
|
*/ |
|
172
|
|
|
public function deleteEmoji(Request $request, Response $response, $args) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
$id = $args['id']; |
|
175
|
|
|
|
|
176
|
|
|
$emoji = Emoji::find($id); |
|
177
|
|
|
if ($emoji->id) { |
|
178
|
|
|
$emoji->delete(); |
|
|
|
|
|
|
179
|
|
|
// Delete keywords associated with the emoji |
|
180
|
|
|
Keyword::where('emoji_id', '=', $id)->delete(); |
|
181
|
|
|
|
|
182
|
|
|
return $response->withJson(['message' => 'Emoji was sucessfully deleted'], 204); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $response->withJson(['message' => 'Emoji cannot be deleted'], 404); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* This method creates emoji keywords. |
|
190
|
|
|
* |
|
191
|
|
|
* @param $request |
|
192
|
|
|
* @param $response |
|
193
|
|
|
* @param $args |
|
194
|
|
|
* |
|
195
|
|
|
* @return $id |
|
|
|
|
|
|
196
|
|
|
*/ |
|
197
|
|
View Code Duplication |
public function createEmojiKeywords($emoji_id, $keywords) |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
if ($keywords) { |
|
200
|
|
|
$splittedKeywords = explode(',', $keywords); |
|
201
|
|
|
|
|
202
|
|
|
$created_at = date('Y-m-d h:i:s'); |
|
203
|
|
|
|
|
204
|
|
|
foreach ($splittedKeywords as $keyword) { |
|
205
|
|
|
$emojiKeyword = Keyword::create([ |
|
206
|
|
|
'emoji_id' => $emoji_id, |
|
207
|
|
|
'keyword_name' => $keyword, |
|
208
|
|
|
'created_at' => $created_at, |
|
209
|
|
|
]); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $emojiKeyword->id; |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* This method format emoji result |
|
218
|
|
|
* |
|
219
|
|
|
* @param $emojis |
|
220
|
|
|
* |
|
221
|
|
|
* @return array $emojis |
|
222
|
|
|
*/ |
|
223
|
|
|
public function formatEmoji(array $emojis) |
|
224
|
|
|
{ |
|
225
|
|
|
foreach ($emojis as $key => &$value) { |
|
226
|
|
|
$value['created_by'] = $value['created_by']['firstname'].' '.$value['created_by']['lastname']; |
|
227
|
|
|
$value['category'] = $value['category']['category_name']; |
|
228
|
|
|
$value['keywords'] = array_map(function ($key) { return $key['keyword_name']; }, $value['keywords']); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $emojis; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* This method authenticate and return user id. |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getCurrentUserId($request, $response) |
|
238
|
|
|
{ |
|
239
|
|
|
$loadEnv = DatabaseConnection::loadEnv(); |
|
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
$jwtoken = $request->getHeader('HTTP_AUTHORIZATION'); |
|
242
|
|
|
|
|
243
|
|
|
try { |
|
244
|
|
|
if (isset($jwtoken)) { |
|
245
|
|
|
$secretKey = base64_decode(getenv('secret')); |
|
246
|
|
|
|
|
247
|
|
|
$jwt = json_decode($jwtoken[0], true); |
|
248
|
|
|
|
|
249
|
|
|
//decode the JWT using the key from config |
|
250
|
|
|
$decodedToken = JWT::decode($jwt['jwt'], $secretKey, ['HS512']); |
|
251
|
|
|
|
|
252
|
|
|
$tokenInfo = (array) $decodedToken; |
|
253
|
|
|
|
|
254
|
|
|
$userInfo = (array) $tokenInfo['dat']; |
|
255
|
|
|
|
|
256
|
|
|
return $userInfo['id']; |
|
257
|
|
|
} |
|
258
|
|
|
} catch (\Exception $e) { |
|
259
|
|
|
return $response->withJson(['status' => $e->getMessage()], 401); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* This method checks for duplicate emoji |
|
265
|
|
|
* |
|
266
|
|
|
* @param $name |
|
267
|
|
|
* |
|
268
|
|
|
* @return boolean true |
|
269
|
|
|
*/ |
|
270
|
|
|
public function checkForDuplicateEmoji($emojiName) |
|
271
|
|
|
{ |
|
272
|
|
|
if (isset($emojiName)) { |
|
273
|
|
|
$emojiFound = Capsule::table('emojis') |
|
274
|
|
|
->where('name', '=', strtoupper($emojiName)) |
|
275
|
|
|
->orWhere('name', '=', strtolower($emojiName)) |
|
276
|
|
|
->orWhere('name', '=', ucwords($emojiName)) |
|
277
|
|
|
->orWhere('name', '=', $emojiName) |
|
278
|
|
|
->get(); |
|
279
|
|
|
|
|
280
|
|
|
if (count($emojiFound) > 0) { |
|
281
|
|
|
return true; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
return false; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.