<?php

namespace AppBundle\Entity\Traits;

use AppBundle\Entity\Person\Person;
use AppBundle\Enum\Core\AddressPostalTypeEnum;
use AppBundle\Entity\Organization\Organization;

/**
 * Description of AddressPostalType
 *
 * @author Sébastien Hupin <sebastien.hupin at gmail.com>
 */
trait AddressPostalType
{
    /**
     *
     * @param string $type
     * @return \AppBundle\Entity\Core\AddressPostal or null
     */
    public function getAddressPostalForType($type)
    {
        $addressPostalTypeEnum = new AddressPostalTypeEnum($type);

        if ($this instanceof Organization)
            $addressPostals = $this->getOrganizationAddressPostal();
        else if ($this instanceof Person)
            $addressPostals = $this->getPersonAddressPostal();

        foreach ($addressPostals as $addressPostal) {
            if ($addressPostalTypeEnum->getValue() === $addressPostal->getType()) {
                return $addressPostal->getAddressPostal();
            }
        }
        return null;
    }
}

?>
