| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm\Behavior; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ipl\Orm\Contract\PropertyBehavior; |
| 7 |
use ipl\Orm\Exception\ValueConversionException; |
| 8 |
|
| 9 |
use function ipl\Stdlib\get_php_type; |
| 10 |
|
| 11 |
/** |
| 12 |
* Convert specific database values from and to boolean |
| 13 |
* |
| 14 |
* To unify the support of boolean values in different database systems, |
| 15 |
* specific database values are converted to and from boolean values, |
| 16 |
* e.g. by default `n` is converted to `false` and `y` to `true` and vice versa respectively, |
| 17 |
* which could be stored as `ENUM('n', 'y')`. |
| 18 |
*/ |
| 19 |
class BoolCast extends PropertyBehavior |
| 20 |
{ |
| 21 |
/** @var mixed Database value for boolean `false` */ |
| 22 |
protected mixed $falseValue = 'n'; |
| 23 |
|
| 24 |
/** @var mixed Database value for boolean `true` */ |
| 25 |
protected mixed $trueValue = 'y'; |
| 26 |
|
| 27 |
/** @var bool Whether to throw an exception if the value is not equal to the value for false or true */ |
| 28 |
protected bool $strict = true; |
| 29 |
|
| 30 |
/** |
| 31 |
* Get the database value representing boolean `false` |
| 32 |
* |
| 33 |
* @return mixed |
| 34 |
*/ |
| 35 |
public function getFalseValue(): mixed |
| 36 |
{ |
| 37 |
return $this->falseValue; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Set the database value representing boolean `false` |
| 42 |
* |
| 43 |
* @param mixed $falseValue |
| 44 |
* |
| 45 |
* @return $this |
| 46 |
*/ |
| 47 |
public function setFalseValue(mixed $falseValue): static |
| 48 |
{ |
| 49 |
$this->falseValue = $falseValue; |
| 50 |
|
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get the database value representing boolean `true` |
| 56 |
* |
| 57 |
* @return mixed |
| 58 |
*/ |
| 59 |
public function getTrueValue(): mixed |
| 60 |
{ |
| 61 |
return $this->trueValue; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get the database value representing boolean `true` |
| 66 |
* |
| 67 |
* @param mixed $trueValue |
| 68 |
* |
| 69 |
* @return $this |
| 70 |
*/ |
| 71 |
public function setTrueValue(mixed $trueValue): static |
| 72 |
{ |
| 73 |
$this->trueValue = $trueValue; |
| 74 |
|
| 75 |
return $this; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get whether to throw an exception if the value is not equal to the value for false or true |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function isStrict(): bool |
| 84 |
{ |
| 85 |
return $this->strict; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Set whether to throw an exception if the value is not equal to the value for false or true |
| 90 |
* |
| 91 |
* @param bool $strict |
| 92 |
* |
| 93 |
* @return $this |
| 94 |
*/ |
| 95 |
public function setStrict(bool $strict): static |
| 96 |
{ |
| 97 |
$this->strict = $strict; |
| 98 |
|
| 99 |
return $this; |
| 100 |
} |
| 101 |
|
| 102 |
public function fromDb($value, $key, $_) |
| 103 |
{ |
| 104 |
switch (true) { |
| 105 |
case $this->trueValue === $value: |
| 106 |
return true; |
| 107 |
case $this->falseValue === $value: |
| 108 |
return false; |
| 109 |
case is_bool($value): |
| 110 |
return $value; |
| 111 |
default: |
| 112 |
if ($this->isStrict() && $value !== null) { |
| 113 |
throw new InvalidArgumentException(sprintf( |
| 114 |
'Expected %s or %s, got %s instead', |
| 115 |
$this->trueValue, |
| 116 |
$this->falseValue, |
| 117 |
$value |
| 118 |
)); |
| 119 |
} |
| 120 |
|
| 121 |
return $value; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function toDb($value, $key, $_) |
| 126 |
{ |
| 127 |
if ($value === null) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
if (! is_bool($value)) { |
| 132 |
if ( |
| 133 |
$this->isStrict() |
| 134 |
&& $value !== '*' |
| 135 |
&& $value !== $this->getFalseValue() |
| 136 |
&& $value !== $this->getTrueValue() |
| 137 |
) { |
| 138 |
throw new ValueConversionException(sprintf( |
| 139 |
'Expected bool, got %s instead', |
| 140 |
get_php_type($value) |
| 141 |
)); |
| 142 |
} |
| 143 |
|
| 144 |
return $value; |
| 145 |
} |
| 146 |
|
| 147 |
return $value ? $this->trueValue : $this->falseValue; |
| 148 |
} |
| 149 |
} |