exporter icon indicating copy to clipboard operation
exporter copied to clipboard

Make a distinction between writers and formatters

Open leroy0211 opened this issue 5 years ago • 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

leroy0211 avatar Apr 17 '19 10:04 leroy0211

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

greg0ire avatar Apr 17 '19 20:04 greg0ire

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
    }
}

leroy0211 avatar Apr 18 '19 12:04 leroy0211

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?)

greg0ire avatar Apr 18 '19 19:04 greg0ire

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.

stale[bot] avatar Jan 30 '20 19:01 stale[bot]

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.

phansys avatar Oct 22 '23 11:10 phansys