LineItemCollection.php in Payment 8.2
File
src/LineItemCollection.php
View source
<?php
namespace Drupal\payment;
use Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface;
class LineItemCollection implements LineItemCollectionInterface {
protected $currencyCode;
protected $lineItems = [];
public function __construct($currency_code = NULL, array $line_items = []) {
$this->currencyCode = $currency_code;
$this
->setLineItems($line_items);
}
public function setCurrencyCode($currency_code) {
$this->currencyCode = $currency_code;
return $this;
}
public function getCurrencyCode() {
return $this->currencyCode;
}
public function setLineItems(array $line_items) {
$this->lineItems = [];
foreach ($line_items as $line_item) {
$this
->setLineItem($line_item);
}
return $this;
}
public function setLineItem(PaymentLineItemInterface $line_item) {
$this->lineItems[$line_item
->getName()] = $line_item;
return $this;
}
public function unsetLineItem($name) {
unset($this->lineItems[$name]);
return $this;
}
public function getLineItems() {
return $this->lineItems;
}
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;
}
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;
}
public function getAmount() {
$total = 0;
foreach ($this
->getLineItems() as $line_item) {
$total = bcadd($total, $line_item
->getTotalAmount(), 6);
}
return $total;
}
}