Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RepositoryTrait.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Lc\CaracoleBundle\Repository;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
  5. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  6. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  7. use Lc\CaracoleBundle\Resolver\SectionResolver;
  8. use Lc\SovBundle\Repository\AbstractRepository as SovAbstractRepository;
  9. trait RepositoryTrait
  10. {
  11. protected $merchantResolver;
  12. protected $sectionResolver;
  13. public function __construct(
  14. EntityManagerInterface $entityManager,
  15. SectionResolver $sectionResolver,
  16. MerchantResolver $merchantResolver
  17. ) {
  18. parent::__construct($entityManager);
  19. $this->merchantResolver = $merchantResolver;
  20. $this->sectionResolver = $sectionResolver;
  21. }
  22. protected function setCriteria(array $criteria) :array
  23. {
  24. $criteria = parent::setCriteria($criteria);
  25. $className = $this->getClassMetadata()->getName();
  26. $entity = new $className;
  27. if ($entity instanceof FilterMerchantInterface) {
  28. if (!isset($criteria['merchant'])) {
  29. $criteria['merchant'] = $this->merchantResolver->getCurrent();
  30. }
  31. if ($criteria['merchant'] === false) {
  32. unset($criteria['merchant']);
  33. }
  34. }
  35. if ($entity instanceof FilterSectionInterface) {
  36. if (!isset($criteria['section'])) {
  37. $criteria['section'] = $this->sectionResolver->getCurrent();
  38. }
  39. if ($criteria['section'] === false) {
  40. unset($criteria['section']);
  41. }
  42. }
  43. return $criteria;
  44. }
  45. }