1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\Hydration\Mapping\Property\Type; |
5
|
|
|
|
6
|
|
|
use Stratadox\Hydration\Mapping\Property\UnmappableProperty; |
7
|
|
|
use Stratadox\HydrationMapping\KeyedMapping; |
8
|
|
|
use function assert; |
9
|
|
|
use function in_array; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Maps boolean-like input to a boolean property in an object property. |
13
|
|
|
* |
14
|
|
|
* @author Stratadox |
15
|
|
|
*/ |
16
|
|
|
final class BooleanValue extends ScalarValue |
17
|
|
|
{ |
18
|
|
|
/** @var mixed[] */ |
19
|
|
|
private $truths = [true, 1, '1']; |
20
|
|
|
/** @var mixed[] */ |
21
|
|
|
private $falsehoods = [false, 0, '0']; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Creates a new mapping for the boolean type object property. |
25
|
|
|
* |
26
|
|
|
* @param string $name The name of both the key and the property. |
27
|
|
|
* @param array $truths The values that should be considered true. |
28
|
|
|
* @param array $falsehoods The values that should be considered false. |
29
|
|
|
* @return KeyedMapping The boolean mapping object. |
30
|
|
|
*/ |
31
|
|
|
public static function withCustomTruths( |
32
|
|
|
string $name, |
33
|
|
|
array $truths, |
34
|
|
|
array $falsehoods |
35
|
|
|
): KeyedMapping { |
36
|
|
|
$instance = parent::inProperty($name); |
37
|
|
|
|
38
|
|
|
assert($instance instanceof BooleanValue); |
39
|
|
|
$instance->truths = $truths; |
40
|
|
|
$instance->falsehoods = $falsehoods; |
41
|
|
|
|
42
|
|
|
return $instance; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new mapping for the boolean type object property, using the |
47
|
|
|
* data from a specific key. |
48
|
|
|
* |
49
|
|
|
* @param string $name The name of the property. |
50
|
|
|
* @param string $key The array key to use. |
51
|
|
|
* @param array $truths The values that should be considered true. |
52
|
|
|
* @param array $falsehoods The values that should be considered false. |
53
|
|
|
* @return KeyedMapping The boolean mapping object. |
54
|
|
|
*/ |
55
|
|
|
public static function withCustomTruthsAndKey( |
56
|
|
|
string $name, |
57
|
|
|
string $key, |
58
|
|
|
array $truths, |
59
|
|
|
array $falsehoods |
60
|
|
|
): KeyedMapping { |
61
|
|
|
$instance = parent::inPropertyWithDifferentKey($name, $key); |
62
|
|
|
|
63
|
|
|
assert($instance instanceof BooleanValue); |
64
|
|
|
$instance->truths = $truths; |
65
|
|
|
$instance->falsehoods = $falsehoods; |
66
|
|
|
|
67
|
|
|
return $instance; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function value(array $data, $owner = null): bool |
72
|
|
|
{ |
73
|
|
|
$value = $this->my($data); |
74
|
|
|
if (in_array($value, $this->truths, true)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
if (in_array($value, $this->falsehoods, true)) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
throw UnmappableProperty::itMustBeConvertibleToBoolean($this, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|