<?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 AppDocumentApiLogHydrator implements HydratorInterface
{
    public function __construct(private DocumentManager $dm, private ClassMetadata $class) {}

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

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

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

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

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

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

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

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

        // Field(type: "hash")
        if (isset($data['response']) || (! empty($this->class->fieldMappings['response']['nullable']) && array_key_exists('response', $data))) {
            $value = $data['response'];
            if ($value !== null) {
                $typeIdentifier = $this->class->fieldMappings['response']['type'];
                $return = (array) $value;
            } else {
                $return = null;
            }
            $this->class->propertyAccessors['response']->setValue($document, $return);
            $hydratedData['response'] = $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\ApiLog', '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;
        }

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

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

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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}