jikan-rest/app/Services/DefaultPrivateFieldMapperService.php
pushrbx fbc3b8277d wip - more fixes
- added user animelist/mangalist endpoints back
- fixed issues with the container image
- improved club model factory
- fixed ordering while searching when search engine is disabled (mongodb based search)
2023-05-21 11:18:02 +01:00

35 lines
892 B
PHP

<?php
namespace App\Services;
use Spatie\Enum\Laravel\Enum;
use Spatie\LaravelData\Optional;
final class DefaultPrivateFieldMapperService implements PrivateFieldMapperService
{
public function map($instance, array $values): mixed
{
$cls = get_class($instance);
foreach ($values as $fieldName => $fieldValue) {
if ($fieldValue instanceof Optional) {
continue;
}
if ($fieldValue instanceof Enum) {
$fieldValue = $fieldValue->label;
}
if (!property_exists($cls, $fieldName)) {
continue;
}
$reflection = new \ReflectionProperty($cls, $fieldName);
// note: ->setAccessible call would be required under php version 8.1
$reflection->setValue($instance, $fieldValue);
}
return $instance;
}
}