-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from chadicus/master
Add TimeOfDayFilter
- Loading branch information
Showing
11 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/coverage/ | ||
/vendor/ | ||
/clover.xml | ||
phpcs.xml | ||
phpunit.xml | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,21 @@ | ||
sudo: false | ||
language: php | ||
dist: xenial | ||
os: linux | ||
php: | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
- 7.4 | ||
- nightly | ||
env: | ||
- PREFER_LOWEST="--prefer-lowest --prefer-stable" | ||
- PREFER_LOWEST="" | ||
matrix: | ||
jobs: | ||
fast_finish: true | ||
allow_failures: | ||
- php: nightly | ||
before_script: | ||
- composer update $PREFER_LOWEST | ||
script: ./vendor/bin/phpunit --coverage-clover clover.xml | ||
after_success: ./vendor/bin/coveralls -v | ||
after_success: ./vendor/bin/php-coveralls -v |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace TraderInteractive\Filter; | ||
|
||
use TraderInteractive\Exceptions\FilterException; | ||
|
||
final class TimeOfDayFilter | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const NON_EMPTY_STRING_ERROR = 'Value must be a non-empty string'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
const INCORRECT_FORMAT_ERROR = 'Value must be in the format HH:MM:SS'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
const TIME_OF_DAY_REGEX = '/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/'; | ||
|
||
/** | ||
* Filters a given value as a valid HH:MM:SS formatted string. | ||
* | ||
* @param mixed $value the value to be filtered. | ||
* | ||
* @return string | ||
* | ||
* @throws FilterException Thrown if the value cannot be filtered. | ||
*/ | ||
public static function filter($value) : string | ||
{ | ||
if (!is_string($value) || trim($value) === '') { | ||
throw new FilterException(self::NON_EMPTY_STRING_ERROR); | ||
} | ||
|
||
if (preg_match(self::TIME_OF_DAY_REGEX, $value) === 0) { | ||
throw new FilterException(self::INCORRECT_FORMAT_ERROR); | ||
} | ||
|
||
return $value; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Filter; | ||
|
||
use TraderInteractive\Exceptions\FilterException; | ||
use TraderInteractive\Filter\TimeOfDayFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \TraderInteractive\Filter\TimeOfDayFilter | ||
*/ | ||
class TimeOfDayFilterTest extends TestCase | ||
{ | ||
/** | ||
* @param mixed $value The value to be filtered. | ||
* @param string|null $expectedExceptionMessage Optional expection message to be thrown. | ||
* | ||
* @test | ||
* @covers ::filter | ||
* @dataProvider provideFilterData | ||
*/ | ||
public function filterValue($value, string $expectedExceptionMessage = null) | ||
{ | ||
if ($expectedExceptionMessage !== null) { | ||
$this->expectException(FilterException::class); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
} | ||
|
||
$filteredValue = TimeOfDayFilter::filter($value); | ||
$this->assertSame($value, $filteredValue); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function provideFilterData() : array | ||
{ | ||
return [ | ||
[ | ||
'value' => '23:59:59', | ||
], | ||
[ | ||
'value' => '1:1:1', | ||
'message' => TimeOfDayFilter::INCORRECT_FORMAT_ERROR, | ||
], | ||
[ | ||
'value' => null, | ||
'message' => TimeOfDayFilter::NON_EMPTY_STRING_ERROR, | ||
], | ||
[ | ||
'value' => '', | ||
'message' => TimeOfDayFilter::NON_EMPTY_STRING_ERROR, | ||
], | ||
[ | ||
'value' => "\n\t \n", | ||
'message' => TimeOfDayFilter::NON_EMPTY_STRING_ERROR, | ||
], | ||
]; | ||
} | ||
} |