-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCSVResultsFormatter.php
152 lines (134 loc) · 4.01 KB
/
CSVResultsFormatter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once "Request.php";
require_once "Results.php";
require_once "PlayersInput.php";
require_once "PlayersOutput.php";
class CSVResultsFormatter
{
private $pointsLeague = false;
public function buildCSV($request, $results, $outputSimpleCSV, $pointsLeague)
{
$this->pointsLeague = $pointsLeague;
header("Content-type: text/csv");
$fileName = $this->buildCSVFileName($request);
header('Content-Disposition: attachment; filename="' . $fileName . '"');
$outstream = fopen("php://output",'w');
fwrite($outstream, $this->buildPlayerLines($request, $results, $request->outputAsSimpleCSV));
fclose($outstream);
}
private function buildCSVFileName($request)
{
$filename = "";
$fileName .= $request->numberOfTeams . "TM-";
$fileName .= $request->league . "-";
if (!$this->pointsLeague)
{
$fileName .= count($request->hittersInput->categories) . "x" . count($request->pitchersInput->categories);
}
else
{
$fileName .= "PTS";
}
$fileName .= ".csv";
return $fileName;
}
private function addToArray($origArray, $addedArray)
{
foreach($addedArray as $newItem)
{
if (!in_array($newItem, $origArray))
{
$origArray[] = $newItem;
}
}
return $origArray;
}
private function addKeysToArray($origArray, $addedArray)
{
foreach($addedArray as $newItem => $value)
{
if (!in_array($newItem, $origArray))
{
$origArray[] = $newItem;
}
}
return $origArray;
}
private function buildPlayerLines($request, $results, $outputSimpleCSV)
{
if (!$outputSimpleCSV)
{
$cols = array("mlbamID", "statsID", "cbsID", "espnID", "playerName", "team", "league", "pos", "total", "adjTotal", "dollarValue", "adjustedDollarValue", "keeperPrice");
if (!$this->pointsLeague)
{
$cols = $this->addToArray($cols, $request->hittersInput->categories);
$cols = $this->addToArray($cols, $results->hittersOutput->components);
foreach($request->hittersInput->categories as $category)
{
$cols[] = "m" . $category;
}
$cols = $this->addToArray($cols, $request->pitchersInput->categories);
$cols = $this->addToArray($cols, $results->pitchersOutput->components);
foreach($request->pitchersInput->categories as $category)
{
$cols[] = "m" . $category;
}
}
else
{
$cols = $this->addKeysToArray($cols, $request->hittersInput->categories);
$cols = $this->addKeysToArray($cols, $request->pitchersInput->categories);
}
foreach($cols as $col)
{
$lines .= $col . ",";
}
$lines .= "\n";
}
else
{
$cols = array("statsID", "cbsID", "espnID", "dollarValue");
}
foreach($results->hitters as $player)
{
foreach($cols as $col)
{
if ($col == "pos" && is_array($player["pos"]))
{
foreach($player["pos"] as $pos)
{
$lines .= $pos . "-";
}
$lines[strlen($lines) - 1] = ",";
}
else
{
$lines .= $player[$col] . ",";
}
}
$lines .= "\n";
}
foreach($results->pitchers as $player)
{
foreach($cols as $col)
{
if ($col == "pos" && is_array($player["pos"]))
{
foreach($player["pos"] as $pos)
{
$lines .= $pos . "-";
}
$lines[strlen($lines) - 1] = ",";
}
else
{
$lines .= $player[$col] . ",";
}
}
$lines .= "\n";
}
$lines .= "\n";
return $lines;
}
}
?>