Reputation: 122
Hi I'm writing a CSV/XML package for my own project, I'm not using packages around there because I need to render for some cases CSVs with more than 100.000.000 of rows. So I figured my basic solution doesn't work when the database queries take long time, then I wrote a script to download the file batching it. It's working I just want extend and re-factor it for XML. Would be great do it with a good design pattern.
Upvotes: 1
Views: 749
Reputation: 5133
Several libraries working on this kind of subject in PHP are implementing a design pattern adapter because standard PHP libraries (that you should use if you want performances) associated to each kind of file provide a completely different interface.
So, in fact, you just have to find your own interface (you can do that with many interfaces if you can identify different functional aspects).
Example of standard library for CSV in PHP: http://php.net/manual/fr/function.fgetcsv.php
Example of abstraction tool on type of file: https://github.com/KnpLabs/Gaufrette
Upvotes: 2
Reputation: 34199
This question is primarily opinion-based - I will tell how I have solved this problem.
I have recently met the same problem in one of my projects and I decided to solve this problem with writing reading and writing interfaces.
As result, I had a code like:
public interface IReportReader
{
Report ReadFromFile(string filePath);
Report ReadFromStream(Stream stream);
}
public interface IReportWriter
{
void WriteToFile(string filePath, Report report);
void WriteToStream(Stream stream, Report report);
}
Then, I have implemented two implementations:
StubConsoleReportReader
\ StubConsoleReportWriter
- just for debug purposes.
CSVReportReader
\ CSVReportWriter
.
Later, I have easily added XML, HTML and word output formats.
As for me, this pattern suits great because:
1. It is plain and simple, without over-engineering, can be easily extended;
2. These two interfaces describe two fundamental operations - input and output, which are fitting any existing file types and formats.
Upvotes: 1