You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.5KB

  1. <?php
  2. namespace Lc\SovBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\ORM\QueryBuilder;
  5. use Knp\Component\Pager\PaginatorInterface;
  6. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Lc\SovBundle\Doctrine\EntityInterface;
  9. abstract class AbstractRepositoryQuery
  10. {
  11. use StatusRepositoryQueryTrait;
  12. use TreeRepositoryQueryTrait;
  13. protected ServiceEntityRepository $repository;
  14. protected QueryBuilder $query;
  15. protected PaginatorInterface $paginator;
  16. protected string $id;
  17. public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null)
  18. {
  19. $this->repository = $repository;
  20. $this->query = $repository->createQueryBuilder($id);
  21. $this->paginator = $paginator;
  22. $this->id = $id;
  23. }
  24. public function __call(string $name, $params): self
  25. {
  26. foreach ($params as $key => $value) {
  27. $this->populateDqlId($params[$key]);
  28. }
  29. call_user_func_array([$this->query, $name], $params);
  30. return $this;
  31. }
  32. public function create()
  33. {
  34. $class = get_called_class();
  35. return new $class($this->repository, $this->paginator);
  36. }
  37. public function call(callable $fn): self
  38. {
  39. $fn($this->query, $this);
  40. return $this;
  41. }
  42. public function count(): string
  43. {
  44. return $this->query->getQuery()
  45. ->getSingleScalarResult();
  46. }
  47. public function findOne(): ?EntityInterface
  48. {
  49. return $this->query->getQuery()
  50. ->setMaxResults(1)
  51. ->getOneOrNullResult();
  52. }
  53. public function find(): array
  54. {
  55. return $this->query->getQuery()->getResult();
  56. }
  57. public function limit(int $maxResults)
  58. {
  59. return $this->query->setMaxResults($maxResults);
  60. }
  61. public function paginate(int $page = 1, int $limit = 20)
  62. {
  63. return $this->paginator->paginate($this->query->getQuery(), $page, $limit);
  64. }
  65. public function getRepository(): ServiceEntityRepository
  66. {
  67. return $this->repository;
  68. }
  69. protected function populateDqlId(&$data)
  70. {
  71. if (is_string($data)) {
  72. $words = explode(' ', $data);
  73. foreach ($words as $k => $v) {
  74. if (isset($v[0]) && '.' === $v[0]) {
  75. $words[$k] = $this->id . $v;
  76. }
  77. }
  78. $data = implode(' ', $words);
  79. } elseif (is_array($data)) {
  80. foreach ($data as $k => $v) {
  81. $this->populateDqlId($data[$k]);
  82. }
  83. }
  84. return $data;
  85. }
  86. public function orderBy(string $field, string $sort = 'ASC'): self
  87. {
  88. if (substr($field, 0, 1) === '.') {
  89. return $this->addOrderBy($field, $sort);
  90. } else {
  91. return $this->addOrderBy('.' . $field, $sort);
  92. }
  93. }
  94. public function filterById(int $id)
  95. {
  96. return $this
  97. ->andWhere('.id = :id')
  98. ->setParameter('id', $id);
  99. }
  100. public function filterByDevAlias(string $devAlias): self
  101. {
  102. return $this
  103. ->andWhere('.devAlias = :devAlias')
  104. ->setParameter('devAlias', $devAlias);
  105. }
  106. public function filterBySlug(string $slug)
  107. {
  108. return $this
  109. ->andWhere('.slug = :slug')
  110. ->setParameter('slug', $slug);
  111. }
  112. }