vendor/shopware/core/Content/Product/SalesChannel/Sorting/ProductSortingEntity.php line 9

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Sorting;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  6. use Shopware\Core\Framework\Log\Package;
  7. #[Package('inventory')]
  8. class ProductSortingEntity extends Entity
  9. {
  10.     use EntityIdTrait;
  11.     /**
  12.      * @var string
  13.      */
  14.     protected $key;
  15.     /**
  16.      * @var int
  17.      */
  18.     protected $priority;
  19.     /**
  20.      * @var bool
  21.      */
  22.     protected $active;
  23.     /**
  24.      * @var array<array{field: string, priority: int, order: ?string, naturalSorting: bool|int|null}>
  25.      */
  26.     protected $fields;
  27.     /**
  28.      * @var string|null
  29.      */
  30.     protected $label;
  31.     /**
  32.      * @var ProductSortingTranslationCollection|null
  33.      */
  34.     protected $translations;
  35.     /**
  36.      * @var bool
  37.      */
  38.     protected $locked;
  39.     /**
  40.      * @return array<FieldSorting>
  41.      */
  42.     public function createDalSorting(): array
  43.     {
  44.         $sorting = [];
  45.         $fields $this->fields;
  46.         if (!\is_array($fields)) {
  47.             $fields = [];
  48.         }
  49.         usort($fields, fn ($a$b) => $b['priority'] <=> $a['priority']);
  50.         foreach ($fields as $field) {
  51.             $direction mb_strtoupper((string) $field['order']) === FieldSorting::ASCENDING
  52.                 FieldSorting::ASCENDING
  53.                 FieldSorting::DESCENDING;
  54.             $sorting[] = new FieldSorting(
  55.                 $field['field'],
  56.                 $direction,
  57.                 (bool) ($field['naturalSorting'] ?? false)
  58.             );
  59.         }
  60.         $flat array_column($fields'field');
  61.         if (\in_array('id'$flattrue)) {
  62.             return $sorting;
  63.         }
  64.         if (\in_array('product.id'$flattrue)) {
  65.             return $sorting;
  66.         }
  67.         $sorting[] = new FieldSorting('id'FieldSorting::ASCENDING);
  68.         return $sorting;
  69.     }
  70.     public function getKey(): string
  71.     {
  72.         return $this->key;
  73.     }
  74.     public function setKey(string $key): void
  75.     {
  76.         $this->key $key;
  77.     }
  78.     public function getPriority(): int
  79.     {
  80.         return $this->priority;
  81.     }
  82.     public function setPriority(int $priority): void
  83.     {
  84.         $this->priority $priority;
  85.     }
  86.     public function isActive(): bool
  87.     {
  88.         return $this->active;
  89.     }
  90.     public function setActive(bool $active): void
  91.     {
  92.         $this->active $active;
  93.     }
  94.     /**
  95.      * @return array<array{field: string, priority: int, order: ?string, naturalSorting: bool|int|null}>
  96.      */
  97.     public function getFields(): array
  98.     {
  99.         return $this->fields;
  100.     }
  101.     /**
  102.      * @param array<array{field: string, priority: int, order: ?string, naturalSorting: bool|int|null}> $fields
  103.      */
  104.     public function setFields(array $fields): void
  105.     {
  106.         $this->fields $fields;
  107.     }
  108.     public function getLabel(): ?string
  109.     {
  110.         return $this->label;
  111.     }
  112.     public function setLabel(?string $label): void
  113.     {
  114.         $this->label $label;
  115.     }
  116.     public function getTranslations(): ?ProductSortingTranslationCollection
  117.     {
  118.         return $this->translations;
  119.     }
  120.     public function setTranslations(ProductSortingTranslationCollection $translations): void
  121.     {
  122.         $this->translations $translations;
  123.     }
  124.     public function isLocked(): bool
  125.     {
  126.         return $this->locked;
  127.     }
  128.     public function setLocked(bool $locked): void
  129.     {
  130.         $this->locked $locked;
  131.     }
  132.     public function getApiAlias(): string
  133.     {
  134.         return 'product_sorting';
  135.     }
  136. }