@param VisitorInterface $visitor
@param mixed            $data
@param array            $type

@throws \Paillechat\Enum\Exception\EnumException

@return Enum
549279
public function deserialize(VisitorInterface $visitor, $data, array $type)
    {
        if ($data === null) {
            return null;
        }

        // Return enum if exists:
        $class = $type['params'][0] ?? null;
        if (class_exists($class) && isset(class_parents($class)[Enum::class])) {
            return new $class($data);
        }

        throw new \LogicException('Enum class does not exist.');
    }
149839
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('ContentTypeGroupRefList');
        $visitor->setHeader('Content-Type', $generator->getMediaType('ContentTypeGroupRefList'));

        $generator->startAttribute(
            'href',
            $this->router->generate(
                'ezpublish_rest_listContentTypesForGroup',
                array(
                    'contentTypeGroupId' => $data->contentType->id,
                )
            )
        );
        $generator->endAttribute('href');

        $groupCount = count($data->contentTypeGroups);

        $generator->startList('ContentTypeGroupRef');
        foreach ($data->contentTypeGroups as $contentTypeGroup) {
            $generator->startObjectElement('ContentTypeGroupRef', 'ContentTypeGroup');

            $generator->startAttribute(
                'href',
                $this->router->generate(
                    'ezpublish_rest_loadContentTypeGroup',
                    array(
                        'contentTypeGroupId' => $contentTypeGroup->id,
                    )
                )
            );
            $generator->endAttribute('href');

            // Unlinking last group is not allowed
            if ($groupCount > 1) {
                $generator->startHashElement('unlink');

                $generator->startAttribute(
                    'href',
                    $this->router->generate(
                        'ezpublish_rest_unlinkContentTypeFromGroup',
                        array(
                            'contentTypeId' => $data->contentType->id,
                            'contentTypeGroupId' => $contentTypeGroup->id,
                        )
                    )
                );
                $generator->endAttribute('href');

                $generator->startAttribute('method', 'DELETE');
                $generator->endAttribute('method');

                $generator->endHashElement('unlink');
            }

            $generator->endObjectElement('ContentTypeGroupRef');
        }
        $generator->endList('ContentTypeGroupRef');

        $generator->endObjectElement('ContentTypeGroupRefList');
    }
1013990
private function navigate(
        $data,
        $direction,
        $format,
        ContextInterface $context = null,
        TypeMetadataInterface $type = null
    ) {
        $visitor = $this->visitorRegistry->getVisitor($direction, $format);

        $context = $context ?: new Context();
        $context->initialize($this->navigator, $visitor, $direction, $format);
        $this->navigator->navigate($visitor->prepare($data, $context), $context, $type);

        return $visitor->getResult();
    }
876196
public function doSerialize(
        JsonSerializationVisitor $visitor,
        Structure $structure,
        array $type,
        Context $context
    ) {
        $array = $structure->toArray();

        return $context->accept($array);
    }
1038944
public function serializeImageToJson(JsonSerializationVisitor $visitor, Image $image, array $type, Context $context)
    {
        $type['name'] = 'array';

        return $visitor->visitArray([
            'id' => $image->getId(),
            'url' => $this->imageManager->filePathToWebPath($image->getPath())
        ], $type, $context);
    }
