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.

197 lines
4.6KB

  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\SovBundle\Doctrine\EntityInterface;
  7. abstract class AbstractRepositoryQuery implements RepositoryQueryInterface
  8. {
  9. protected ServiceEntityRepository $repository;
  10. protected QueryBuilder $query;
  11. protected PaginatorInterface $paginator;
  12. protected string $id;
  13. public function __construct(ServiceEntityRepository $repository, string $id, PaginatorInterface $paginator = null)
  14. {
  15. $this->repository = $repository;
  16. $this->query = $repository->createQueryBuilder($id);
  17. $this->paginator = $paginator;
  18. $this->id = $id;
  19. }
  20. public function __call(string $name, $params): self
  21. {
  22. foreach ($params as $key => $value) {
  23. $this->populateDqlId($params[$key]);
  24. }
  25. call_user_func_array([$this->query, $name], $params);
  26. return $this;
  27. }
  28. public function create()
  29. {
  30. $class = get_called_class();
  31. return new $class($this->repository, $this->paginator);
  32. }
  33. public function call(callable $fn): self
  34. {
  35. $fn($this->query, $this);
  36. return $this;
  37. }
  38. public function count(): string
  39. {
  40. return $this->query->getQuery()
  41. ->getSingleScalarResult();
  42. }
  43. public function findOne(): ?EntityInterface
  44. {
  45. return $this->query->getQuery()
  46. ->setMaxResults(1)
  47. ->getOneOrNullResult();
  48. }
  49. public function find(): array
  50. {
  51. return $this->query->getQuery()->getResult();
  52. }
  53. public function limit(int $maxResults)
  54. {
  55. return $this->query->setMaxResults($maxResults);
  56. }
  57. public function paginate(int $page = 1, int $limit = 20)
  58. {
  59. return $this->paginator->paginate($this->query->getQuery(), $page, $limit);
  60. }
  61. public function getRepository(): ServiceEntityRepository
  62. {
  63. return $this->repository;
  64. }
  65. protected function populateDqlId(&$data)
  66. {
  67. if (is_string($data)) {
  68. $words = explode(' ', $data);
  69. foreach ($words as $k => $v) {
  70. if (isset($v[0]) && '.' === $v[0]) {
  71. $words[$k] = $this->id . $v;
  72. }
  73. }
  74. $data = implode(' ', $words);
  75. } elseif (is_array($data)) {
  76. foreach ($data as $k => $v) {
  77. $this->populateDqlId($data[$k]);
  78. }
  79. }
  80. return $data;
  81. }
  82. public function groupBy(string $field): self
  83. {
  84. if (substr($field, 0, 1) === '.') {
  85. return $this->groupBy($field) ;
  86. } else {
  87. return $this->groupBy('.'.$field) ;
  88. }
  89. }
  90. public function orderBy(string $field, string $sort = 'ASC'): self
  91. {
  92. if (substr($field, 0, 1) === '.') {
  93. return $this->addOrderBy($field, $sort);
  94. } else {
  95. return $this->addOrderBy('.' . $field, $sort);
  96. }
  97. }
  98. public function filterById(int $id)
  99. {
  100. return $this
  101. ->andWhere('.id = :id')
  102. ->setParameter('id', $id);
  103. }
  104. /*
  105. * DEVALIAS
  106. */
  107. public function filterByDevAlias(string $devAlias): self
  108. {
  109. return $this
  110. ->andWhere('.devAlias = :devAlias')
  111. ->setParameter('devAlias', $devAlias);
  112. }
  113. /*
  114. * SLUG
  115. */
  116. public function filterBySlug(string $slug)
  117. {
  118. return $this
  119. ->andWhere('.slug = :slug')
  120. ->setParameter('slug', $slug);
  121. }
  122. /*
  123. * TREE
  124. */
  125. public function filterIsParent()
  126. {
  127. return $this->andWhere('.parent is NULL');
  128. }
  129. public function filterIsChildren()
  130. {
  131. return $this->andWhere('.parent is NOT NULL');
  132. }
  133. public function filterByParent(EntityInterface $parent)
  134. {
  135. return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
  136. }
  137. /*
  138. * STATUS
  139. */
  140. public function filterByStatus(int $status):self
  141. {
  142. return $this->andWhereStatus($this->id, $status);
  143. }
  144. public function filterIsOffline():self
  145. {
  146. return $this->andWhereStatus($this->id, 0);
  147. }
  148. public function filterIsOnline():self
  149. {
  150. return $this->andWhereStatus($this->id, 1);
  151. }
  152. public function filterIsDeleted():self
  153. {
  154. return $this->andWhereStatus($this->id, -1);
  155. }
  156. public function filterIsOnlineAndOffline():self
  157. {
  158. return $this->andWhere('.status >= 0');
  159. }
  160. }