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.

99 lines
2.7KB

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