BooleanMappingFailure::unrecognised()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 13
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Hydration\Mapping\Primitive;
4
5
use RuntimeException;
6
use Stratadox\HydrationMapping\Mapping;
7
use Stratadox\HydrationMapping\MappingFailure;
8
use function array_map;
9
use function implode;
10
use function var_export;
11
12
final class BooleanMappingFailure extends RuntimeException implements MappingFailure
13
{
14
    /** @param mixed $value */
15
    public static function unrecognised(
16
        $value,
17
        Mapping $mapping,
18
        array $trueValues,
19
        array $falseValues
20
    ): MappingFailure {
21
        return new self(sprintf(
22
            'The conversion to boolean failed for property `%s`: The input ' .
23
            'value %s is not among the true values (%s) nor the false values (%s)',
24
            $mapping->name(),
25
            var_export($value, true),
26
            self::formatList($trueValues),
27
            self::formatList($falseValues)
28
        ));
29
    }
30
31
    private static function formatList(array $values): string
32
    {
33
        return implode(', ', array_map(function ($value) {
34
            return var_export($value, true);
35
        }, $values));
36
    }
37
}
38