-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
77 lines (65 loc) · 1.87 KB
/
index.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
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
$app = new Silex\Application();
$app['emoji'] = json_decode('[
{
"id": 1,
"code": "U+1F600",
"value": "😀",
"description": "grinning face",
"hasSkinTone": false
},
{
"id": 2,
"code": "U+1F601",
"value": "😁",
"description": "grinning face with smiling eyes",
"hasSkinTone": false
},
{
"id": 99,
"code": "U+1F466",
"value": "👦",
"description": "boy",
"hasSkinTone": true
},
{
"id": 105,
"code": "U+1F467",
"value": "👧",
"description": "girl",
"hasSkinTone": true
}
]');
/* ignore this hackiness, it makes it simpler to set the headers and escape options*/
class Utf8JsonResponse extends JsonResponse
{
public function __construct($data = null, $status = 200, $headers = array(), $json = false)
{
$headers = array_merge(
["Content-Type" => "application/json; charset=utf-8"],
$headers
);
$this->encodingOptions = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE;
parent::__construct($data, $status, $headers, $json);
}
}
$app->get('/emoji/', function () use ($app) {
return new Utf8JsonResponse(["emoji" => $app['emoji']]);
});
$app->get('/emoji/{id}', function (int $id) use ($app) {
$filtered = array_filter($app['emoji'], function ($k) use ($id) {
return $k->id == $id;
});
if (empty($filtered)) {
return new Utf8JsonResponse(null, 404);
}
$response = new Utf8JsonResponse(array_pop($filtered));
return $response;
});
$app->post('/emoji/', function (Request $request) use ($app) {
return new Utf8JsonResponse(null, 204);
});
$app->run();