This is a command-line PHP application that reads product data from CSV or TSV files, prints each product, and outputs a file that counts unique product combinations.
- PHP 7.0 or higher
- Composer (optional, for autoloading/testing)
- Terminal/Command Line access
php parser.php --file=examples/products_tab_separated.tsv --unique-combinations=output/combination_count.csv--file: Input CSV or TSV file with product data--unique-combinations: Output file to save combination counts (optional)
Each product should have the following fields:
| Field Name | Required | Description |
|---|---|---|
| brand_name | ✅ | Brand (e.g. Apple) |
| model_name | ✅ | Model (e.g. iPhone 13) |
| condition_name | ❌ | Condition (e.g. Working) |
| grade_name | ❌ | Grade (e.g. Grade A) |
| gb_spec_name | ❌ | Storage (e.g. 128GB) |
| colour_name | ❌ | Colour (e.g. Blue) |
| network_name | ❌ | Network (e.g. Unlocked) |
Missing make or model will throw an exception.
| make | model | colour | capacity | network | grade | condition | count |
|---|---|---|---|---|---|---|---|
| Apple | iPhone 6s Plus | Red | 256GB | Unlocked | Grade A | Working | 129 |
- Add unit tests using PHPUnit or Pest
- Extend for JSON/XML support
.
├── Product.php
├── ProductParser.php
├── parser.php
├── tests/
│ └── ProductTest.php
├── examples/
│ └── products_tab_separated.tsv
├── output/
│ └── combination_count.csv (generated)
├── README.md
├── composer.json
└── .gitignore
Basic test cases are included using PHPUnit to verify:
✅ Successful creation of a Product
❌ Exception thrown when make or model is missing
./vendor/bin/phpunit tests
💡 Bonus: Extending Support for JSON & XML The parser is structured in a way that makes it easy to support additional formats like .json or .xml.
- Here’s how your parse() method inside ProductParser.php is designed:
public function parse(string $filename): array
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
switch ($ext) {
case 'csv':
case 'tsv':
return $this->parseCsv($filename);
case 'json':
return $this->parseJson($filename); // (To be implemented)
case 'xml':
return $this->parseXml($filename); // (To be implemented)
default:
throw new Exception("Unsupported file format: $ext");
}
}
- You can add parseJson() and parseXml() methods in the future to expand functionality with minimal changes.