<commit_before># Writers

Writers output the data to  
take the data from the [readers](readers.md)

## ArrayWriter

Resembles the [ArrayReader](#arrayreader). Probably most useful for testing
your workflow.

## CsvWriter

Writes CSV files.

{!include/csv.md!}

Then use the writer:

```php
use Port\Csv\CsvWriter;

$writer = new CsvWriter();
$writer->setStream(fopen('output.csv', 'w'));

// Write column headers:
$writer->writeItem(['first', 'last']);

$writer
    ->writeItem(['James', 'Bond'])
    ->writeItem(['Auric', 'Goldfinger'])
    ->finish();
```

## DoctrineWriter

Writes data through the [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html)
and [ODM](http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/).

{!include/doctrine.md!}

```php
use Port\Doctrine\DoctrineWriter;

$writer = new DoctrineWriter($objectManager, 'YourNamespace:Employee');
$writer
    ->prepare()
    ->writeItem(
        [
            'first' => 'James',
            'last'  => 'Bond'
        ]
    )
    ->finish();
```

By default, DoctrineWriter will truncate your data before running the workflow.
Call `disableTruncate()` if you don't want this.

If you are not truncating data, DoctrineWriter will try to find an entity having it's primary key set to the value of
the first column of the item. If it finds one, the entity will be updated, otherwise it's inserted.
You can tell DoctrineWriter to lookup the entity using different columns of your item by passing a third parameter to
it's constructor.

```php
$writer = new DoctrineWriter($entityManager, 'YourNamespace:Employee', 'columnName');
```

or

```php
$writer = new DoctrineWriter($entityManager, 'YourNamespace:Employee', ['column1', 'column2', 'column3']);
```

The DoctrineWriter will also search out associations automatically and link them by an entity reference. For example
suppose you have a Product entity that you are importing and must be associated to a Category. If there is a field in
the import file named 'Category' with an id, the writer will use metadata to get the association class and create a
reference so that it can be associated properly. The DoctrineWriter will skip any association fields that are already
objects in cases where a converter was used to retrieve the association.

## ExcelWriter

Writes data to an Excel file. 

{!include/excel.md!}

Then construct an ExcelWriter:

```php
use Port\Excel\ExcelWriter;

$file = new \SplFileObject('data.xlsx', 'w');
$writer = new ExcelWriter($file);

$writer
    ->prepare()
    ->writeItem(['first', 'last'])
    ->writeItem(['first' => 'James', 'last' => 'Bond'])
    ->finish();
```

You can specify the name of the sheet to write to:

```php<commit_msg>Remove fluent interface from docs<commit_after># Writers

Writers output the data to  
take the data from the [readers](readers.md)

## ArrayWriter

Resembles the [ArrayReader](#arrayreader). Probably most useful for testing
your workflow.

## CsvWriter

Writes CSV files.

{!include/csv.md!}

Then use the writer:

```php
use Port\Csv\CsvWriter;

$writer = new CsvWriter();
$writer->setStream(fopen('output.csv', 'w'));

// Write column headers:
$writer->writeItem(['first', 'last']);

// Write some data
$writer->writeItem(['James', 'Bond']);
$writer->writeItem(['Auric', 'Goldfinger'])

$writer->finish();
```

## DoctrineWriter

Writes data through the [Doctrine ORM](http://www.doctrine-project.org/projects/orm.html)
and [ODM](http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/).

{!include/doctrine.md!}

```php
use Port\Doctrine\DoctrineWriter;

$writer = new DoctrineWriter($objectManager, 'YourNamespace:Employee');
$writer->prepare();
$writer->writeItem(
    [
        'first' => 'James',
        'last'  => 'Bond'
    ]
);
$writer->finish();
```

By default, DoctrineWriter will truncate your data before running the workflow.
Call `disableTruncate()` if you don't want this.

If you are not truncating data, DoctrineWriter will try to find an entity having it's primary key set to the value of
the first column of the item. If it finds one, the entity will be updated, otherwise it's inserted.
You can tell DoctrineWriter to lookup the entity using different columns of your item by passing a third parameter to
it's constructor.

```php
$writer = new DoctrineWriter($entityManager, 'YourNamespace:Employee', 'columnName');
```

or

```php
$writer = new DoctrineWriter($entityManager, 'YourNamespace:Employee', ['column1', 'column2', 'column3']);
```

The DoctrineWriter will also search out associations automatically and link them by an entity reference. For example
suppose you have a Product entity that you are importing and must be associated to a Category. If there is a field in
the import file named 'Category' with an id, the writer will use metadata to get the association class and create a
reference so that it can be associated properly. The DoctrineWriter will skip any association fields that are already
objects in cases where a converter was used to retrieve the association.

## ExcelWriter

Writes data to an Excel file. 

{!include/excel.md!}

Then construct an ExcelWriter:

```php
use Port\Excel\ExcelWriter;

$file = new \SplFileObject('data.xlsx', 'w');
$writer = new ExcelWriter($file);

$writer->prepare();
$writer->writeItem(['first', 'last'])
$writer->writeItem(['first' => 'James', 'last' => 'Bond'])
$writer->finish();
```

You can specify the name of the sheet to write to:

```php
{'fraction_non_alphanumeric': 0.12600585223116312, 'fraction_numerical': 0.0010972933430870519, 'mean_word_length': 4.632337796086508, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2589385', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1579, 'n_words': 615}