PluginProbe
MBE eShip / trunk
MBE eShip vtrunk
2.8.1 trunk 1.0.0 1.1.0 1.1.3 1.2.1 1.2.2 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.7.0 1.7.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1.0 2.1.1 2.1.2 2.2.1 All 37 releases
mail-boxes-etc / lib / Helper / Csv.php

Csv.php in MBE eShip trunk, at lib/Helper/Csv.php

88 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MbeExceptions\ValidationException;
4
5 class Mbe_Shipping_Helper_Csv
6 {
7 public function getCsvHeaderKnowDefinitions()
8 {
9 return $this->_csvHeaderKnowDefinitions;
10 }
11
12 /**
13 * @throws ValidationException
14 */
15 public function readFile($importFilePath, $headerDefinition = [])
16 {
17 $file = fopen($importFilePath, "r");
18 $i = 0;
19 $header = null;
20 $result = [];
21 while (!feof($file)) {
22 $currentRow = fgetcsv($file, 0, ",", '"');
23
24 if (!empty($currentRow) && ([null]!==$currentRow)) {
25 if ($i == 0) {
26 //File heading
27 $header = $currentRow;
28 } else {
29 try {
30 $current = $this->readCsvRowToArray( $headerDefinition, $header, $currentRow );
31 $result[] = $current;
32 } catch (ValidationException $e) {
33 throw new ValidationException(esc_html($e->getMessage() . ' file:'. basename($importFilePath) ));
34 }
35 }
36 }
37 $i++;
38 }
39 return $result;
40 }
41
42 /**
43 * @throws ValidationException
44 */
45 function readCsvRowToArray($headerDefinition, $header, $row)
46 {
47 $result = [];
48 if (is_array($row) && (count($row) === count($headerDefinition))) {
49 foreach ($row as $index => $currentRowValue) {
50 $headerName = $this->cleanString($header[$index]);
51 $currentKey = $headerName;
52 if (isset($headerDefinition[$headerName])) {
53 $currentKey = $headerDefinition[$headerName];
54 }
55 $currentRowValue = trim($currentRowValue);
56 /*
57 $currentRowValue = str_replace(" ", " ", $currentRowValue);
58 $currentRowValue = str_replace(" ", " ", $currentRowValue);
59 $currentRowValue = str_replace(" ", " ", $currentRowValue);
60 $currentRowValue = str_replace("„",createShipment "\"", $currentRowValue);
61 $currentRowValue = str_replace("“", "\"", $currentRowValue);
62 $currentRowValue = str_replace("”", "\"", $currentRowValue);
63 */
64 $result[$currentKey] = $currentRowValue;
65 }
66 } else {
67 throw new ValidationException(esc_html__('Expected ('. count($headerDefinition) .') columns, found ('. count($row) .').', 'mail-boxes-etc'));
68 }
69 return $result;
70 }
71
72 function cleanString($str)
73 {
74 $result = $str;
75 $result = trim($result);
76 $result = strtolower($result);
77 $result = str_replace(" ", "_", $result);
78 $result = str_replace(chr(160), "_", $result);
79 $result = str_replace(chr(194), "_", $result);
80 $result = str_replace(chr(195), "_", $result);
81 $result = str_replace(",", "_", $result);
82 $result = str_replace("/", "", $result);
83 $result = str_replace("(", "", $result);
84 $result = str_replace(")", "", $result);
85 $result = str_replace("__", "_", $result);
86 return $result;
87 }
88 }