b2pweb /
bdf-form
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\Form\Leaf; |
||
| 4 | |||
| 5 | use BadMethodCallException; |
||
| 6 | use Bdf\Form\Child\Http\HttpFieldPath; |
||
| 7 | use Bdf\Form\Leaf\View\BooleanElementView; |
||
| 8 | use Bdf\Form\Transformer\TransformerInterface; |
||
| 9 | use Bdf\Form\Validator\ValueValidatorInterface; |
||
| 10 | use Bdf\Form\View\ElementViewInterface; |
||
| 11 | use Bdf\Form\View\FieldViewInterface; |
||
| 12 | use LogicException; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Handle a boolean value, like with checkbox input |
||
| 16 | * A value is considered as true when a value is present, and equals to the defined value |
||
| 17 | * |
||
| 18 | * @see BooleanElementBuilder for build the element |
||
| 19 | * |
||
| 20 | * @method bool value() |
||
| 21 | * @extends LeafElement<bool> |
||
| 22 | */ |
||
| 23 | class BooleanElement extends LeafElement |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $httpValue = '1'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * BooleanElement constructor. |
||
| 32 | * |
||
| 33 | * @param ValueValidatorInterface|null $validator |
||
| 34 | * @param TransformerInterface|null $transformer |
||
| 35 | * @param string $httpValue Value to use as "true" value for HTTP value |
||
| 36 | */ |
||
| 37 | 36 | public function __construct(?ValueValidatorInterface $validator = null, ?TransformerInterface $transformer = null, string $httpValue = '1') |
|
| 38 | { |
||
| 39 | 36 | parent::__construct($validator, $transformer); |
|
| 40 | |||
| 41 | 36 | $this->httpValue = $httpValue; |
|
| 42 | 36 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | 11 | protected function toPhp($httpValue) |
|
| 48 | { |
||
| 49 | 11 | return (bool) $httpValue; |
|
|
0 ignored issues
–
show
|
|||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritdoc} |
||
| 54 | */ |
||
| 55 | 6 | protected function toHttp($phpValue) |
|
| 56 | { |
||
| 57 | 6 | return $phpValue ? $this->httpValue : null; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | * |
||
| 63 | * @return bool|null |
||
| 64 | */ |
||
| 65 | 15 | protected function tryCast($value): ?bool |
|
| 66 | { |
||
| 67 | 15 | if ($value === null) { |
|
| 68 | 1 | return null; |
|
| 69 | } |
||
| 70 | |||
| 71 | 14 | if (!is_scalar($value)) { |
|
| 72 | 3 | throw new \TypeError('The import()\'ed value of a '.static::class.' must be a scalar value or null'); |
|
| 73 | } |
||
| 74 | |||
| 75 | 11 | return (bool) $value; |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | * |
||
| 81 | * @return FieldViewInterface |
||
| 82 | */ |
||
| 83 | 2 | public function view(?HttpFieldPath $field = null): ElementViewInterface |
|
| 84 | { |
||
| 85 | 2 | return new BooleanElementView(self::class, (string) $field, $this->httpValue(), $this->httpValue, (bool) $this->value(), $this->error()->global()); |
|
| 86 | } |
||
| 87 | } |
||
| 88 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: