Skip to content

Commit

Permalink
Fix '0' being seen as empty value bug in AbstractEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Jul 27, 2020
1 parent 79a2448 commit f8b03db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Parable PHP ORM

## 0.9.1

- When using typed properties and trying to set a value of '0', untyping it caused it to become a string value. This would end up being seen as an 'empty' value and removed from the value set.

## 0.9.0

- `Transaction` has been added. Now you can start, commit or roll back a transaction very easily. Simply call `Transaction::begin()`, `Transaction::commit()` or `Transaction::rollback()`.
Expand Down
3 changes: 2 additions & 1 deletion src/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public function toArrayWithoutEmptyValues(): array
$array = $this->toArray();

foreach ($array as $key => $value) {
if (empty($value) && $value !== 0) {
// We don't want values 0 or '0' to be seen as empty, so we ctype_digit it
if (empty($value) && !ctype_digit($value)) {
unset($array[$key]);
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ public function testToArrayWithout(): void
);
}

public function testToArrayWithoutSees0AsNonEmpty(): void
{
$entity = new TestEntity();

$entity->setName('0');

self::assertSame(
[
'name' => '0',
],
$entity->toArrayWithoutEmptyValues()
);
}

public function testCreateValidEntityAndToArrayWithoutEmptyValues(): void
{
$entity = new TestEntity();
Expand Down

0 comments on commit f8b03db

Please sign in to comment.