VSVverkeerskunde/gvq-api

View on GitHub
src/Question/Serializers/AnswerDenormalizer.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Question\Serializers;

use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use VSV\GVQ_API\Company\ValueObjects\PositiveNumber;
use VSV\GVQ_API\Question\Models\Answer;
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString;

class AnswerDenormalizer implements DenormalizerInterface
{
    /**
     * @inheritdoc
     */
    public function denormalize($data, $class, $format = null, array $context = []): Answer
    {
        return new Answer(
            Uuid::fromString($data['id']),
            new PositiveNumber($data['index']),
            new NotEmptyString($data['text']),
            (bool) $data['correct']
        );
    }

    /**
     * @inheritdoc
     */
    public function supportsDenormalization($data, $type, $format = null): bool
    {
        return ($type === Answer::class) && ($format === 'json');
    }
}