Php script to read files with different formats

46 views Asked by At

I am developing a script in php that receives as a parameter by console the name of a file that can have 2 different formats (XML or csv).

The idea is that it is as scalable as possible, being able to add another type of data input, for example "txt" files or other types.

That's why I was thinking of using the "Adapter" and "interfaces" patterns. Conceptually I had thought of a "Files" interface and then 2 classes called "CsvFile" and "XmlFile", which implement "Files" like this

Interface Files

<?php

namespace Class\Interfaces;


interface Files
{
    public function readFile();
}

CsvFile Class

<?php

namespace Class\Interfaces;

class CsvFile implements Files
{
    private $file;

    public function __construct(string $file)
    {
        $this->file = $file;
    }


    public function readFile() : void
    {
        echo $this->file;
    }
}

CsvFileAdapter

<?php

class CsvFileAdapter implements CsvFile
{
    protected $csvFile;

    public function __construct(CsvFile $csvFile)
    {
        $this->csvFile = $csvFile;
    }

    public function readFile()
    {
        return $this->csvFile->readFile();
    }
}

This is a bit of the skeleton that I have assembled so far, but I don't know very well if I am implementing the "Adapter" pattern correctly.

0

There are 0 answers