namespace Test;

class Entity
{
    /**
     * Checks if this entity can be associated with the given target entity type
     *
     * @param string $targetClass The class name of the target entity
     * @return bool
     */
    public function supportTestTarget($targetClass)
    {
        $className = \Doctrine\Common\Util\ClassUtils::getRealClass($targetClass);
        if ($className === 'Test\TargetEntity1') { return true; }
        if ($className === 'Test\TargetEntity2') { return true; }
        return false;
    }

    /**
     * Sets the entity this entity is associated with
     *
     * @param object $target Any configurable entity that can be associated with this type of entity
     * @return object This object
     */
    public function setTestTarget($target)
    {
        if (null === $target) { $this->resetTestTargets(); return $this; }
        $className = \Doctrine\Common\Util\ClassUtils::getClass($target);
        // This entity can be associated with only one another entity
        if ($className === 'Test\TargetEntity1') { $this->resetTestTargets(); $this->target_entity1_2ce134a6 = $target; return $this; }
        if ($className === 'Test\TargetEntity2') { $this->resetTestTargets(); $this->target_entity2_b5e8651c = $target; return $this; }
        throw new \RuntimeException(sprintf('The association with "%s" entity was not configured.', $className));
    }

    /**
     * Returns array with all associated entities
     *
     * @return array
     */
    public function getTestTargetEntities()
    {
        $associationEntities = [];
        $entity = $this->target_entity1_2ce134a6;
        if ($entity) {
            $associationEntities[] = $entity;
        }
        $entity = $this->target_entity2_b5e8651c;
        if ($entity) {
            $associationEntities[] = $entity;
        }
        return $associationEntities;
    }

    /**
     * Gets the entity this entity is associated with
     *
     * @return object|null Any configurable entity
     */
    public function getTestTarget()
    {
        if (null !== $this->target_entity1_2ce134a6) { return $this->target_entity1_2ce134a6; }
        if (null !== $this->target_entity2_b5e8651c) { return $this->target_entity2_b5e8651c; }
        return null;
    }

    private function resetTestTargets()
    {
        $this->target_entity1_2ce134a6 = null;
        $this->target_entity2_b5e8651c = null;
    }
}
