Skip to content

Commit

Permalink
Adding Force Enclosure for Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jul 31, 2023
1 parent 054a594 commit 475ca34
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 614 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ All Notable changes to `Csv` will be documented in this file

### Added

- `JsonEncoder` to allow converting huge CSV documents in JSON payload.
- `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

### Deprecated

Expand Down
169 changes: 0 additions & 169 deletions docs/9.0/converter/json.md

This file was deleted.

5 changes: 4 additions & 1 deletion docs/9.0/interoperability/enclose-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ title: Force Enclosure
# Force field 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.

<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-info">Deprecated since version <code>9.10.0</code>. You should instead use the
<code>Writer::forceEnclosure</code> method for better results and improved DX. Please refer to
<a href="/9.0/writer/#force-emclosure">its documentation</a> for more informations.</p>

## Usage with Writer objects

```php
Expand Down
42 changes: 39 additions & 3 deletions docs/9.0/writer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,50 @@ public Writer::getFlushThreshold(void): ?int
By default, `getFlushTreshold` returns `null`.

<p class="message-info"><code>Writer::insertAll</code> always flushes its buffer when all records are inserted, regardless of the threshold value.</p>

<p class="message-info">If set to <code>null</code> the inner flush mechanism of PHP's <code>fputcsv</code> will be used.</p>

## Force Enclosure

<p class="message-info">Since version <code>9.10.0</code> you can provide control the presence of enclosure around all records.</p>

```php
public Writer::forceEnclosure(): self
public Writer::relaxEnclosure(): self
```

By default, the `Writer` adds enclosures only around records that requires them. For all other records no enclosure character is present,
With this feature, you can force the enclosure to be present on every record entry or CSV cell. The inclusion will respect
the presence of enclosures inside the cell content as well as the presence of PHP's escape character.

```php
<?php
use League\Csv\Writer;

$collection = [
[1, 2],
['value 2-0', 'value 2-1'],
['to"to', 'foo\"bar'],
];

$writer = Writer::createFromString();
$writer->forceEnclosure();
$writer->insertAll($collection);
echo $writer->toString(), PHP_EOL;

// the CSV file will contain enclosed cell.
// Double quote are not added in presence of the
// escape character as per PHP's CSV writing documentation

// "1","2"
// "value 2-0","value 2-1"
// "to""to","foo\"bar"
```

## Records filtering

```php
public Writer::addFormatter(callable $callable): Writer
public Writer::addValidator(callable $callable, string $validatorName): Writer
public Writer::addFormatter(callable $callable): self
public Writer::addValidator(callable $callable, string $validatorName): self
```

Sometimes you may want to format and/or validate your records prior to their insertion into your CSV document. The `Writer` class provides a formatter and a validator mechanism to ease these operations.
Expand Down
1 change: 0 additions & 1 deletion docs/_data/menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ version:
Charset Converter: '/9.0/converter/charset/'
XML Converter: '/9.0/converter/xml/'
HTML Converter: '/9.0/converter/html/'
JSON Converter: '/9.0/converter/json/'
Extensions:
Overview: '/9.0/extensions/'
Doctrine: '/9.0/extensions/doctrine'
Expand Down
Loading

0 comments on commit 475ca34

Please sign in to comment.