BooleanMappingFailure   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A unrecognised() 0 13 1
A formatList() 0 5 1
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