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