*/ final class FileSource implements IteratorAggregate { private string $filePath; /** @var Traversable */ private Traversable $delegate; public function __construct(SplFileObject $file) { $this->filePath = $file->getPathname(); $content = $file->fread($file->getSize()); /** @infection-ignore-all */ if ($content === false || $content === '') { throw new UnableToReadFile($this->filePath); } $this->delegate = match (strtolower($file->getExtension())) { 'json' => new JsonSource($content), 'yaml', 'yml' => new YamlSource($content), default => throw new FileExtensionNotHandled($file->getExtension()), }; } /** * @return Iterator */ public function getIterator(): Iterator { yield from $this->delegate; } }