vendor/shopware/core/Checkout/Cart/Delivery/Struct/DeliveryCollection.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\Delivery\Struct;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressCollection;
  6. use Shopware\Core\Framework\Struct\Collection;
  7. /**
  8.  * @method void          add(Delivery $entity)
  9.  * @method void          set(string $key, Delivery $entity)
  10.  * @method Delivery[]    getIterator()
  11.  * @method Delivery[]    getElements()
  12.  * @method Delivery|null get(string $key)
  13.  * @method Delivery|null first()
  14.  * @method Delivery|null last()
  15.  */
  16. class DeliveryCollection extends Collection
  17. {
  18.     /**
  19.      * Sorts the delivery collection by earliest delivery date
  20.      */
  21.     public function sortDeliveries(): self
  22.     {
  23.         $this->sort(function (Delivery $aDelivery $b) {
  24.             if ($a->getLocation() !== $b->getLocation()) {
  25.                 return -1;
  26.             }
  27.             return $a->getDeliveryDate()->getEarliest() > $b->getDeliveryDate()->getEarliest();
  28.         });
  29.         return $this;
  30.     }
  31.     public function getDelivery(DeliveryDate $deliveryDateShippingLocation $location): ?Delivery
  32.     {
  33.         foreach ($this->getIterator() as $delivery) {
  34.             if ($delivery->getDeliveryDate()->getEarliest()->format('Y-m-d') !== $deliveryDate->getEarliest()->format('Y-m-d')) {
  35.                 continue;
  36.             }
  37.             if ($delivery->getDeliveryDate()->getLatest()->format('Y-m-d') !== $deliveryDate->getLatest()->format('Y-m-d')) {
  38.                 continue;
  39.             }
  40.             if ($delivery->getLocation() !== $location) {
  41.                 continue;
  42.             }
  43.             return $delivery;
  44.         }
  45.         return null;
  46.     }
  47.     public function contains(LineItem $item): bool
  48.     {
  49.         foreach ($this->getIterator() as $delivery) {
  50.             if ($delivery->getPositions()->has($item->getId())) {
  51.                 return true;
  52.             }
  53.         }
  54.         return false;
  55.     }
  56.     public function getShippingCosts(): PriceCollection
  57.     {
  58.         return new PriceCollection(
  59.             $this->map(function (Delivery $delivery) {
  60.                 return $delivery->getShippingCosts();
  61.             })
  62.         );
  63.     }
  64.     public function getAddresses(): CustomerAddressCollection
  65.     {
  66.         $addresses = new CustomerAddressCollection();
  67.         foreach ($this->getIterator() as $delivery) {
  68.             $address $delivery->getLocation()->getAddress();
  69.             if ($address !== null) {
  70.                 $addresses->add($delivery->getLocation()->getAddress());
  71.             }
  72.         }
  73.         return $addresses;
  74.     }
  75.     public function getApiAlias(): string
  76.     {
  77.         return 'cart_delivery_collection';
  78.     }
  79.     protected function getExpectedClass(): ?string
  80.     {
  81.         return Delivery::class;
  82.     }
  83. }