slogsdon/php-flat-file

View on GitHub
src/FileParserFactory.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

namespace FlatFile;

use FlatFile\FileParser\FileParserInterface;
use FlatFile\FileParser\MarkdownFileParser;
use FlatFile\FileParser\PhpFileParser;

/**
 * Obtains correct file parser based on file attributes
 */
class FileParserFactory
{
    /** @var array */
    private $parsers;

    public function __construct(array $parsers = [])
    {
        if (empty($parsers)) {
            $parsers = $this->getDefaultParsers();
        }

        $this->parsers = $parsers;
    }

    public function createFrom(\SplFileInfo $file): FileParserInterface
    {
        foreach ($this->parsers as $parser) {
            if ($parser->canParse($file)) {
                return $parser;
            }
        }

        throw new \Exception('Unsupported file type');
    }

    public function getDefaultParsers(): array
    {
        return [
            new MarkdownFileParser,
            new PhpFileParser,
        ];
    }
}