| 1: | <?php |
| 2: | namespace TIC\DormBundle\Traits; |
| 3: | |
| 4: | use Doctrine\ORM\QueryBuilder; |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | trait RepositoryFilterable |
| 10: | { |
| 11: | public $alias = 'a'; |
| 12: | public $sortBy = ['id' => 'ASC']; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | public function initQB(&$qb_or_alias = null): string |
| 21: | { |
| 22: | |
| 23: | $alias = \is_string($qb_or_alias) ? $qb_or_alias : $this->alias; |
| 24: | |
| 25: | |
| 26: | if (\is_object($qb_or_alias)) { |
| 27: | $from = $qb_or_alias->getDQLPart('from'); |
| 28: | if (\count($from)) $alias = $from[0]->getAlias(); |
| 29: | } else { |
| 30: | $qb_or_alias = $this->createQueryBuilder($alias); |
| 31: | } |
| 32: | |
| 33: | |
| 34: | return $alias; |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function qbFilters(array $filters = array(), $qb = null): QueryBuilder |
| 45: | { |
| 46: | $alias = $a = $this->initQB($qb) . '.'; |
| 47: | |
| 48: | |
| 49: | if (! empty($filters)) foreach ($filters as $type => $value) switch ($type) { |
| 50: | case 'id' : |
| 51: | if ($value) $qb->andWhere($a."id = :id" )->setParameter('id', \is_object($value)?$value->getId():\intval($value)); |
| 52: | break; |
| 53: | case 'exclude' : |
| 54: | if ($value) $qb->andWhere($a."id != :exclude")->setParameter('exclude', $value); |
| 55: | break; |
| 56: | case 'excludes' : |
| 57: | if (\is_string($value)) $value = \explode(',', $value); |
| 58: | if (\count($value)) $qb->andWhere($a."id NOT IN (:excludes)")->setParameter('excludes', $value); |
| 59: | break; |
| 60: | case 'includes' : |
| 61: | if (\is_string($value)) $value = \explode(',', $value); |
| 62: | if (\count($value)) $qb->andWhere($a."id IN (:includes)")->setParameter('includes', $value); |
| 63: | break; |
| 64: | |
| 65: | case 'enabled' : |
| 66: | if ($value !== null) $qb->andWhere($a."enabled = :enabled")->setParameter('enabled', (bool)$value); |
| 67: | break; |
| 68: | case 'allorcur': |
| 69: | if ($value === true) break; |
| 70: | if ($value === null || $value === false) |
| 71: | $qb->andWhere($a.'enabled = true'); |
| 72: | else |
| 73: | $qb->andWhere('('.$a.'enabled = true OR '.$a.'id = :curid)')->setParameter('curid', \is_object($value)?$value->getId():\intval($value)); |
| 74: | break; |
| 75: | } |
| 76: | return $qb; |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | public function qbOrders($orders = null, $qb = null): QueryBuilder |
| 87: | { |
| 88: | $alias = $a = $this->initQB($qb) . '.'; |
| 89: | |
| 90: | if ($orders === null || $orders === true) $orders = $this->sortBy; |
| 91: | if (\is_string($orders)) $orders = array($orders => 'ASC'); |
| 92: | |
| 93: | if (\is_array($orders)) foreach ($orders as $field => $order) { |
| 94: | if (\strpos($field, '.') === FALSE) $field = $alias . $field; |
| 95: | $qb->addOrderBy($field, $order); |
| 96: | } |
| 97: | return $qb; |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public function listAll(array $filters = array(), $orders = null, ?string $hydrator = null): array |
| 108: | { |
| 109: | return $this->qbOrders($orders, $this->qbFilters($filters))->getQuery()->getResult($hydrator); |
| 110: | } |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function getOne(array $filters = array()) |
| 119: | { |
| 120: | return $this->qbFilters($filters)->getQuery()->getOneOrNullResult(); |
| 121: | } |
| 122: | |
| 123: | |
| 124: | |
| 125: | public function getCountResults($filters = array()) |
| 126: | { |
| 127: | $qb = $this->qbFilters($filters, 'a'); |
| 128: | return $qb->select($qb->expr()->count('a'))->getQuery()->getSingleScalarResult(); |
| 129: | } |
| 130: | |
| 131: | public function getMinValue($field, $filters = array()) |
| 132: | { |
| 133: | $qb = $this->qbFilters($filters, 'a'); |
| 134: | return $qb->select($qb->expr()->min('a.'.$field))->getQuery()->getSingleScalarResult(); |
| 135: | } |
| 136: | |
| 137: | public function getMaxValue($field, $filters = array()) |
| 138: | { |
| 139: | $qb = $this->qbFilters($filters, 'a'); |
| 140: | return $qb->select($qb->expr()->max('a.'.$field))->getQuery()->getSingleScalarResult(); |
| 141: | } |
| 142: | |
| 143: | public function getSumValue($field, $filters = array()) |
| 144: | { |
| 145: | $qb = $this->qbFilters($filters, 'a'); |
| 146: | return $qb->select("SUM(a.".$field.")")->getQuery()->getSingleScalarResult(); |
| 147: | } |
| 148: | |
| 149: | } |
| 150: | |