Skip to content

Commit

Permalink
Merge pull request #3 from chadicus/master
Browse files Browse the repository at this point in the history
Add TimeOfDayFilter
  • Loading branch information
chadicus authored Oct 28, 2020
2 parents f9e60f1 + 17c539e commit 7c4c0f5
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/coverage/
/vendor/
/clover.xml
phpcs.xml
phpunit.xml
composer.lock
9 changes: 6 additions & 3 deletions .travis.yml
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ The following checks that `$value` is a timezone
$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York');
```

#### TimeOfDayFilter::filter

This will filter values as a time-of-day string in the format of `HH:MM:SS`

The following checks that `$value` is a valid time-of-day string

```php
$timeOfDay = \TraderInteractive\Filter\TimeOfDayFilter::filter('12:00:59');
```

## Contact

Developers may be contacted at:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
"ext-timezonedb": "The latest version of the timezone database"
},
"require-dev": {
"php-coveralls/php-coveralls": "^1.0",
"php-coveralls/php-coveralls": "^2.4",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
"psr-4": { "TraderInteractive\\": "src/" }
"psr-4": { "TraderInteractive\\Filter\\": "src/" }
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions src/TimeOfDayFilter.php
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.
60 changes: 60 additions & 0 deletions tests/TimeOfDayFilterTest.php
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,
],
];
}
}

0 comments on commit 7c4c0f5

Please sign in to comment.