This repository was archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataManager.php
More file actions
56 lines (44 loc) · 1.61 KB
/
DataManager.php
File metadata and controls
56 lines (44 loc) · 1.61 KB
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
<?php
include 'Client.php';
include 'Formatter.php';
abstract class DataManager
{
public static function importAndReadData($inputFile)
{
$importData = [];
foreach ($inputFile as $line) {
$importData[] = explode(',', trim($line));
}
//Removing and formatting header array from the rest of the data.
$headers = Formatter::removeQuotes(array_shift($importData));
//Main export content string that will be written into output file.
$exportContent = "\n";
foreach ($importData as $client) {
$client = Formatter::removeQuotes($client);
$sortedData = [];
//Making associative array with headers and respective properties.
for ($i = 0; $i < count($headers); $i++) {
$sortedData[$headers[$i]] = $client[$i];
}
//Assigning properties to new Client class object.
$newClient = new Client
(
$sortedData['name'],
$sortedData['surname'],
$sortedData['address'],
$sortedData['city'],
$sortedData['gender'],
$sortedData['soc_security_num']
);
//Adding Client info as a new line to export content.
$exportContent .= Formatter::formatOutputData($newClient) . PHP_EOL;
}
self::exportData(rtrim($exportContent));
}
public static function exportData(string $contentToAdd)
{
$exportFile = fopen('output_data.csv', 'a');
fwrite($exportFile, $contentToAdd);
fclose($exportFile);
}
}