Skip to content

Commit

Permalink
Implement null-if-empty check (#74)
Browse files Browse the repository at this point in the history
* Update tests for #73

* Implement null-if-empty check, closes #73
  • Loading branch information
g105b authored Nov 26, 2019
1 parent 7897c0d commit d85b65d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
30 changes: 24 additions & 6 deletions src/InputValueGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getString(string $key):?string {

public function getInt(string $key):?int {
$value = $this->getString($key);
if(is_null($value)) {
if(is_null($value) || strlen($value) === 0) {
return null;
}

Expand All @@ -26,7 +26,7 @@ public function getInt(string $key):?int {

public function getFloat(string $key):?float {
$value = $this->getString($key);
if(is_null($value)) {
if(is_null($value) || strlen($value) === 0) {
return null;
}

Expand All @@ -35,7 +35,7 @@ public function getFloat(string $key):?float {

public function getBool(string $key):?bool {
$value = $this->getString($key);
if(is_null($value)) {
if(is_null($value) || strlen($value) === 0) {
return null;
}

Expand Down Expand Up @@ -68,14 +68,32 @@ public function getMultipleFile(string $key):MultipleInputDatum {
return $this->get($key);
}

public function getDateTime(string $key):DateTimeInterface {
public function getDateTime(
string $key,
string $format = null
):?DateTimeInterface {
$value = $this->getString($key);
if(is_null($value) || strlen($value) === 0) {
return null;
}

try {
$dateTime = new DateTime($this[$key]);
return $dateTime;
if($format) {
$dateTime = DateTime::createFromFormat($format, $value);
}
else {
$dateTime = new DateTime($value);
}
}
catch(Exception $exception) {
$dateTime = false;
}

if($dateTime === false) {
throw new DataNotCompatibleFormatException($key);
}

return $dateTime;
}

/**
Expand Down
35 changes: 32 additions & 3 deletions test/unit/InputTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Gt\Input\Test;

use DateTime;
use Gt\Input\DataNotCompatibleFormatException;
use Gt\Input\Input;
use Gt\Input\InputData\Datum\FileUpload;
use Gt\Input\InputData\InputData;
Expand All @@ -17,6 +19,10 @@ class InputTest extends TestCase {
"half-of-five" => "2.5",
"many-dots" => "1.2.3.4",
"this-is-positive" => "oh yes",
"a-date-without-timezone" => "2005-08-15T15:52:00",
"a-date-with-timezone" => "2005-08-15T16:52:00+01:00",
"a-date-without-timezone-or-seconds" => "2005-08-15T15:52",
"a-date-in-rss-format" => "Mon, 15 Aug 2005 15:52:00 +0000",
"empty-value" => "",
];
const FAKE_FILE = [
Expand Down Expand Up @@ -471,7 +477,7 @@ public function testGetInt():void {
self::assertSame(2, $input->getInt("one-plus-one"));
self::assertSame(1, $input->getInt("many-dots"));
self::assertSame(0, $input->getInt("this-is-positive"));
self::assertSame(0, $input->getInt("empty-value"));
self::assertNull($input->getInt("empty-value"));
self::assertNull($input->getInt("not-set"));
}

Expand All @@ -482,7 +488,7 @@ public function testGetFloat():void {
self::assertSame(2.5, $input->getFloat("half-of-five"));
self::assertSame(1.2, $input->getFloat("many-dots"));
self::assertSame(0.0, $input->getFloat("this-is-positive"));
self::assertSame(0.0, $input->getFloat("empty-value"));
self::assertNull($input->getFloat("empty-value"));
self::assertNull($input->getFloat("not-set"));
}

Expand All @@ -493,10 +499,33 @@ public function testGetBool():void {
self::assertSame(true, $input->getBool("half-of-five"));
self::assertSame(true, $input->getBool("many-dots"));
self::assertSame(true, $input->getBool("this-is-positive"));
self::assertSame(false, $input->getBool("empty-value"));
self::assertNull($input->getBool("empty-value"));
self::assertNull($input->getBool("not-set"));
}

public function testGetDateTime():void {
$dateTime = new DateTime("2005-08-15T15:52");

$input = new Input(self::FAKE_DATA);
self::assertNull($input->getDateTime("empty-value"));
self::assertEquals($dateTime, $input->getDateTime("a-date-with-timezone"));
self::assertEquals($dateTime, $input->getDateTime("a-date-without-timezone"));
self::assertEquals($dateTime, $input->getDateTime("a-date-without-timezone-or-seconds"));
}

public function testGetDateTimeInvalid():void {
$input = new Input(self::FAKE_DATA);
self::expectException(DataNotCompatibleFormatException::class);
$input->getDateTime("one-plus-one");
}

public function testGetDateTimeFromFormat():void {
$dateTime = new DateTime("2005-08-15T15:52");

$input = new Input(self::FAKE_DATA);
self::assertEquals($dateTime, $input->getDateTime("a-date-in-rss-format"));
}

/** @dataProvider dataRandomGetPost */
public function testContains($get, $post):void {
$files = self::FAKE_FILE;
Expand Down

0 comments on commit d85b65d

Please sign in to comment.