<?php

namespace Hydrators;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Hydrator\HydratorException;
use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface;
use Doctrine\ODM\MongoDB\Query\Query;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;

use function array_key_exists;
use function gettype;
use function is_array;

/**
 * THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE.
 */
class AppDocumentRateLimitRateLimitAuditEntryHydrator implements HydratorInterface
{
    public function __construct(private DocumentManager $dm, private ClassMetadata $class) {}

    public function hydrate(object $document, array $data, array $hints = []): array
    {
        $hydratedData = [];

        // Field(type: "date")
        if (array_key_exists('timestamp', $data) && ($data['timestamp'] !== null || ($this->class->fieldMappings['timestamp']['nullable'] ?? false))) {
            $value = $data['timestamp'];
            if ($value === null) { $return = null; } else { $return = \Doctrine\ODM\MongoDB\Types\DateType::getDateTime($value); }
            $this->class->propertyAccessors['timestamp']->setValue($document, $return === null ? null : clone $return);
            $hydratedData['timestamp'] = $return;
        }

        // ReferenceOne
        if (isset($data['user']) || (! empty($this->class->fieldMappings['user']['nullable']) && array_key_exists('user', $data))) {
            $return = $data['user'];
            if ($return !== null) {
                if ($this->class->fieldMappings['user']['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID && ! is_array($return)) {
                    throw HydratorException::associationTypeMismatch('App\Document\RateLimit\RateLimitAuditEntry', 'user', 'array', gettype($return));
                }

                $className = $this->dm->getClassNameForAssociation($this->class->fieldMappings['user'], $return);
                $identifier = ClassMetadata::getReferenceId($return, $this->class->fieldMappings['user']['storeAs']);
                $targetMetadata = $this->dm->getClassMetadata($className);
                $id = $targetMetadata->getPHPIdentifierValue($identifier);
                $return = $this->dm->getReference($className, $id);
            }

            $this->class->propertyAccessors['user']->setValue($document, $return);
            $hydratedData['user'] = $return;
        }

        // Field(type: "string")
        if (isset($data['action']) || (! empty($this->class->fieldMappings['action']['nullable']) && array_key_exists('action', $data))) {
            $value = $data['action'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['action']['type'];
                $return = (string) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['action']->setValue($document, $return);
            $hydratedData['action'] = $return;
        }

        // Field(type: "raw")
        if (isset($data['oldValue']) || (! empty($this->class->fieldMappings['oldValue']['nullable']) && array_key_exists('oldValue', $data))) {
            $value = $data['oldValue'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['oldValue']['type'];
                $return = $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['oldValue']->setValue($document, $return);
            $hydratedData['oldValue'] = $return;
        }

        // Field(type: "raw")
        if (isset($data['newValue']) || (! empty($this->class->fieldMappings['newValue']['nullable']) && array_key_exists('newValue', $data))) {
            $value = $data['newValue'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['newValue']['type'];
                $return = $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['newValue']->setValue($document, $return);
            $hydratedData['newValue'] = $return;
        }

        return $hydratedData;
    }
}