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.

AbstractRepositoryQuery.php 5.6KB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. public function getQueryBuilder(): QueryBuilder
  67. {
  68. return $this->query;
  69. }
  70. protected function populateDqlId(&$data)
  71. {
  72. if (is_string($data)) {
  73. $words = explode(' ', $data);
  74. foreach ($words as $k => $v) {
  75. if (isset($v[0]) && '.' === $v[0]) {
  76. $words[$k] = $this->id . $v;
  77. }
  78. }
  79. $data = implode(' ', $words);
  80. } elseif (is_array($data)) {
  81. foreach ($data as $k => $v) {
  82. $this->populateDqlId($data[$k]);
  83. }
  84. }
  85. return $data;
  86. }
  87. public function groupBy(string $field): self
  88. {
  89. if (strpos($field, '.')!==false) {
  90. $this->addGroupBy($field) ;
  91. } else {
  92. $this->addGroupBy('.'.$field) ;
  93. }
  94. return $this;
  95. }
  96. /*
  97. public function addGroupBy(string $field): self
  98. {
  99. if (strpos($field, '.')!==false) {
  100. $this->query->addGroupBy($field) ;
  101. } else {
  102. $this->query->addGroupBy('.'.$field) ;
  103. }
  104. return $this;
  105. }*/
  106. // @TODO : créer un addOrderBy et un orderBy
  107. public function orderBy(string $field, string $sort = 'ASC'): self
  108. {
  109. if (strpos($field, '.')!==false) {
  110. return $this->addOrderBy($field, $sort);
  111. } else {
  112. return $this->addOrderBy('.' . $field, $sort);
  113. }
  114. }
  115. public function filterById(int $id):self
  116. {
  117. return $this
  118. ->andWhere('.id = :id')
  119. ->setParameter('id', $id);
  120. }
  121. public function andWhereEqual($field, $value)
  122. {
  123. return $this->andWhere('.'.$field.' = :'.$field)->setParameter($field, $value);
  124. }
  125. public function filterByOldUrl(string $oldUrl): self
  126. {
  127. return $this->andWhere('.oldUrls LIKE :oldUrl')->setParameter('oldUrl', '%'.$oldUrl.'%');
  128. }
  129. public function resetRelationsJoin(): void
  130. {
  131. }
  132. /*
  133. * DEVALIAS
  134. */
  135. public function filterByDevAlias(string $devAlias): self
  136. {
  137. return $this
  138. ->andWhere('.devAlias = :devAlias')
  139. ->setParameter('devAlias', $devAlias);
  140. }
  141. /*
  142. * SLUG
  143. */
  144. public function filterBySlug(string $slug):self
  145. {
  146. return $this
  147. ->andWhere('.slug = :slug')
  148. ->setParameter('slug', $slug);
  149. }
  150. /*
  151. * TREE
  152. */
  153. public function filterIsParent():self
  154. {
  155. return $this->andWhere('.parent IS NULL');
  156. }
  157. public function filterIsChildren():self
  158. {
  159. return $this->andWhere('.parent IS NOT NULL');
  160. }
  161. public function filterByParent(EntityInterface $parent = null):self
  162. {
  163. return $this->andWhere('.parent = :parent')->setParameter('parent', $parent);
  164. }
  165. /*
  166. * STATUS
  167. */
  168. public function filterIsOffline():self
  169. {
  170. return $this->andWhereStatus($this->id, 0);
  171. }
  172. public function filterIsOnline():self
  173. {
  174. return $this->andWhereStatus($this->id, 1);
  175. }
  176. public function filterIsDeleted():self
  177. {
  178. return $this->andWhereStatus($this->id, -1);
  179. }
  180. public function filterIsOnlineAndOffline():self
  181. {
  182. return $this->andWhere('.status >= 0');
  183. }
  184. /*
  185. * POSITION
  186. */
  187. public function filterByPositionBiggerThan(int $position)
  188. {
  189. return $this->andWhere('.position > :position')->setParameter('position', $position);
  190. }
  191. public function filterByPositionSmallerThan(int $position)
  192. {
  193. return $this->andWhere('.position < :position')->setParameter('position', $position);
  194. }
  195. }