Skip to content

Commit

Permalink
[FRAM-126] Handle empty string on DateTimeElement and BooleanStringEl…
Browse files Browse the repository at this point in the history
…ement (#9)

* fix: Handle empty string on DateTimeElement and BooleanStringElement (#FRAM-126)

* ci: fix psalm error
  • Loading branch information
vincent4vx authored Jul 26, 2023
1 parent 18c41e9 commit a6dec95
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/Leaf/BooleanStringElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@
*/
class BooleanStringElement extends AbstractBooleanElement
{
/**
* {@inheritdoc}
*
* @return scalar|null
* @psalm-suppress ImplementedReturnTypeMismatch
*/
protected function sanitize($rawValue)
{
// Does not cast to string, to allow boolean value
return is_scalar($rawValue) ? $rawValue : null;
}

/**
* {@inheritdoc}
*
* @return bool|null
*/
protected function toPhp($httpValue): ?bool
{
if ($httpValue === null) {
if ($httpValue === null || $httpValue === '') {
return null;
}

Expand All @@ -34,7 +46,7 @@ protected function toPhp($httpValue): ?bool
}

/** @var bool|null */
return filter_var($httpValue, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
return filter_var((string) $httpValue, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Leaf/Date/DateTimeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function dateTimeClassName(): string
*/
protected function toPhp($httpValue): ?DateTimeInterface
{
if ($httpValue === null) {
if ($httpValue === null || $httpValue === '') {
return null;
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Leaf/BooleanStringElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ public function test_submit_null()
$this->assertTrue($element->error()->empty());
}

/**
*
*/
public function test_submit_empty()
{
$element = new BooleanStringElement();

$this->assertTrue($element->submit('')->valid());
$this->assertNull($element->value());
$this->assertTrue($element->error()->empty());

$this->assertTrue($element->submit([])->valid());
$this->assertNull($element->value());
$this->assertTrue($element->error()->empty());
}

/**
*
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Leaf/Date/DateTimeElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ public function test_submit_null()
$this->assertTrue($element->error()->empty());
}

/**
*
*/
public function test_submit_empty()
{
$element = new DateTimeElement();

$this->assertTrue($element->submit('')->valid());
$this->assertNull($element->value());
$this->assertTrue($element->error()->empty());

$this->assertTrue($element->submit([])->valid());
$this->assertNull($element->value());
$this->assertTrue($element->error()->empty());
}

/**
*
*/
Expand Down

0 comments on commit a6dec95

Please sign in to comment.