Skip to content

Instantly share code, notes, and snippets.

@peterlafferty
Created September 14, 2017 21:16
Show Gist options
  • Select an option

  • Save peterlafferty/3ad1c51e296f09840399c2aac511531f to your computer and use it in GitHub Desktop.

Select an option

Save peterlafferty/3ad1c51e296f09840399c2aac511531f to your computer and use it in GitHub Desktop.
<?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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment