<?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 AppDocumentDtoExpeditionSlotHydrator 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;
        }

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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}