Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Format
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
10
100.00% covered (success)
100.00%
1 / 1
 value
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
8
 values
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Projom\Util;
6
7class Format
8{
9    const INT = 'int';
10    const FLOAT = 'float';
11    const BOOL = 'bool';
12    const STRING = 'string';
13    const DATE = 'date';
14    const DATETIME = 'datetime';
15
16    public static function value(mixed $value, string $type): mixed
17    {
18        $type = strtolower($type);
19        return match ($type) {
20            'int' => (int) $value,
21            'float' => (float) $value,
22            'bool' => (bool) $value,
23            'string' => (string) $value,
24            'date' => date('Y-m-d', strtotime((string) $value)),
25            'datetime' => date('Y-m-d H:i:s', strtotime((string) $value)),
26            default => $value,
27        };
28    }
29
30    public static function values(array $values): array
31    {
32        $formatted = [];
33        foreach ($values as $index => [$type, $value])
34            $formatted[$index] = static::value($value, $type);
35        return $formatted;
36    }
37}