IgBuS/php-project-lvl2

View on GitHub
src/parser.php

Summary

Maintainability
A
0 mins
Test Coverage
B
85%
<?php

namespace Biserg\Gendiff\Parser;

use Symfony\Component\Yaml\Yaml;

function parse($data, $dataType)
{
    $result = [
        "json" => fn($data) => json_decode($data),
        "yaml" => fn($data) => Yaml::parse($data, Yaml::PARSE_OBJECT_FOR_MAP),
        "yml" => fn($data) => Yaml::parse($data, Yaml::PARSE_OBJECT_FOR_MAP)
    ];

    if (!array_key_exists($dataType, $result)) {
        throw new \Exception("File type {$dataType} is not suported");
    }
    return $result[$dataType]($data);
}