vendor/shopware/core/Content/Property/PropertyGroupCollection.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Property;
  3. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  5. /**
  6.  * @method void                     add(PropertyGroupEntity $entity)
  7.  * @method void                     set(string $key, PropertyGroupEntity $entity)
  8.  * @method PropertyGroupEntity[]    getIterator()
  9.  * @method PropertyGroupEntity[]    getElements()
  10.  * @method PropertyGroupEntity|null get(string $key)
  11.  * @method PropertyGroupEntity|null first()
  12.  * @method PropertyGroupEntity|null last()
  13.  */
  14. class PropertyGroupCollection extends EntityCollection
  15. {
  16.     public function getOptionIdMap(): array
  17.     {
  18.         $map = [];
  19.         /** @var PropertyGroupEntity $group */
  20.         foreach ($this->elements as $group) {
  21.             if (!$group->getOptions()) {
  22.                 continue;
  23.             }
  24.             foreach ($group->getOptions() as $option) {
  25.                 $map[$option->getId()] = $group->getId();
  26.             }
  27.         }
  28.         return $map;
  29.     }
  30.     public function sortByPositions(): void
  31.     {
  32.         usort($this->elements, function (PropertyGroupEntity $aPropertyGroupEntity $b) {
  33.             $posA $a->getTranslation('position') ?? $a->getPosition() ?? 0;
  34.             $posB $b->getTranslation('position') ?? $b->getPosition() ?? 0;
  35.             if ($posA === $posB) {
  36.                 return strnatcmp($a->getTranslation('name'), $b->getTranslation('name'));
  37.             }
  38.             return $posA <=> $posB;
  39.         });
  40.     }
  41.     public function sortByConfig(): void
  42.     {
  43.         /** @var PropertyGroupEntity $group */
  44.         foreach ($this->elements as $group) {
  45.             if (!$group->getOptions()) {
  46.                 continue;
  47.             }
  48.             $group->getOptions()->sort(static function (PropertyGroupOptionEntity $aPropertyGroupOptionEntity $b) use ($group) {
  49.                 if ($group->getSortingType() === PropertyGroupDefinition::SORTING_TYPE_ALPHANUMERIC) {
  50.                     return strnatcmp($a->getTranslation('name'), $b->getTranslation('name'));
  51.                 }
  52.                 /* @deprecated tag:v6.4.0 - SORTING_TYPE_NUMERIC will be removed in 6.4.0 */
  53.                 if ($group->getSortingType() === PropertyGroupDefinition::SORTING_TYPE_NUMERIC) {
  54.                     return $a->getTranslation('name') <=> $b->getTranslation('name');
  55.                 }
  56.                 return ($a->getTranslation('position') ?? $a->getPosition() ?? 0) <=> ($b->getTranslation('position') ?? $b->getPosition() ?? 0);
  57.             });
  58.         }
  59.     }
  60.     public function getApiAlias(): string
  61.     {
  62.         return 'product_group_collection';
  63.     }
  64.     protected function getExpectedClass(): string
  65.     {
  66.         return PropertyGroupEntity::class;
  67.     }
  68. }