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.

87 lines
2.5KB

  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']) && $defaultOptions['attribute_id_producer']) {
  58. $this->query->andWhere([$defaultOptions['attribute_id_producer'] => $this->getProducerContextId()]);
  59. }
  60. }
  61. public function defaultOrderBy(): void
  62. {
  63. $defaultOptions = $this->getDefaultOptionsSearch();
  64. if(isset($defaultOptions['orderby']) && $defaultOptions['orderby']) {
  65. $this->query->orderBy($defaultOptions['orderby']);
  66. }
  67. }
  68. public function findAll()
  69. {
  70. return $this->createQuery()->find();
  71. }
  72. }