1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

feat(fields): improve EntriesField logic

This commit is contained in:
Awilum
2022-04-07 14:47:33 +03:00
parent 7ca7efdbf4
commit 2ba37a2efa

View File

@@ -14,14 +14,14 @@ declare(strict_types=1);
* Redistributions of files must retain the above copyright notice.
*/
use Glowy\Arrays\Arrays;
use Glowy\Arrays\Arrays as Collection;
emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void {
dump(entries()->registry()->get('methods.fetch.collection'));
if (! entries()->registry()->get('methods.fetch.collection.fields.entries.enabled')) {
return;
}
if (entries()->registry()->has('methods.fetch.result.entries.fetch')) {
// Get
@@ -34,35 +34,34 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
break;
case 'toObject':
default:
$resultTo = 'copy';
break;
default:
if (entries()->registry()->has('methods.fetch.collection.fields.entries.fetch.result')) {
if (in_array(entries()->registry()->get('methods.fetch.collection.fields.entries.fetch.result'), ['toArray', 'toObject'])) {
$resultTo = entries()->registry()->get('methods.fetch.collection.fields.entries.fetch.result');
if ($resultTo == 'toObject') {
$resultTo = 'copy';
}
}
} else {
$resultTo = 'copy';
}
break;
}
// Modify
foreach (entries()->registry()->get('methods.fetch.result.entries.fetch') as $field => $body) {
if (isset($body['options']['method']) &&
strpos($body['options']['method'], 'fetch') !== false &&
is_callable([entries(), $body['options']['method']])) {
$fetchFromCallbackMethod = $body['options']['method'];
} else {
$fetchFromCallbackMethod = 'fetch';
}
$result = isset($body['result']) && in_array($body['result'], ['toArray', 'toObject']) ? $body['result'] : $resultTo;
$data[$field] = entries()->{$fetchFromCallbackMethod}($body['id'],
isset($body['options']) ?
$body['options'] :
[]);
$data[$field] = ($data[$field] instanceof Arrays) ? $data[$field]->{$result}() : $data[$field];
$data[$field] = entries()->fetch($body['id'], isset($body['options']) ? $body['options'] : []);
$data[$field] = ($data[$field] instanceof Collection) ? $data[$field]->{$result}() : $data[$field];
}
$result = collection($original['result'])->merge($data)->toArray();
// Remove entries field
if (boolval(entries()->registry()->get('methods.fetch.collection.fields.entries.dump')) === false) {
unset($result['entries']);
}
@@ -71,6 +70,5 @@ emitter()->addListener('onEntriesFetchSingleHasResult', static function (): void
entries()->registry()->set('methods.fetch.params.id', $original['params']['id']);
entries()->registry()->set('methods.fetch.params.options', $original['params']['options']);
entries()->registry()->set('methods.fetch.result', $result);
}
});