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

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

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

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

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

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

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

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

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

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

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

        return $hydratedData;
    }
}