1013647
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('View');
        $visitor->setHeader('Content-Type', $generator->getMediaType('View'));

        $generator->startAttribute(
            'href',
            $this->router->generate('ezpublish_rest_views_load', array('viewId' => $data->identifier))
        );
        $generator->endAttribute('href');

        $generator->startValueElement('identifier', $data->identifier);
        $generator->endValueElement('identifier');

        // BEGIN Query
        $generator->startObjectElement('Query');
        $generator->endObjectElement('Query');
        // END Query

        // BEGIN Result
        $generator->startObjectElement('Result', 'ViewResult');
        $generator->startAttribute(
            'href',
            $this->router->generate('ezpublish_rest_views_load_results', array('viewId' => $data->identifier))
        );
        $generator->endAttribute('href');

        // BEGIN Result metadata
        $generator->startValueElement('count', $data->searchResults->totalCount);
        $generator->endValueElement('count');

        $generator->startValueElement('time', $data->searchResults->time);
        $generator->endValueElement('time');

        $generator->startValueElement('timedOut', $generator->serializeBool($data->searchResults->timedOut));
        $generator->endValueElement('timedOut');

        $generator->startValueElement('maxScore', $data->searchResults->maxScore);
        $generator->endValueElement('maxScore');
        // END Result metadata

        // BEGIN searchHits
        $generator->startHashElement('searchHits');
        $generator->startList('searchHit');

        foreach ($data->searchResults->searchHits as $searchHit) {
            $generator->startObjectElement('searchHit');

            $generator->startAttribute('score', (float)$searchHit->score);
            $generator->endAttribute('score');

            $generator->startAttribute('index', (string)$searchHit->index);
            $generator->endAttribute('index');

            $generator->startObjectElement('value');

            // @todo Refactor
            if ($searchHit->valueObject instanceof ApiValues\Content) {
                /** @var \eZ\Publish\API\Repository\Values\Content\Content $searchHit->valueObject */
                $contentInfo = $searchHit->valueObject->contentInfo;
                $valueObject = new RestContentValue(
                    $contentInfo,
                    $this->locationService->loadLocation($contentInfo->mainLocationId),
                    $searchHit->valueObject,
                    $searchHit->valueObject->getContentType(),
                    $this->contentService->loadRelations($searchHit->valueObject->getVersionInfo())
                );
            } elseif ($searchHit->valueObject instanceof ApiValues\Location) {
                $valueObject = $searchHit->valueObject;
            } elseif ($searchHit->valueObject instanceof ApiValues\ContentInfo) {
                $valueObject = new RestContentValue($searchHit->valueObject);
            } else {
                throw new Exceptions\InvalidArgumentException('Unhandled object type');
            }

            $visitor->visitValueObject($valueObject);
            $generator->endObjectElement('value');
            $generator->endObjectElement('searchHit');
        }

        $generator->endList('searchHit');

        $generator->endHashElement('searchHits');
        // END searchHits

        $generator->endObjectElement('Result');
        // END Result

        $generator->endObjectElement('View');
    }
746250
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('ContentTypeInfoList');
        $visitor->setHeader('Content-Type', $generator->getMediaType('ContentTypeInfoList'));
        //@todo Needs refactoring, disabling certain headers should not be done this way
        $visitor->setHeader('Accept-Patch', false);

        $generator->startAttribute('href', $data->path);
        $generator->endAttribute('href');

        $generator->startList('ContentType');
        foreach ($data->contentTypes as $contentType) {
            $visitor->visitValueObject(
                new Values\RestContentType($contentType)
            );
        }
        $generator->endList('ContentType');

        $generator->endObjectElement('ContentTypeInfoList');
    }
657094
public static function validateEnum($domain, $type, $value)
    {
        self::validateEnumDomainAndType($domain, $type);

        if (!in_array($value, self::$enum[$domain][$type]['values'], true)) {
            throw new \InvalidArgumentException(
                sprintf('Unknown value "%s" for "%s"', $value, $type)
            );
        };
    }
386378
public function deserializeHashFromJson(
        JsonDeserializationVisitor $visitor,
        array $data,
        array $type,
        Context $context
    ) {
        $currentPath = $context->getCurrentPath();
        $currentRequestContent = $this->getCurrentRequestContent();
        $dataObj = null;

        if (!is_null($currentRequestContent)) {
            $dataObj = $currentRequestContent;
            foreach ($currentPath as $pathElement) {
                if (isset($dataObj->{$pathElement})) {
                    $dataObj = $dataObj->{$pathElement};
                } else {
                    $dataObj = null;
                    break;
                }
            }
        }

        if (!is_null($dataObj)) {
            if ($this->isSequentialArrayCase($dataObj, $data)) {
                $dataObj = $dataObj[$this->getLocationCounter($currentPath)];
            }

            $data = $dataObj;
        }

        return new Hash($visitor->visitArray((array) $data, $type, $context));
    }
