Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ColumnResolver.php
Go to the documentation of this file.
1 <?php
8 
10 {
11  const COLUMN_COUNTRY = 'Country';
12  const COLUMN_REGION = 'Region/State';
13  const COLUMN_ZIP = 'Zip/Postal Code';
14  const COLUMN_WEIGHT = 'Weight (and above)';
15  const COLUMN_WEIGHT_DESTINATION = 'Weight (and above)';
16  const COLUMN_PRICE = 'Shipping Price';
17 
21  private $nameToPositionIdMap = [
22  self::COLUMN_COUNTRY => 0,
23  self::COLUMN_REGION => 1,
24  self::COLUMN_ZIP => 2,
25  self::COLUMN_WEIGHT => 3,
26  self::COLUMN_WEIGHT_DESTINATION => 3,
27  self::COLUMN_PRICE => 4,
28  ];
29 
33  private $headers;
34 
40  public function __construct(array $headers, array $columns = [])
41  {
42  $this->nameToPositionIdMap = array_merge($this->nameToPositionIdMap, $columns);
43  $this->headers = array_map('trim', $headers);
44  }
45 
52  public function getColumnValue($column, array $values)
53  {
54  $column = (string) $column;
55  $columnIndex = array_search($column, $this->headers, true);
56  if (false === $columnIndex) {
57  if (array_key_exists($column, $this->nameToPositionIdMap)) {
58  $columnIndex = $this->nameToPositionIdMap[$column];
59  } else {
60  throw new ColumnNotFoundException(__('Requested column "%1" cannot be resolved', $column));
61  }
62  }
63 
64  if (!array_key_exists($columnIndex, $values)) {
65  throw new ColumnNotFoundException(__('Column "%1" not found', $column));
66  }
67 
68  return trim($values[$columnIndex]);
69  }
70 }
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$columns
Definition: default.phtml:15