Skip to content

Commit

Permalink
Correction to the treatment of 0 in FLOAT type.
Browse files Browse the repository at this point in the history
  • Loading branch information
martywallace committed Jul 13, 2017
1 parent 58eb3a5 commit 83a2dcb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "martywallace/simpledb",
"version": "2.2.0",
"version": "2.2.1",
"description": "A thin wrapper around PDO for common MySQL operations.",
"homepage": "https://github.com/MartyWallace/simpledb",
"type": "library",
Expand Down
2 changes: 1 addition & 1 deletion src/SimpleDb/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Field {
public static function isNull($value, $type) {
if ($type === self::INT || $type === self::FLOAT) {
// Don't treat '0' and 0 as empty values, those are valid ints of value 0.
return empty($value) && $value !== 0 && $value !== '0';
return $value === null || $value === false || $value === '' || $value === [];
} else if ($type === self::STRING) {
// Don't consider empty strings, "0" or 0 as empty, we still want to store those.
return $value === null || $value === false || $value === [];
Expand Down
24 changes: 24 additions & 0 deletions tests/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ public function testIntToRefined() {
]);
}

public function testFloatToRefined() {
$this->assertSame([
Field::toRefined(0, Field::FLOAT),
Field::toRefined('0', Field::FLOAT),
Field::toRefined('0.0', Field::FLOAT),
Field::toRefined(0.000000, Field::FLOAT),
Field::toRefined('', Field::FLOAT),
Field::toRefined(false, Field::FLOAT),
Field::toRefined(null, Field::FLOAT),
Field::toRefined(123.5, Field::FLOAT),
Field::toRefined('0832', Field::FLOAT)
], [
0,
0.0,
0.0,
0.0,
null,
null,
null,
123.5,
832.0
]);
}

public function testStringToPrimitive() {
$this->assertSame([
Field::toPrimitive('', Field::STRING),
Expand Down

0 comments on commit 83a2dcb

Please sign in to comment.