You are here

final class ShippingRate in Commerce Shipping 8.2

Represents a shipping rate.

Hierarchy

Expanded class hierarchy of ShippingRate

8 files declare their use of ShippingRate
DynamicRate.php in tests/modules/commerce_shipping_test/src/Plugin/Commerce/ShippingMethod/DynamicRate.php
FlatRate.php in src/Plugin/Commerce/ShippingMethod/FlatRate.php
FlatRatePerItem.php in src/Plugin/Commerce/ShippingMethod/FlatRatePerItem.php
PromotionSubscriberTest.php in tests/src/Kernel/EventSubscriber/PromotionSubscriberTest.php
ShippingMethodBase.php in src/Plugin/Commerce/ShippingMethod/ShippingMethodBase.php

... See full list

File

src/ShippingRate.php, line 11

Namespace

Drupal\commerce_shipping
View source
final class ShippingRate {

  /**
   * The ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The shipping method ID.
   *
   * @var string
   */
  protected $shippingMethodId;

  /**
   * The shipping service.
   *
   * @var \Drupal\commerce_shipping\ShippingService
   */
  protected $service;

  /**
   * The original amount.
   *
   * @var \Drupal\commerce_price\Price
   */
  protected $originalAmount;

  /**
   * The amount.
   *
   * @var \Drupal\commerce_price\Price
   */
  protected $amount;

  /**
   * The description.
   *
   * @var string
   */
  protected $description;

  /**
   * The delivery date.
   *
   * @var \Drupal\Core\Datetime\DrupalDateTime
   */
  protected $deliveryDate;

  /**
   * Constructs a new ShippingRate object.
   *
   * @param array $definition
   *   The definition.
   */
  public function __construct(array $definition) {
    foreach ([
      'shipping_method_id',
      'service',
      'amount',
    ] as $required_property) {
      if (empty($definition[$required_property])) {
        throw new \InvalidArgumentException(sprintf('Missing required property %s.', $required_property));
      }
    }
    if (!$definition['service'] instanceof ShippingService) {
      throw new \InvalidArgumentException(sprintf('Property "service" should be an instance of %s.', ShippingService::class));
    }
    if (!$definition['amount'] instanceof Price) {
      throw new \InvalidArgumentException(sprintf('Property "amount" should be an instance of %s.', Price::class));
    }

    // The ID is not required because most shipping methods generate one
    // rate per service, and use the service ID when purchasing labels.
    if (empty($definition['id'])) {
      $shipping_method_id = $definition['shipping_method_id'];
      $service_id = $definition['service']
        ->getId();
      $definition['id'] = $shipping_method_id . '--' . $service_id;
    }
    $this->id = $definition['id'];
    $this->shippingMethodId = $definition['shipping_method_id'];
    $this->service = $definition['service'];
    $this->originalAmount = $definition['original_amount'] ?? $definition['amount'];
    $this->amount = $definition['amount'];
    $this->description = $definition['description'] ?? '';
    $this->deliveryDate = $definition['delivery_date'] ?? NULL;
  }

  /**
   * Gets the ID.
   *
   * @return string
   *   The ID.
   */
  public function getId() : string {
    return $this->id;
  }

  /**
   * Gets the shipping method ID.
   *
   * @return string
   *   The shipping method ID.
   */
  public function getShippingMethodId() : string {
    return $this->shippingMethodId;
  }

  /**
   * Gets the shipping service.
   *
   * The shipping service label is meant to be displayed when presenting rates
   * for selection.
   *
   * @return \Drupal\commerce_shipping\ShippingService
   *   The shipping service.
   */
  public function getService() : ShippingService {
    return $this->service;
  }

  /**
   * Gets the original amount.
   *
   * This is the amount before promotions and fees are applied.
   *
   * @return \Drupal\commerce_price\Price
   *   The original amount.
   */
  public function getOriginalAmount() : Price {
    return $this->originalAmount;
  }

  /**
   * Sets the original amount.
   *
   * @param \Drupal\commerce_price\Price $original_amount
   *   The original amount.
   *
   * @return $this
   */
  public function setOriginalAmount(Price $original_amount) {
    $this->originalAmount = $original_amount;
    return $this;
  }

  /**
   * Gets the amount.
   *
   * @return \Drupal\commerce_price\Price
   *   The amount.
   */
  public function getAmount() : Price {
    return $this->amount;
  }

  /**
   * Sets the amount.
   *
   * @param \Drupal\commerce_price\Price $amount
   *   The amount.
   *
   * @return $this
   */
  public function setAmount(Price $amount) {
    $this->amount = $amount;
    return $this;
  }

  /**
   * Gets the description.
   *
   * Displayed to the end-user when available.
   *
   * @return string
   *   The description
   */
  public function getDescription() : string {
    return $this->description;
  }

  /**
   * Sets the description.
   *
   * @param string $description
   *   The description.
   *
   * @return $this
   */
  public function setDescription(string $description) {
    $this->description = $description;
    return $this;
  }

  /**
   * Gets the delivery date, if known.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime|null
   *   The delivery date, or NULL.
   */
  public function getDeliveryDate() {
    return $this->deliveryDate;
  }

  /**
   * Sets the delivery date.
   *
   * @param \Drupal\Core\Datetime\DrupalDateTime $delivery_date
   *   The delivery date.
   *
   * @return $this
   */
  public function setDeliveryDate(DrupalDateTime $delivery_date) {
    $this->deliveryDate = $delivery_date;
    return $this;
  }

  /**
   * Gets the array representation of the shipping rate.
   *
   * @return array
   *   The array representation of the shipping rate.
   */
  public function toArray() : array {
    return [
      'id' => $this->id,
      'shipping_method_id' => $this->shippingMethodId,
      'service' => $this->service,
      'original_amount' => $this->originalAmount,
      'amount' => $this->amount,
      'description' => $this->description,
      'delivery_date' => $this->deliveryDate,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ShippingRate::$amount protected property The amount.
ShippingRate::$deliveryDate protected property The delivery date.
ShippingRate::$description protected property The description.
ShippingRate::$id protected property The ID.
ShippingRate::$originalAmount protected property The original amount.
ShippingRate::$service protected property The shipping service.
ShippingRate::$shippingMethodId protected property The shipping method ID.
ShippingRate::getAmount public function Gets the amount.
ShippingRate::getDeliveryDate public function Gets the delivery date, if known.
ShippingRate::getDescription public function Gets the description.
ShippingRate::getId public function Gets the ID.
ShippingRate::getOriginalAmount public function Gets the original amount.
ShippingRate::getService public function Gets the shipping service.
ShippingRate::getShippingMethodId public function Gets the shipping method ID.
ShippingRate::setAmount public function Sets the amount.
ShippingRate::setDeliveryDate public function Sets the delivery date.
ShippingRate::setDescription public function Sets the description.
ShippingRate::setOriginalAmount public function Sets the original amount.
ShippingRate::toArray public function Gets the array representation of the shipping rate.
ShippingRate::__construct public function Constructs a new ShippingRate object.