1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Filesystem\Files;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Filesystem\File;
|
19
|
|
|
use O2System\Filesystem\Files\Abstracts\AbstractFile;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Class CsvFile
|
23
|
|
|
*
|
24
|
|
|
* @package O2System\Filesystem\Factory
|
25
|
|
|
*/
|
26
|
|
|
class CsvFile extends AbstractFile
|
27
|
|
|
{
|
28
|
|
|
/**
|
29
|
|
|
* CsvFile::$fileExtension
|
30
|
|
|
*
|
31
|
|
|
* @var string
|
32
|
|
|
*/
|
33
|
|
|
protected $fileExtension = '.csv';
|
34
|
|
|
|
35
|
|
|
// ------------------------------------------------------------------------
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* CsvFile::readFile
|
39
|
|
|
*
|
40
|
|
|
* @param string $filePath Path to the file.
|
41
|
|
|
* @param array $options Read file options.
|
42
|
|
|
*
|
43
|
|
|
* @return mixed
|
44
|
|
|
*/
|
45
|
|
|
public function readFile($filePath = null, array $options = [])
|
46
|
|
|
{
|
47
|
|
|
$filePath = empty($filePath)
|
48
|
|
|
? $this->filePath
|
49
|
|
|
: $filePath;
|
50
|
|
|
|
51
|
|
|
$defaultOptions = [
|
52
|
|
|
'length' => 1000,
|
53
|
|
|
'delimiter' => ',',
|
54
|
|
|
];
|
55
|
|
|
|
56
|
|
|
$options = array_merge($defaultOptions, $options);
|
57
|
|
|
|
58
|
|
|
$result = [];
|
59
|
|
|
|
60
|
|
|
if (false !== ($handle = fopen($filePath, 'r'))) {
|
61
|
|
|
while (false !== ($data = fgetcsv($handle, $options[ 'length' ], $options[ 'delimiter' ]))) {
|
|
|
|
|
62
|
|
|
$result[] = fgetcsv($handle);
|
63
|
|
|
}
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
return $result;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* CsvFile::writeFile
|
73
|
|
|
*
|
74
|
|
|
* @param string $filePath Path to the file.
|
75
|
|
|
* @param array $options Write file options.
|
76
|
|
|
*
|
77
|
|
|
* @return bool Returns TRUE on success or FALSE on failure.
|
78
|
|
|
*/
|
79
|
|
|
public function writeFile($filePath = null, array $options = [])
|
80
|
|
|
{
|
81
|
|
|
$filePath = empty($filePath)
|
82
|
|
|
? $this->filePath
|
83
|
|
|
: $filePath;
|
84
|
|
|
|
85
|
|
|
$handle = (new File())->create($filePath);
|
86
|
|
|
|
87
|
|
|
foreach ($this->getArrayCopy() as $key => $value) {
|
88
|
|
|
if ( ! is_array($value)) {
|
89
|
|
|
$list = [$key, $value];
|
90
|
|
|
} else {
|
91
|
|
|
$list = $value;
|
92
|
|
|
}
|
93
|
|
|
fputcsv($handle, $list);
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
return fclose($handle);
|
97
|
|
|
}
|
98
|
|
|
} |