<?php
namespace App\Doctrine\Repository\Content;
use App\Model\Searching\AbstractSearch;
use Cms\CoreBundle\Util\DateTimeUtils;
use DateInterval;
use DateTimeInterface;
use Doctrine\ORM\QueryBuilder;
use Platform\SecurityBundle\Entity\Identity\Account;
class LegacyProxySearch extends AbstractSearch
{
public const FILTERS = [
self::FILTERS__ALL,
self::FILTERS__DRAFT,
self::FILTERS__PUBLISHED,
self::FILTERS__MY_DRAFT,
self::FILTERS__MY_PUBLISHED,
];
const FILTERS__DEFAULT = self::FILTERS__ALL;
public const FILTERS__ALL = 'all';
public const FILTERS__DRAFT = 'draft';
public const FILTERS__PUBLISHED = 'published';
public const FILTERS__MY_DRAFT = 'my_draft';
public const FILTERS__MY_PUBLISHED = 'my_published';
const DIRECTIONS = [
self::SORTS__NAME => 'ASC',
self::SORTS__TIMESTAMP => 'DESC',
];
const SORTS__DEFAULT = self::SORTS__TIMESTAMP;
const SORTS__NAME = 'name';
const SORTS__TIMESTAMP = 'timestamp';
/**
* @var string|null
*/
private ?string $lookup = null;
/**
* @var bool
*/
private bool $defaults = true;
/**
* @var DateTimeInterface|null
*/
private ?DateTimeInterface $cutoff = null;
/**
* @var DateTimeInterface|DateInterval|null
*/
private $window = null;
/**
* @var bool|null
*/
private ?bool $visibility = null;
/**
* @var bool
*/
private bool $past = true;
/**
* @var Account|null
*/
private ?Account $account;
/**
* @return string|null
*/
public function getLookup(): ?string
{
return $this->lookup;
}
/**
* @param string|null $lookup
* @return $this
*/
public function setLookup(?string $lookup): self
{
$this->lookup = $lookup ?: null;
return $this;
}
/**
* @return bool
*/
public function hasDefaults(): bool
{
return $this->defaults;
}
/**
* @param bool $defaults
* @return $this
*/
public function setDefaults(bool $defaults): self
{
$this->defaults = $defaults;
return $this;
}
/**
* @return DateTimeInterface
*/
public function getCutoff(): DateTimeInterface
{
return $this->cutoff ?: DateTimeUtils::now();
}
/**
* @param DateTimeInterface|null $cutoff
* @return $this
*/
public function setCutoff(?DateTimeInterface $cutoff): self
{
$this->cutoff = $cutoff;
return $this;
}
/**
* @return DateTimeInterface|DateInterval|null
*/
public function getWindow()
{
return $this->window;
}
/**
* @param DateTimeInterface|DateInterval|string|null $window
* @return $this
*/
public function setWindow($window): self
{
switch (true) {
case empty($window):
case $window instanceof DateTimeInterface:
case $window instanceof DateInterval:
break;
case is_string($window):
$window = new DateInterval($window);
break;
default:
throw new \LogicException();
}
$this->window = $window ?: null;
return $this;
}
/**
* @return bool|null
*/
public function getVisibility(): ?bool
{
return $this->visibility;
}
/**
* @param bool|null $visibility
* @return $this
*/
public function setVisibility(?bool $visibility): self
{
$this->visibility = $visibility;
return $this;
}
/**
* @return bool
*/
public function isPast(): bool
{
return $this->past;
}
/**
* @param bool $past
* @return $this
*/
public function setPast(bool $past): self
{
$this->past = $past;
return $this;
}
/**
* @return Account|null
*/
public function getAccount(): ?Account
{
return $this->account;
}
/**
* @param Account|null $account
* @return $this
*/
public function setAccount(?Account $account): self
{
$this->account = $account;
return $this;
}
/**
* @return bool
*/
public function isFuture(): bool
{
return !$this->isPast();
}
/**
* @param bool $future
* @return $this
*/
public function setFuture(bool $future): self
{
return $this->setPast(!$future);
}
/**
* @param QueryBuilder $qb
* @return QueryBuilder
*/
public function filterCutoff(QueryBuilder $qb): QueryBuilder
{
return $qb
->andWhere(
$this->isPast()
? $qb->expr()->lte('objects.createdAt', ':cutoff')
: $qb->expr()->gte('objects.createdAt', ':cutoff')
)
->setParameter(
'cutoff',
$this->getCutoff() ?: DateTimeUtils::now()
);
}
/**
* @param QueryBuilder $qb
* @return QueryBuilder
*/
public function filterWindow(QueryBuilder $qb): QueryBuilder
{
if ($this->getWindow()) {
$qb
->andWhere(
$this->isPast()
? $qb->expr()->gte('objects.createdAt', ':window')
: $qb->expr()->lte('objects.createdAt', ':window')
)
->setParameter(
'window',
($this->getWindow() instanceof DateInterval)
? ($this->isPast() ? ((clone $this->getCutoff()) - $this->getWindow()) : (clone $this->getCutoff()) + $this->getWindow())
: $this->getWindow()
)
;
}
return $qb;
}
/**
* @param QueryBuilder $qb
* @return QueryBuilder
*/
public function orderCutoff(QueryBuilder $qb): QueryBuilder
{
return $qb->addOrderBy('objects.createdAt', $this->isPast() ? 'DESC' : 'ASC');
}
}