Skip to content

Commit 8aabf00

Browse files
committed
fix: set default open mode to w
1 parent eb95db0 commit 8aabf00

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/CsvFile.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ private function __construct(public string $file, public $stream)
1515
{
1616
}
1717

18-
public static function make(string $filename, string $mode): CsvFile
18+
/**
19+
* A list of possible modes. The default is 'w' (open for writing).:
20+
*
21+
* 'r' - Open for reading only; place the file pointer at the beginning of the file.
22+
* 'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
23+
* 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
24+
* 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
25+
* 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
26+
* 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
27+
* 'x' - Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen call will fail by returning false and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
28+
* 'x+' - Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen call will fail by returning false and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
29+
*
30+
* @see https://www.php.net/manual/en/function.fopen.php
31+
*/
32+
public static function make(string $filename, string $mode = 'w'): CsvFile
1933
{
2034
$csv = self::create($filename);
2135

@@ -42,6 +56,18 @@ public static function create(string $file): string
4256
return $file;
4357
}
4458

59+
public function append(
60+
array $fields,
61+
string $separator = ',',
62+
string $enclosure = '"',
63+
string $escape = '\\',
64+
string $eol = PHP_EOL
65+
): CsvFile {
66+
fputcsv($this->stream, $fields, $separator, $enclosure, $escape, $eol);
67+
68+
return $this;
69+
}
70+
4571
public static function blank(string $file): string
4672
{
4773
if (File::exists($file)) {
@@ -79,18 +105,6 @@ public function isEmpty(): bool
79105
return filesize($this->file) === 0;
80106
}
81107

82-
public function append(
83-
array $fields,
84-
string $separator = ',',
85-
string $enclosure = '"',
86-
string $escape = '\\',
87-
string $eol = PHP_EOL
88-
): CsvFile {
89-
fputcsv($this->stream, $fields, $separator, $enclosure, $escape, $eol);
90-
91-
return $this;
92-
}
93-
94108
public function insert(array $updates): CsvFile
95109
{
96110
foreach ($updates as $row) {

0 commit comments

Comments
 (0)