chill-bundles/src/Bundle/ChillPersonBundle/Export/AbstractAccompanyingPeriodExportElement.php
2021-12-21 10:59:23 +01:00

48 lines
1.3 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Export;
use Doctrine\ORM\QueryBuilder;
use LogicException;
use function in_array;
class AbstractAccompanyingPeriodExportElement
{
/**
* Add the accompanying period alias to the query.
*
* @throws LogicException if the "person" alias is not present and attaching accompanying period is not possible
*/
protected function addJoinAccompanyingPeriod(QueryBuilder $query): void
{
if (false === $this->havingAccompanyingPeriodInJoin($query)) {
if (false === in_array('person', $query->getAllAliases(), true)) {
throw new LogicException("the alias 'person' does not exists in "
. 'query builder');
}
$query->join('person.accompanyingPeriods', 'accompanying_period');
}
}
/**
* Return true if "accompanying_period" alias is present in the query alises.
*/
protected function havingAccompanyingPeriodInJoin(QueryBuilder $query): bool
{
$joins = $query->getDQLPart('join') ?? [];
return in_array('accompanying_period', $query->getAllAliases(), true);
}
}