<?php
namespace Cms\ModuleBundle\Util;
use Cms\ModuleBundle\Entity\Proxy;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
/**
* Class ModuleProxyPlaceholderDoctrineFilter
* @package Cms\ModuleBundle\Util
*/
class ModuleProxyPlaceholderDoctrineFilter extends SQLFilter
{
const FILTER = 'module_proxy_placeholder_filter';
const MODES__ANY = null;
const MODES__NO_PLACEHOLDERS = 0;
const MODES__ONLY_PLACEHOLDERS = 1;
/**
* {@inheritdoc}
*/
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
// make sure we are in fact an entity that tracks tenant stuff
if ( ! $targetEntity->getReflectionClass()->isSubclassOf(Proxy::class)) {
return '';
}
// obtain our param
if ( ! $this->hasParameter('mode')) {
$this->setParameter('mode', self::MODES__ANY);
}
$mode = trim($this->getParameter('mode'), '\'');
// fix the mode, sql filters suck to work with
if (preg_match('/^[0-9]+$/', strval($mode)) === 1) {
$mode = intval($mode);
} else {
$mode = null;
}
// if in any mode, do nothing
if ($mode === self::MODES__ANY) {
return '';
}
// calculate the necessary dql to filter this
return sprintf(
'%s.placeholder = %s',
$targetTableAlias,
$mode
);
}
}