megabgg/php-project-lvl2

View on GitHub
src/Formatters.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace CalcDiff\Formatters;

use Exception;

use function CalcDiff\Formatters\Stylish\getMapForStylish;
use function CalcDiff\Formatters\Plain\getMapForPlain;
use function CalcDiff\Formatters\Json\getMapForJson;

function getFormatter(string $format): array
{
    return match ($format) {
        'plain' => getMapForPlain(),
        'stylish' => getMapForStylish(),
        'json' => getMapForJson(),
        default => throw new Exception("Format not defined"),
    };
}

function boolToString(mixed $value): string
{
    return is_bool($value) ? ($value === true ? 'true' : 'false') : (is_null($value) ? 'null' : $value);
}