You are here

class LineItemCollection in Payment 8.2

Provides a line item collection.

Hierarchy

Expanded class hierarchy of LineItemCollection

2 files declare their use of LineItemCollection
LineItemCollectionTest.php in tests/src/Unit/LineItemCollectionTest.php
PaymentLineItemOverview.php in src/Plugin/Field/FieldFormatter/PaymentLineItemOverview.php

File

src/LineItemCollection.php, line 10

Namespace

Drupal\payment
View source
class LineItemCollection implements LineItemCollectionInterface {

  /**
   * The line items' ISO 4217 currency code.
   *
   * @var string|null $currency_code
   *   The currency code or NULL if the collection itself has no specific
   *   currency.
   */
  protected $currencyCode;

  /**
   * The line items.
   *
   * @var \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface[]
   *   Keys are line item names.
   */
  protected $lineItems = [];

  /**
   * Constructs a new instance.
   *
   * @param string $currency_code
   * @param \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface[] $line_items
   */
  public function __construct($currency_code = NULL, array $line_items = []) {
    $this->currencyCode = $currency_code;
    $this
      ->setLineItems($line_items);
  }

  /**
   * {@inheritdoc}
   */
  public function setCurrencyCode($currency_code) {
    $this->currencyCode = $currency_code;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCurrencyCode() {
    return $this->currencyCode;
  }

  /**
   * {@inheritdoc}
   */
  public function setLineItems(array $line_items) {
    $this->lineItems = [];
    foreach ($line_items as $line_item) {
      $this
        ->setLineItem($line_item);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLineItem(PaymentLineItemInterface $line_item) {
    $this->lineItems[$line_item
      ->getName()] = $line_item;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function unsetLineItem($name) {
    unset($this->lineItems[$name]);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getLineItems() {
    return $this->lineItems;
  }

  /**
   * {@inheritdoc}
   */
  public function getLineItem($name) {
    $line_items = $this
      ->getLineItems();
    foreach ($line_items as $delta => $line_item) {
      if ($line_item
        ->getName() == $name) {
        return $line_item;
      }
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getLineItemsByType($plugin_id) {
    $line_items = [];
    foreach ($this
      ->getLineItems() as $name => $line_item) {
      if ($line_item
        ->getPluginId() == $plugin_id) {
        $line_items[$name] = $line_item;
      }
    }
    return $line_items;
  }

  /**
   * {@inheritdoc}
   */
  public function getAmount() {
    $total = 0;
    foreach ($this
      ->getLineItems() as $line_item) {
      $total = bcadd($total, $line_item
        ->getTotalAmount(), 6);
    }
    return $total;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LineItemCollection::$currencyCode protected property The line items' ISO 4217 currency code.
LineItemCollection::$lineItems protected property The line items.
LineItemCollection::getAmount public function Gets the line items' total amount. Overrides LineItemCollectionInterface::getAmount
LineItemCollection::getCurrencyCode public function Gets the line items' ISO 4217 currency code. Overrides LineItemCollectionInterface::getCurrencyCode
LineItemCollection::getLineItem public function Gets a line item. Overrides LineItemCollectionInterface::getLineItem
LineItemCollection::getLineItems public function Gets all line items. Overrides LineItemCollectionInterface::getLineItems
LineItemCollection::getLineItemsByType public function Gets line items by plugin type. Overrides LineItemCollectionInterface::getLineItemsByType
LineItemCollection::setCurrencyCode public function Sets the line items' ISO 4217 currency code. Overrides LineItemCollectionInterface::setCurrencyCode
LineItemCollection::setLineItem public function Sets a line item. Overrides LineItemCollectionInterface::setLineItem
LineItemCollection::setLineItems public function Sets line items. Overrides LineItemCollectionInterface::setLineItems
LineItemCollection::unsetLineItem public function Unsets a line item. Overrides LineItemCollectionInterface::unsetLineItem
LineItemCollection::__construct public function Constructs a new instance.