-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataParser.php
104 lines (86 loc) · 2.21 KB
/
DataParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Parse AmazonScraper data and output to csv.
*
* @author Costin G <[email protected]>
*/
use Carbon\Carbon;
use League\Csv\Writer;
use Tightenco\Collect\Support\Collection;
class DataParser
{
private $data;
private $csv;
private $computed;
public function __construct(Collection $data, string $fileName)
{
Carbon::setToStringFormat('jS \o\f F, Y');
$this->fileName = $fileName;
$this->data = $data;
$this->csv = Writer::createFromPath($this->fileName, "w");
$this->csv->insertOne(['orderid', 'date', 'product', 'status', 'price']);
$this->computed = $this->_buildData();
}
private function _buildData() : array
{
$computed =
[
'totalSpent' => 0,
'totalSpentItems' => 0,
'totalOrders' => 0,
'packagedReceived' => 0,
'totalItems' => 0,
'refundedItemsCount' => 0,
'totalRefunded' => 0,
'monthly' => [],
];
foreach ($this->data as $orderId => $order)
{
$orderedOn = new Carbon($order['date']);
$total = $order['price'];
$computed['totalSpent'] += $total;
$computed['totalOrders'] ++;
foreach ($order['shipments'] as $shipment)
{
$computed['packagedReceived'] ++;
$status = null;
if (strpos($shipment['status'], 'Delivered') !== false)
{
$status = 'delivered';
}
elseif (strpos($shipment['status'], 'Return') !== false)
{
$status = 'returned';
}
else
{
$status = $shipment['status'];
}
foreach ($shipment['items'] as $item)
{
if (!isset($item['price'])) {
$priceReadable = 'n/a';
$price = 0;
} else {
$priceReadable = $item['price'];
$price = $item['price'];
}
if ($status == 'returned') {
$computed['refundedItemsCount'] ++;
$computed['totalRefunded'] += $price;
}
$computed['totalItems'] ++;
$computed['totalSpentItems'] += $price;
$itemName = empty($item['name']) ? 'Uknown' : $item['name'];
$this->csv->insertOne([$orderId, $orderedOn, $itemName, $status, $priceReadable]);
}
}
}
return $computed;
}
public function getComputed() : array
{
return $this->computed;
}
}
?>