Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
8 
16 {
20  protected $adjustmentPool;
21 
25  protected $adjustments;
26 
31 
36  public function __construct(
38  array $adjustments
39  ) {
40  $this->adjustmentPool = $adjustmentPool;
41  $this->adjustments = $adjustments;
42  }
43 
47  public function getItems()
48  {
49  if ($this->adjustmentInstances === null) {
50  $this->adjustmentInstances = $this->fetchAdjustments($this->adjustments);
51  }
53  }
54 
62  public function getItemByCode($adjustmentCode)
63  {
64  if ($this->adjustmentInstances === null) {
65  $this->adjustmentInstances = $this->fetchAdjustments($this->adjustments);
66  }
67 
68  if (!isset($this->adjustmentInstances[$adjustmentCode])) {
69  throw new \InvalidArgumentException(sprintf('Price adjustment "%s" is not found', $adjustmentCode));
70  }
71  return $this->adjustmentInstances[$adjustmentCode];
72  }
73 
78  protected function fetchAdjustments($adjustments)
79  {
80  $instances = [];
81  foreach ($adjustments as $code) {
82  $instances[$code] = $this->adjustmentPool->getAdjustmentByCode($code);
83  }
84 
85  uasort($instances, [$this, 'sortAdjustments']);
86 
87  return $instances;
88  }
89 
97  protected function sortAdjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment)
98  {
100  return 1;
101  }
102  return $firstAdjustment->getSortOrder() > $secondAdjustment->getSortOrder() ? 1 : -1;
103  }
104 }
sortAdjustments(AdjustmentInterface $firstAdjustment, AdjustmentInterface $secondAdjustment)
Definition: Collection.php:97
__construct(Pool $adjustmentPool, array $adjustments)
Definition: Collection.php:36
$code
Definition: info.phtml:12