1369147
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('UrlAliasRefList');
        $visitor->setHeader('Content-Type', $generator->getMediaType('UrlAliasRefList'));

        $generator->startAttribute('href', $data->path);
        $generator->endAttribute('href');

        $generator->startList('UrlAlias');
        foreach ($data->urlAliases as $urlAlias) {
            $generator->startObjectElement('UrlAlias');

            $generator->startAttribute(
                'href',
                $this->router->generate('ezpublish_rest_loadURLAlias', array('urlAliasId' => $urlAlias->id))
            );
            $generator->endAttribute('href');

            $generator->endObjectElement('UrlAlias');
        }
        $generator->endList('UrlAlias');

        $generator->endObjectElement('UrlAliasRefList');
    }
1210238
public function serializeDateTime(SerializationVisitorInterface $visitor, \DateTime $date, array $type, SerializationContext $context)
    {
        return $this->serializeDateTimeInterface($visitor, $date, $type, $context);
    }
782732
public function serialize(VisitorInterface $visitor, ?UuidInterface $uuid, Type $type, Context $context): ?string
    {
        if (null === $uuid) {
            return $visitor->visitNull(null, Type::null(), $context);
        }

        return $visitor->visitString($uuid->toString(), $type, $context);
    }
990956
public function serializeArrayObjectToJson(
        JsonSerializationVisitor $visitor,
        \ArrayObject $data,
        array $type,
        Context $context
    ) {
        return new \ArrayObject($visitor->visitArray($data->getArrayCopy(), $type, $context));
    }
534710
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('RoleList');
        $visitor->setHeader('Content-Type', $generator->getMediaType('RoleList'));
        //@todo Needs refactoring, disabling certain headers should not be done this way
        $visitor->setHeader('Accept-Patch', false);

        $generator->startAttribute('href', $data->path);
        $generator->endAttribute('href');

        $generator->startList('Role');
        foreach ($data->roles as $role) {
            $visitor->visitValueObject($role);
        }
        $generator->endList('Role');

        $generator->endObjectElement('RoleList');
    }
551030
public static function sanitizeForSerialization($data, $type = null, $format = null)
    {
        if (is_scalar($data) || null === $data) {
            return $data;
        } elseif ($data instanceof \DateTime) {
            return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
        } elseif (is_array($data)) {
            foreach ($data as $property => $value) {
                $data[$property] = self::sanitizeForSerialization($value);
            }
            return $data;
        } elseif (is_object($data)) {
            $values = [];
            $formats = $data::openAPIFormats();
            foreach ($data::openAPITypes() as $property => $openAPIType) {
                $getter = $data::getters()[$property];
                $value = $data->$getter();
                if ($value !== null
                    && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)
                    && method_exists($openAPIType, 'getAllowableEnumValues')
                    && !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
                    $imploded = implode("', '", $openAPIType::getAllowableEnumValues());
                    throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
                }
                if ($value !== null) {
                    $values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
                }
            }
            return (object)$values;
        } else {
            return (string)$data;
        }
    }
956043
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $generator->startObjectElement('Version');

        $visitor->setHeader('Content-Type', $generator->getMediaType('Version'));
        $visitor->setHeader('Accept-Patch', $generator->getMediaType('VersionUpdate'));
        $this->visitVersionAttributes($visitor, $generator, $data);
        $generator->endObjectElement('Version');
    }
1163737
public function visit(Visitor $visitor, Generator $generator, $data)
    {
        $versionInfo = $data->getVersionInfo();
        if (empty($versionInfo->languageCodes)) {
            return;
        }

        $generator->startObjectElement('VersionTranslationInfo');
        $this->visitVersionTranslationInfoAttributes($visitor, $generator, $data);
        $generator->endObjectElement('VersionTranslationInfo');
    }