UnsupportedEncoderInputException example


    public function __construct(private readonly NormalizerInterface $serializer)
    {
    }

    /** * @param EntityCollection<Entity>|Entity|null $data */
    public function encode(Criteria $criteria, EntityDefinition $definition$data, string $baseUrl): array
    {
        if ((!$data instanceof EntityCollection) && (!$data instanceof Entity)) {
            throw new UnsupportedEncoderInputException();
        }

        if ($data instanceof EntityCollection) {
            return $this->getDecodedCollection($criteria$data$definition$baseUrl);
        }

        return $this->getDecodedEntity($criteria$data$definition$baseUrl);
    }

    /** * @param EntityCollection<Entity> $collection */
/** * @param EntityCollection<Entity>|Entity|null $data * * @throws UnsupportedEncoderInputException */
    public function encode(Criteria $criteria, EntityDefinition $definition$data, string $baseUrl, array $metaData = []): string
    {
        $this->serializeCache = [];
        $result = new JsonApiEncodingResult($baseUrl);

        if (!$data instanceof EntityCollection && !$data instanceof Entity) {
            throw new UnsupportedEncoderInputException();
        }

        $result->setSingleResult($data instanceof Entity);
        $result->setMetaData($metaData);

        $fields = new ResponseFields($criteria->getIncludes());

        $this->encodeData($fields$definition$data$result);

        return $this->formatToJson($result);
    }

    
Home | Imprint | This part of the site doesn't use cookies.