sonata-project/exporter

Make a distinction between writers and formatters

Opened this issue · 5 comments

Feature Request

Right now the Writers are actually Formatters and Writers in one. It would be much nicer to have Formatters and Writers. For example:

Formatters

  • XmlFormatter
  • JsonFormatter
  • RSSFormatter

Writers

  • FileWriter
  • ResponseWriter
  • StreamedResponseWriter
  • FlysystemWriter

This way the exporter component can be used for more usecases

Currently the typical usage is $writer = new CsvWriter('data.csv');
Maybe we could deprecate this in favor of $writer = new CsvFormatter(new FileWriter('data.csv')):

<?php
final class CsvWriter extends CsvFormatter
{
     public function __construct(
        string $filename,
        string $delimiter = ',',
        string $enclosure = '"',
        string $escape = '\\',
        bool $showHeaders = true,
        bool $withBom = false,
        string $terminate = "\n"
    ) {
        // trigger deprecation here
        return parent::__construct(
            new FileWriter($filename)
            $delimiter,
            $enclosure,
            $escape,
            $showHeaders,
            $terminate,
            $withBom
        );
    }
}

or the other way around with CsvWrite extends FileWriter

You could also only implement the WriterInterface, and implement both new classes. So both Formatters and Writers can be considered final classes.

final class CsvWriter implements WriterInterface
{
   private $writer;
   private $formatter;

   public function __construct(
        string $filename,
        string $delimiter = ',',
        string $enclosure = '"',
        string $escape = '\\',
        bool $showHeaders = true,
        bool $withBom = false,
        string $terminate = "\n"
    ) {
          $this->writer = new FileWriter($filename);
          $this->formatter = new CsvFormatter(
               $delimiter, 
               $enclosure, 
               $escape, 
               $showHeaders, 
               $withBom, 
               $terminate
          );
    }

    public function open()
    {
         // implement logic
    }

    public function write(array $data)
    {
         // implement logic
    }

    public function close()
    {
         // implement logic
    }
}

Even better! If you feel up to it please submit a pull request with only the CSVFormatter and the FileWriter, and necessary changes in other classes (like the Exporter maybe?)

stale commented

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

I agree. We should have a more consistent mechanism to compose the required combination between formatters and writers.

Currently, we have cases like #648, where we are using the source to provide arbitrary formats. At the same time, we also have dedicated formatters like FormattedBoolWriter, which are a more robust option IMO.
The problem is the lack of a configurable integration that allows the usage of the required formatters.

If I'm not misunderstanding the purpose of these formatters, we could replace the arbitrary options mentioned above by formatters like DateTimeFormatter, EnumFormatter, etc.