1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-08 14:16:46 +02:00

feat(rest-api): add ability to call macroable fetch methods in Entries Rest API #505

This commit is contained in:
Awilum
2020-12-16 15:05:49 +03:00
parent 657fad7525
commit 5f9430bb99

View File

@@ -11,6 +11,7 @@ namespace Flextype;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Http\Response;
use Atomastic\Arrays\Arrays;
use function array_replace_recursive;
use function count;
@@ -73,22 +74,24 @@ flextype()->get('/api/entries', function (Request $request, Response $response)
->write(flextype('json')->encode($api_errors['0003']));
}
switch ($from) {
case 'collection':
$response_data['data'] = flextype('entries')->fetchCollection($id, $options)->toArray();
break;
case 'single':
default:
$response_data['data'] = flextype('entries')->fetchSingle($id, $options)->toArray();
break;
}
flextype('registry')->set('flextype.settings.entries.fields.entries.fetchCollection.result', 'toArray');
flextype('registry')->set('flextype.settings.entries.fields.entries.fetchSingle.result', 'toArray');
$fetchFromCallbackMethodName = strings($from)
->studly()
->prepend('fetch')
->toString();
$fetchFromCallbackMethod = is_callable([flextype('entries'), $fetchFromCallbackMethodName]) ?
$fetchFromCallbackMethodName :
'fetchSingle';
// Get fetch result
$response_data['data'] = flextype('entries')->{$fetchFromCallbackMethod}($id, $options);
$response_data['data'] = ($response_data['data'] instanceof Arrays) ? $response_data['data']->toArray() : $response_data['data'];
// Set response code
if (is_array($response_data['data'])) {
$response_code = count($response_data['data']) > 0 ? 200 : 404;
} else {
$response_code = $response_data['data'] > 0 ? 200 : 404;
}
$response_code = count($response_data['data']) > 0 ? 200 : 404;
// Update calls counter
filesystem()->file($entries_token_file_path)->put(flextype('yaml')->encode(array_replace_recursive($entries_token_file_data, ['calls' => $entries_token_file_data['calls'] + 1])));