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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        // EmbedOne
        if (isset($data['coordinates']) || (! empty($this->class->fieldMappings['coordinates']['nullable']) && array_key_exists('coordinates', $data))) {
            $return = $data['coordinates'];
            if ($return !== null) {
                $embeddedDocument = $return;

                if (! is_array($embeddedDocument)) {
                    throw HydratorException::associationTypeMismatch('App\Document\DeliveryAddress', 'coordinates', 'array', gettype($embeddedDocument));
                }
        
                $className = $this->dm->getClassNameForAssociation($this->class->fieldMappings['coordinates'], $embeddedDocument);
                $embeddedMetadata = $this->dm->getClassMetadata($className);
                $return = $embeddedMetadata->newInstance();

                $this->dm->getUnitOfWork()->setParentAssociation($return, $this->class->fieldMappings['coordinates'], $document, 'coordinates');

                $embeddedData = $this->dm->getHydratorFactory()->hydrate($return, $embeddedDocument, $hints);
                $embeddedId = $embeddedMetadata->identifier && isset($embeddedData[$embeddedMetadata->identifier]) ? $embeddedData[$embeddedMetadata->identifier] : null;

                if (empty($hints[Query::HINT_READ_ONLY])) {
                    $this->dm->getUnitOfWork()->registerManaged($return, $embeddedId, $embeddedData);
                }
            }

            $this->class->propertyAccessors['coordinates']->setValue($document, $return);
            $hydratedData['coordinates'] = $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;
    }
}