Skip to content

Commit

Permalink
Deprecated methods to prepare for version 10
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 4, 2023
1 parent 49c821b commit 04d2b14
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ All Notable changes to `Csv` will be documented in this file

- `Stream::fwrite` to allow writing to a file in a normalized way. Internal use.
- `Writer::forceEnclosure` and `Writer::relaxEnclosure` to control the presence of enclosure in the generated CSV
- `Writer::getEndOfLine` and `Writer::setEndOfLine`

### Deprecated

- `EncloseField` stream filter in favor of the new `Writer::forceEnclosure` method.
- `Writer::getNewline` and `Writer::setNewline` in favor of `Writer::getEndOfLine` and `Writer::setEndOfLine`

### Fixed

Expand Down
4 changes: 3 additions & 1 deletion docs/9.0/interoperability/enclose-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ title: Force Enclosure

<p class="message-info">Available since version <code>9.1.0</code></p>
The `EncloseField` is a PHP stream filter which forces the `Writer` class to enclose all its record fields.

M.;L'
\
<p class="message-warning">Changing the CSV objects control characters <strong>after registering the stream filter</strong> may result in unexpected returned records.</p>

<p class="message-warning">Deprecated since version <code>9.10.0</code>. You should instead use the
Expand Down
13 changes: 8 additions & 5 deletions docs/9.0/writer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ Because PHP's `fputcsv` implementation has a hardcoded `\n`, we need to be able

### Description

<p class="message-notice"><code>setEndOfline</code> and <code>getEndOfLine</code> are available since version <code>9.10.0</code>.</p>

```php
public Writer::setNewline(string $sequence): self
public Writer::getNewline(void): string
public Writer::setEndOfline(string $sequence): self
public Writer::getEndOfLine(void): string
```

### Example
Expand All @@ -95,13 +97,14 @@ public Writer::getNewline(void): string
use League\Csv\Writer;

$writer = Writer::createFromFileObject(new SplFileObject());
$newline = $writer->getNewline(); //equals "\n";
$writer->setNewline("\r\n");
$newline = $writer->getNewline(); //equals "\r\n";
$newline = $writer->getEndOfLine(); //equals "\n";
$writer->setEndOfLine("\r\n");
$newline = $writer->getEndOfLine(); //equals "\r\n";
$writer->insertOne(["one", "two"]);
echo $writer->toString(); //displays "one,two\r\n";
```

<p class="message-notice"><code>setNewline</code> and <code>getNewLine</code> are deprecated since version <code>9.10.0</code>.</p>
<p class="message-info">The default newline sequence is <code>\n</code>;</p>
<p class="message-warning">If you are using a non-seekable CSV document, changing the newline character will trigger an exception.</p>

Expand Down
38 changes: 33 additions & 5 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ protected function resetProperties(): void
}

/**
* Returns the current newline sequence characters.
* Returns the current end of line sequence characters.
*/
public function getNewline(): string
public function getEndOfLine(): string
{
return $this->newline;
}
Expand Down Expand Up @@ -173,11 +173,11 @@ public function addValidator(callable $validator, string $validator_name): self
}

/**
* Sets the newline sequence.
* Sets the end of line sequence.
*/
public function setNewline(string $newline): self
public function setEndOfLine(string $endOfLine): self
{
$this->newline = $newline;
$this->newline = $endOfLine;

return $this;
}
Expand Down Expand Up @@ -273,4 +273,32 @@ protected function consolidate(): int

return 0;
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @see Writer::getEndOfLine()
* @deprecated Since version 9.10.0
* @codeCoverageIgnore
*
* Returns the current newline sequence characters.
*/
public function getNewline(): string
{
return $this->getEndOfLine();
}

/**
* DEPRECATION WARNING! This method will be removed in the next major point release.
*
* @see Writer::setEndOfLine()
* @deprecated Since version 9.10.0
* @codeCoverageIgnore
*
* Sets the newline sequence.
*/
public function setNewline(string $newline): self
{
return $this->setEndOfLine($newline);
}
}
8 changes: 4 additions & 4 deletions src/WriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public function testCustomNewline(): void
/** @var resource $resource */
$resource = tmpfile();
$csv = Writer::createFromStream($resource);
self::assertSame("\n", $csv->getNewline());
$csv->setNewline("\r\n");
self::assertSame("\n", $csv->getEndOfLine());
$csv->setEndOfLine("\r\n");
$csv->insertOne(['jane', 'doe']);
self::assertSame("jane,doe\r\n", $csv->toString());
$csv = null;
Expand Down Expand Up @@ -208,7 +208,7 @@ public function testWriterTriggerExceptionWithNonSeekableStream(): void
{
$this->expectException(UnavailableStream::class);
$writer = Writer::createFromPath('php://null', 'w');
$writer->setNewline("\r\n");
$writer->setEndOfLine("\r\n");
$writer->insertOne(['foo', 'bar']);
}

Expand All @@ -222,7 +222,7 @@ public function testRFC4180WriterMode(string $expected, array $record): void
{
foreach (["\r\n", "\n", "\r"] as $eol) {
$csv = Writer::createFromString();
$csv->setNewline($eol);
$csv->setEndOfLine($eol);
$csv->setEscape('');
$csv->insertOne($record);
self::assertSame($expected.$eol, $csv->toString());
Expand Down

0 comments on commit 04d2b14

Please sign in to comment.