Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

218 lines
5.2KB

  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()
  39. {
  40. return $this->query->getQuery()
  41. ->getSingleScalarResult();
  42. }
  43. public function findOne()
  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):self
  54. {
  55. $this->query->setMaxResults($maxResults);
  56. return $this;
  57. }
  58. public function paginate(int $page = 1, int $limit = 20)
  59. {
  60. return $this->paginator->paginate($this->query->getQuery(), $page, $limit);
  61. }
  62. public function getRepository(): ServiceEntityRepository
  63. {
  64. return $this->repository;
  65. }
  66. protected function populateDqlId(&$data)
  67. {
  68. if (is_string($data)) {
  69. $words = explode(' ', $data);
  70. foreach ($words as $k => $v) {
  71. if (isset($v[0]) && '.' === $v[0]) {
  72. $words[$k] = $this->id . $v;
  73. }
  74. }
  75. $data = implode(' ', $words);
  76. } elseif (is_array($data)) {
  77. foreach ($data as $k => $v) {
  78. $this->populateDqlId($data[$k]);
  79. }
  80. }
  81. return $data;
  82. }
  83. public function groupBy(string $field): self
  84. {
  85. if (strpos($field, '.')!==false) {
  86. $this->addGroupBy($field) ;
  87. } else {
  88. $this->addGroupBy('.'.$field) ;
  89. }
  90. return $this;
  91. }
  92. /*
  93. public function addGroupBy(string $field): self
  94. {
  95. if (strpos($field, '.')!==false) {
  96. $this->query->addGroupBy($field) ;
  97. } else {
  98. $this->query->addGroupBy('.'.$field) ;
  99. }
  100. return $this;
  101. }*/
  102. public function orderBy(string $field, string $sort = 'ASC'): self
  103. {
  104. if (strpos($field, '.')!==false) {
  105. return $this->addOrderBy($field, $sort);
  106. } else {
  107. return $this->addOrderBy('.' . $field, $sort);
  108. }
  109. }
  110. public function filterById(int $id):self
  111. {
  112. return $this
  113. ->andWhere('.id = :id')
  114. ->setParameter('id', $id);
  115. }
  116. public function andWhereEqual($field, $value)
  117. {
  118. return $this->andWhere('.'.$field.' = :'.$field)->setParameter($field, $value);
  119. }
  120. public function filterByOldUrl(string $oldUrl): self
  121. {
  122. // @TODO : ne fonctionne pas, utilisation de LIKE à la place ?
  123. //return $this->andWhere(':oldUrl IN (.oldUrls)')->setParameter('oldUrl', $oldUrl);
  124. return $this->andWhere(':oldUrl LIKE .oldUrls')->setParameter('oldUrl', $oldUrl);
  125. }
  126. /*
  127. * DEVALIAS
  128. */
  129. public function filterByDevAlias(string $devAlias): self
  130. {
  131. return $this
  132. ->andWhere('.devAlias = :devAlias')
  133. ->setParameter('devAlias', $devAlias);
  134. }
  135. /*
  136. * SLUG
  137. */
  138. public function filterBySlug(string $slug):self
  139. {
  140. return $this
  141. ->andWhere('.slug = :slug')
  142. ->setParameter('slug', $slug);
  143. }
  144. /*
  145. * TREE
  146. */
  147. public function filterIsParent():self
  148. {
  149. return $this->andWhere('.parent IS NULL');
  150. }
  151. public function filterIsChildren():self
  152. {
  153. return $this->andWhere('.parent IS NOT NULL');
  154. }
  155. public function filterByParent(EntityInterface $parent = null):self
  156. {
  157. return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
  158. }
  159. /*
  160. * STATUS
  161. */
  162. public function filterIsOffline():self
  163. {
  164. return $this->andWhereStatus($this->id, 0);
  165. }
  166. public function filterIsOnline():self
  167. {
  168. return $this->andWhereStatus($this->id, 1);
  169. }
  170. public function filterIsDeleted():self
  171. {
  172. return $this->andWhereStatus($this->id, -1);
  173. }
  174. public function filterIsOnlineAndOffline():self
  175. {
  176. return $this->andWhere('.status >= 0');
  177. }
  178. }