AbstractRepository.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace domain\_;
  3. abstract class AbstractRepository extends AbstractService implements RepositoryInterface
  4. {
  5. const WITH = 'with';
  6. const JOIN_WITH = 'join_with';
  7. const ORDER_BY = 'orderby';
  8. const ATTRIBUTE_ID_PRODUCER = 'attribute_id_producer';
  9. public function loadQuery(string $serviceClass)
  10. {
  11. $this->query = $this->loadService($serviceClass);
  12. }
  13. public function createQuery()
  14. {
  15. $this->query->createQuery();
  16. $this->defaultWith();
  17. $this->defaultJoinWith();
  18. return $this->query;
  19. }
  20. public function createDefaultQuery(bool $filterStatus = true): RepositoryQueryInterface
  21. {
  22. $this->createQuery();
  23. if($filterStatus) {
  24. $this->defaultStatus();
  25. }
  26. $this->defaultWith();
  27. $this->defaultJoinWith();
  28. $this->defaultFilterProducerContext();
  29. $this->defaultOrderBy();
  30. return $this->query;
  31. }
  32. public function defaultStatus(): void
  33. {
  34. $class = new \ReflectionClass($this->query->getDefinition()->getEntityFqcn());
  35. if($class->implementsInterface('domain\_\StatusInterface')) {
  36. $this->query->filterIsStatusOnlineAndOffline();
  37. }
  38. }
  39. public function defaultWith(): void
  40. {
  41. $defaultOptions = $this->getDefaultOptionsSearch();
  42. if (is_array($defaultOptions['with']) && count($defaultOptions['with'])) {
  43. $this->query->with($defaultOptions['with']);
  44. }
  45. }
  46. public function defaultJoinWith(): void
  47. {
  48. $defaultOptions = $this->getDefaultOptionsSearch();
  49. if (is_array($defaultOptions['join_with']) && count($defaultOptions['join_with'])) {
  50. $this->query->joinWith($defaultOptions['join_with']);
  51. }
  52. }
  53. public function defaultFilterProducerContext(): void
  54. {
  55. $defaultOptions = $this->getDefaultOptionsSearch();
  56. if(isset($defaultOptions['attribute_id_producer'])
  57. && $defaultOptions['attribute_id_producer']
  58. && $this->getProducerContext()) {
  59. $this->query->andWhere([$defaultOptions['attribute_id_producer'] => $this->getProducerContextId()]);
  60. }
  61. }
  62. public function defaultOrderBy(): void
  63. {
  64. $defaultOptions = $this->getDefaultOptionsSearch();
  65. if(isset($defaultOptions['orderby']) && $defaultOptions['orderby']) {
  66. $this->query->orderBy($defaultOptions['orderby']);
  67. }
  68. }
  69. public function findAll()
  70. {
  71. return $this->createQuery()->find();
  72. }
  73. public function queryAll()
  74. {
  75. return $this->createQuery();
  76. }
  77. public function queryDefaultAll()
  78. {
  79. return $this->createDefaultQuery();
  80. }
  81. }