<?php

namespace AppBundle\Entity\Core\Repository;
use AppBundle\Entity\Place\Place;
use Doctrine\ORM\EntityRepository;
use AppBundle\Enum\Core\AddressPostalTypeEnum;
use AppBundle\Entity\Person\Person;
use AppBundle\Entity\Organization\Organization;

/**
 * Description of AccessRepository
 *
 */
class AddressPostalRepository extends EntityRepository
{

    public function findOwnerAddressPrincipal($owner) {

        $qb = $this->createQueryBuilder('a');

        $qb->where('a.type = :type')
            ->setParameter('type', AddressPostalTypeEnum::ADDRESS_PRINCIPAL);

            if ($owner instanceof Place) {
                $qb->andWhere('a.place = :place')
                    ->setParameter('place', $owner);
            }
            if ($owner instanceof Person) {
                $qb->andWhere('a.person = :person')
                    ->setParameter('person', $owner);
            }
            if ($owner instanceof Organization) {
                $qb->andWhere('a.organization = :organization')
                    ->setParameter('organization', $owner);
            }
        return $qb->getQuery()->getOneOrNullResult();
    }
}

?>
