From 641a22597f3f2c31ea65c383f306a94e1c130105 Mon Sep 17 00:00:00 2001 From: James Galecki Date: Mon, 5 Mar 2018 15:57:56 -0500 Subject: [PATCH 1/9] init commit --- .coveralls.yml | 3 + .gitattributes | 13 +++ .github/CODEOWNERS | 1 + .github/CONTRIBUTING.md | 24 +++++ .github/ISSUE_TEMPLATE.md | 6 ++ .github/PULL_REQUEST_TEMPLATE.md | 9 ++ .gitignore | 5 + .scrutinizer.yml | 22 +++++ .travis.yml | 18 ++++ LICENSE | 20 ++++ README.md | 90 +++++++++++++++++- composer.json | 31 ++++++ phpcs.xml | 8 ++ phpunit.xml.dist | 10 ++ src/Filter/DateTime.php | 67 +++++++++++++ src/Filter/DateTimeZone.php | 43 +++++++++ tests/Filter/DateTimeTest.php | 150 ++++++++++++++++++++++++++++++ tests/Filter/DateTimeZoneTest.php | 114 +++++++++++++++++++++++ 18 files changed, 632 insertions(+), 2 deletions(-) create mode 100644 .coveralls.yml create mode 100644 .gitattributes create mode 100644 .github/CODEOWNERS create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE.md create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .gitignore create mode 100644 .scrutinizer.yml create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 composer.json create mode 100644 phpcs.xml create mode 100644 phpunit.xml.dist create mode 100644 src/Filter/DateTime.php create mode 100644 src/Filter/DateTimeZone.php create mode 100644 tests/Filter/DateTimeTest.php create mode 100644 tests/Filter/DateTimeZoneTest.php diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..4eecff5 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1,3 @@ +service_name: travis-ci +coverage_clover: clover.xml +json_path: coveralls-upload.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..932c4ba --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ +# Enforce Unix newlines +* text=lf + +# Exclude unused files +# see: https://redd.it/2jzp6k +/tests export-ignore +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.*.yml export-ignore +/phpcs.xml export-ignore +/phpunit.xml.dist export-ignore +/README.md export-ignore diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..9a4b075 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @traderinteractive/opensource diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..9217f75 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,24 @@ +# Contribution Guidelines +We welcome you to report [issues](/../../issues) or submit [pull requests](/../../pulls). While the below guidelines are necessary to get code merged, you can +submit pull requests that do not adhere to them and we will try to take care of them in our spare time. We are a smallish group of developers, +though, so if you can make sure the build is passing 100%, that would be very useful. + +We recommend including details of your particular usecase(s) with any issues or pull requests. We love to hear how our libraries are being used +and we can get things merged in quicker when we understand its expected usage. + +## Pull Requests +Code changes should be sent through [GitHub Pull Requests](/../../pulls). Before submitting the pull request, make sure that phpunit reports success +by running: +```sh +./vendor/bin/phpunit +``` +And there are not coding standard violations by running +```sh +./vendor/bin/phpcs +``` + +## Builds +Our [Travis build](https://travis-ci.org/traderinteractive/filter-dates-php) executes [PHPUnit](http://www.phpunit.de) and uses [Coveralls](https://coveralls.io/) to enforce code coverage. +While the build does not strictly enforce 100% code coverage, it will not allow coverage to drop below its current percentage. +[Scrutinizer](https://scrutinizer-ci.com/) is used to ensure code quality and enforce the [coding standard](http://www.php-fig.org/psr/psr-2/). + diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..4eef22e --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,6 @@ +## Expected Behavior + +## Actual Behavior + +## Steps to reproduce the behavior + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..c0d57b0 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +Fixes # . + +#### What does this PR do? + +#### Checklist +- [ ] Pull request contains a clear definition of changes +- [ ] Tests (either unit, integration, or acceptance) written and passing +- [ ] Relevant documentation produced and/or updated + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3be34b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/coverage/ +/vendor/ +/clover.xml +phpunit.xml +composer.lock diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..73720bb --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,22 @@ +filter: + excluded_paths: + - 'vendor/*' +before_commands: + - 'composer install' +tools: + php_analyzer: true + php_mess_detector: true + php_code_sniffer: + config: + standard: PSR2 + sensiolabs_security_checker: true + php_loc: + excluded_dirs: + - vendor + php_pdepend: true + php_sim: true +build_failure_conditions: + - 'elements.rating(<= D).new.exists' # No new classes/methods with a rating of D or worse allowed (useful for legacy code) + - 'issues.label("coding-style").new.exists' # No new coding style issues allowed + - 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity + - 'project.metric("scrutinizer.quality", < 6)' # Code Quality Rating drops below 6 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..36bdffb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +sudo: false +language: php +php: + - 7.0 + - 7.1 + - 7.2 + - nightly +env: + - PREFER_LOWEST="--prefer-lowest --prefer-stable" + - PREFER_LOWEST="" +matrix: + 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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a7a5819 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2017 Trader Interactive + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index d7a9b4b..2fee10b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,88 @@ -# filter-dates-php -A filtering implementation for verifying correct data and performing typical modifications to dates and times. +# filter-strings-php + +A filtering implementation for verifying correct data and performing typical modifications to data. + +[![Build Status](https://travis-ci.org/traderinteractive/filter-strings-php.svg?branch=master)](https://travis-ci.org/traderinteractive/filter-strings-php) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/filter-strings-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/filter-strings-php/?branch=master) +[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/filter-strings-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/filter-strings-php?branch=master) + +[![Latest Stable Version](https://poser.pugx.org/traderinteractive/filter-strings/v/stable)](https://packagist.org/packages/traderinteractive/filter-strings) +[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/filter-strings/v/unstable)](https://packagist.org/packages/traderinteractive/filter-strings) +[![License](https://poser.pugx.org/traderinteractive/filter-strings/license)](https://packagist.org/packages/traderinteractive/filter-strings) + +[![Total Downloads](https://poser.pugx.org/traderinteractive/filter-strings/downloads)](https://packagist.org/packages/traderinteractive/filter-strings) +[![Daily Downloads](https://poser.pugx.org/traderinteractive/filter-strings/d/daily)](https://packagist.org/packages/traderinteractive/filter-strings) +[![Monthly Downloads](https://poser.pugx.org/traderinteractive/filter-strings/d/monthly)](https://packagist.org/packages/traderinteractive/filter-strings) + +## Features + +TO DO + +## Example + +TO DO + +## Composer + +To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on +`traderinteractive/filter-strings` to your project's `composer.json` file such as: + +```sh +composer require traderinteractive/filter-strings +``` + +## Documentation + +TO DO + +#### Specification + +TODO + +#### DateTime::filter + +Aliased in the filterer as `date`, this will filter the value as a `\DateTime` object. The value can be any string that conforms to [PHP's valid date/time formats](http://php.net/manual/en/datetime.formats.php) + +The following checks that `$value` is a date/time. + +```php +$dateTime = \TraderInteractive\Filter\DateTime::filter('2014-02-04T11:55:00-0500'); +``` + +#### DateTime::format + +Aliased in the filterer as `date-format`, this will filter a given `\DateTime' value to a string based on the given format. + +The following returns formatted string for a given `\DateTime` `$value` + +```php +$formatted = \TraderInteractive\Filter\DateTime::format($value, 'Y-m-d H:i:s'); +``` + +#### DateTimeZone::filter + +Aliased in the filterer as `date`, this will filter the value as a `\DateTimeZone` object. The value can be any [supported timezone name](http://php.net/manual/en/timezones.php) + +The following checks that `$value` is a timezone + +```php +$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York'); +``` + +## Contact + +Developers may be contacted at: + + * [Pull Requests](https://github.com/traderinteractive/filter-strings-php/pulls) + * [Issues](https://github.com/traderinteractive/filter-strings-php/issues) + +## Project Build + +With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run: + +```bash +./vendor/bin/phpcs +./vendor/bin/phpunit +``` + +For more information on our build process, read through out our [Contribution Guidelines](CONTRIBUTING.md). diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..482e83d --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "traderinteractive/filter-dates", + "description": "A filtering implementation for verifying dates", + "keywords": [ + "utility", + "dates", + "date", + "datetime", + "datetimezone", + "timezone" + ], + "config": { + "sort-packages": true + }, + "license": "MIT", + "require": { + "php": "^7.0", + "traderinteractive/exceptions": "^1.0" + }, + "suggest": { + "ext-timezonedb": "The latest version of the timezone database" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^1.0", + "phpunit/phpunit": "^6.0", + "squizlabs/php_codesniffer": "^3.2" + }, + "autoload": { + "psr-4": { "TraderInteractive\\": "src/" } + } +} diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..449f1e8 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,8 @@ + + + . + */vendor/* + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..ba76cec --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,10 @@ + + + ./tests + + + + src + + + diff --git a/src/Filter/DateTime.php b/src/Filter/DateTime.php new file mode 100644 index 0000000..30c48eb --- /dev/null +++ b/src/Filter/DateTime.php @@ -0,0 +1,67 @@ +format($format); + } +} diff --git a/src/Filter/DateTimeZone.php b/src/Filter/DateTimeZone.php new file mode 100644 index 0000000..5e80658 --- /dev/null +++ b/src/Filter/DateTimeZone.php @@ -0,0 +1,43 @@ +getMessage(), $e->getCode(), $e); + } + } +} diff --git a/tests/Filter/DateTimeTest.php b/tests/Filter/DateTimeTest.php new file mode 100644 index 0000000..b4877c9 --- /dev/null +++ b/tests/Filter/DateTimeTest.php @@ -0,0 +1,150 @@ +assertSame(strtotime($string), $dateTime->getTimestamp()); + } + + /** + * Verify behavior of filter() when $value is an integer. + * + * @test + * @covers ::filter + * + * @return void + */ + public function filterTimestamp() + { + $now = time(); + $dateTime = DateTime::filter("@{$now}"); + + $this->assertSame($now, $dateTime->getTimestamp()); + } + + /** + * Verify behavior of filter() when $value is a string with only whitespace. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value is not a non-empty string + * + * @return void + */ + public function filterEmptyValue() + { + DateTime::filter("\t \n"); + } + + /** + * Verify behavior of filter() when $value is not a string or integer. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value is not a non-empty string + * + * @return void + */ + public function filterInvalidValue() + { + DateTime::filter(true); + } + + /** + * Verify behavior of filter() when null is given for $value and $allowNull is true. + * + * @test + * @covers ::filter + */ + public function filterNullAllowed() + { + $this->assertNull(DateTime::filter(null, true)); + } + + /** + * Verify behavior of filter() when null is given for $value and $allowNull is true. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value is not a non-empty string + */ + public function filterNullNotAllowed() + { + DateTime::filter(null, false); + } + + /** + * Verify behavior of filter() when $value is a \DateTime object. + * + * @test + * @covers ::filter + */ + public function filterDateTimePass() + { + $dateTime = new \DateTime('now'); + $this->assertSame($dateTime, DateTime::filter($dateTime)); + } + + /** + * Verify behavior of filter() when $timezone is given. + * + * @test + * @covers ::filter + */ + public function filterWithTimeZone() + { + $timezone = new \DateTimeZone('Pacific/Honolulu'); + $dateTime = DateTime::filter('now', false, $timezone); + $this->assertSame($timezone->getName(), $dateTime->getTimeZone()->getName()); + $this->assertSame(-36000, $dateTime->getOffset()); + } + + /** + * Verify behavior of filter() when $value is an integer. + * + * @test + * @covers ::filter + */ + public function filterWithIntegerValue() + { + $now = time(); + $dateTime = DateTime::filter($now); + $this->assertSame($now, $dateTime->getTimestamp()); + } + + /** + * Verify basic behavior of format(). + * + * @test + * @covers ::format + */ + public function format() + { + $now = new \DateTime(); + $this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, 'Y-m-d H:i:s')); + } +} diff --git a/tests/Filter/DateTimeZoneTest.php b/tests/Filter/DateTimeZoneTest.php new file mode 100644 index 0000000..489b16a --- /dev/null +++ b/tests/Filter/DateTimeZoneTest.php @@ -0,0 +1,114 @@ +assertSame($value, $timezone->getName()); + $this->assertSame(-36000, $timezone->getOffset(new \DateTime('now', $timezone))); + } + + /** + * Verify behavior of filter() when $allowNull is true and $value is null. + * + * @test + * @covers ::filter + * + * @return void + */ + public function filterNullAllowed() + { + $this->assertNull(DateTimeZone::filter(null, true)); + } + + /** + * Verify behavior of filter() when $allowNull is false and $value is null. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value not a non-empty string + * + * @return void + */ + public function filterNullNotAllowed() + { + $this->assertNull(DateTimeZone::filter(null, false)); + } + + /** + * Verify behavior of filter() when $value is a \DateTimeZone object. + * + * @test + * @covers ::filter + * + * @return void + */ + public function filterTimeZonePass() + { + $timezone = new \DateTimeZone('America/New_York'); + $this->assertSame($timezone, DateTimeZone::filter($timezone)); + } + + /** + * Verify behavior of filter() when $value is not a valid timezone. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage Unknown or bad timezone (INVALID) + */ + public function filterInvalidName() + { + DateTimeZone::filter('INVALID'); + } + + /** + * Verify behavior of filter() $value is a string with only whitespace. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value not a non-empty string + * + * @return void + */ + public function filterEmptyValue() + { + DateTimeZone::filter("\n\t"); + } + + /** + * Verify behavior of filter() when $value is not a string. + * + * @test + * @covers ::filter + * @expectedException \TraderInteractive\Exceptions\FilterException + * @expectedExceptionMessage $value not a non-empty string + * + * @return void + */ + public function filterNonStringArgument() + { + DateTimeZone::filter(42); + } +} From 9440feeb54eefd8e6d7945977cf4d5a596f265ba Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 12:02:03 -0500 Subject: [PATCH 2/9] scruets --- .scrutinizer.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 73720bb..7ebcd80 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -16,7 +16,7 @@ tools: php_pdepend: true php_sim: true build_failure_conditions: - - 'elements.rating(<= D).new.exists' # No new classes/methods with a rating of D or worse allowed (useful for legacy code) - - 'issues.label("coding-style").new.exists' # No new coding style issues allowed - - 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity - - 'project.metric("scrutinizer.quality", < 6)' # Code Quality Rating drops below 6 + - 'elements.rating(< A).new.exists' + - 'issues.label("coding-style").new.exists' + - 'issues.severity(>= MAJOR).new.exists' + - 'project.metric("scrutinizer.quality", < 9)' From 0641caa263845c186bc2dd917073c70139589715 Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 12:16:25 -0500 Subject: [PATCH 3/9] scrutiny --- .scrutinizer.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 7ebcd80..ef22543 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -16,7 +16,7 @@ tools: php_pdepend: true php_sim: true build_failure_conditions: - - 'elements.rating(< A).new.exists' - - 'issues.label("coding-style").new.exists' - - 'issues.severity(>= MAJOR).new.exists' + - 'elements.rating(< A).exists' + - 'issues.label("coding-style").exists' + - 'issues.severity(>= MAJOR).exists' - 'project.metric("scrutinizer.quality", < 9)' From d2e6bb296073d3849bf0a0369469ff81822cdb0f Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 15:51:08 -0500 Subject: [PATCH 4/9] datetime fix --- src/Filter/DateTime.php | 10 +++++++++- tests/Filter/DateTimeTest.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Filter/DateTime.php b/src/Filter/DateTime.php index 30c48eb..739278a 100644 --- a/src/Filter/DateTime.php +++ b/src/Filter/DateTime.php @@ -27,7 +27,7 @@ class DateTime */ public static function filter($value, bool $allowNull = false, DateTimeZoneStandard $timezone = null) { - if ($value === null && $allowNull) { + if (self::valueIsNullAndValid($allowNull, $value)) { return null; } @@ -64,4 +64,12 @@ public static function format(DateTimeInterface $dateTime, string $format = 'c') return $dateTime->format($format); } + + private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool + { + if ($allowNull === false && $value === null) { + throw new FilterException('Value failed filtering, $allowNull is set to false'); + } + return $allowNull === true && $value === null; + } } diff --git a/tests/Filter/DateTimeTest.php b/tests/Filter/DateTimeTest.php index b4877c9..5eff1f6 100644 --- a/tests/Filter/DateTimeTest.php +++ b/tests/Filter/DateTimeTest.php @@ -90,7 +90,7 @@ public function filterNullAllowed() * @test * @covers ::filter * @expectedException \TraderInteractive\Exceptions\FilterException - * @expectedExceptionMessage $value is not a non-empty string + * @expectedExceptionMessage Value failed filtering, $allowNull is set to false */ public function filterNullNotAllowed() { From 0807cdb828f7ec43453dd745cb73fc8683119f01 Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 15:52:54 -0500 Subject: [PATCH 5/9] fix date timezone --- src/Filter/DateTimeZone.php | 10 +++++++++- tests/Filter/DateTimeZoneTest.php | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Filter/DateTimeZone.php b/src/Filter/DateTimeZone.php index 5e80658..d144d01 100644 --- a/src/Filter/DateTimeZone.php +++ b/src/Filter/DateTimeZone.php @@ -22,7 +22,7 @@ class DateTimeZone */ public static function filter($value, bool $allowNull = false) { - if ($value === null && $allowNull) { + if (self::valueIsNullAndValid($allowNull, $value)) { return null; } @@ -40,4 +40,12 @@ public static function filter($value, bool $allowNull = false) throw new FilterException($e->getMessage(), $e->getCode(), $e); } } + + private static function valueIsNullAndValid(bool $allowNull, $value = null) : bool + { + if ($allowNull === false && $value === null) { + throw new FilterException('Value failed filtering, $allowNull is set to false'); + } + return $allowNull === true && $value === null; + } } diff --git a/tests/Filter/DateTimeZoneTest.php b/tests/Filter/DateTimeZoneTest.php index 489b16a..d82e49f 100644 --- a/tests/Filter/DateTimeZoneTest.php +++ b/tests/Filter/DateTimeZoneTest.php @@ -46,7 +46,7 @@ public function filterNullAllowed() * @test * @covers ::filter * @expectedException \TraderInteractive\Exceptions\FilterException - * @expectedExceptionMessage $value not a non-empty string + * @expectedExceptionMessage Value failed filtering, $allowNull is set to false * * @return void */ From ce9e73e4bb231d3949929889ce33df9a3695ae0f Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 15:55:43 -0500 Subject: [PATCH 6/9] lower scrutinizer regulation --- .scrutinizer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index ef22543..8a1675c 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -16,7 +16,7 @@ tools: php_pdepend: true php_sim: true build_failure_conditions: - - 'elements.rating(< A).exists' + - 'elements.rating(< B).exists' - 'issues.label("coding-style").exists' - 'issues.severity(>= MAJOR).exists' - 'project.metric("scrutinizer.quality", < 9)' From 9cf5fbd3259b8562a06db1411c2b4ef9c102e1ff Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 16:00:49 -0500 Subject: [PATCH 7/9] update readme --- README.md | 53 ++++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 2fee10b..de2d3d8 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,37 @@ -# filter-strings-php +# filter-dates-php -A filtering implementation for verifying correct data and performing typical modifications to data. +[![Build Status](https://travis-ci.org/traderinteractive/filter-dates-php.svg?branch=master)](https://travis-ci.org/traderinteractive/filter-dates-php) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/filter-dates-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/filter-dates-php/?branch=master) +[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/filter-dates-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/filter-dates-php?branch=master) -[![Build Status](https://travis-ci.org/traderinteractive/filter-strings-php.svg?branch=master)](https://travis-ci.org/traderinteractive/filter-strings-php) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/filter-strings-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/filter-strings-php/?branch=master) -[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/filter-strings-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/filter-strings-php?branch=master) +[![Latest Stable Version](https://poser.pugx.org/traderinteractive/filter-dates/v/stable)](https://packagist.org/packages/traderinteractive/filter-dates) +[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/filter-dates/v/unstable)](https://packagist.org/packages/traderinteractive/filter-dates) +[![License](https://poser.pugx.org/traderinteractive/filter-dates/license)](https://packagist.org/packages/traderinteractive/filter-dates) -[![Latest Stable Version](https://poser.pugx.org/traderinteractive/filter-strings/v/stable)](https://packagist.org/packages/traderinteractive/filter-strings) -[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/filter-strings/v/unstable)](https://packagist.org/packages/traderinteractive/filter-strings) -[![License](https://poser.pugx.org/traderinteractive/filter-strings/license)](https://packagist.org/packages/traderinteractive/filter-strings) +[![Total Downloads](https://poser.pugx.org/traderinteractive/filter-dates/downloads)](https://packagist.org/packages/traderinteractive/filter-dates) +[![Daily Downloads](https://poser.pugx.org/traderinteractive/filter-dates/d/daily)](https://packagist.org/packages/traderinteractive/filter-dates) +[![Monthly Downloads](https://poser.pugx.org/traderinteractive/filter-dates/d/monthly)](https://packagist.org/packages/traderinteractive/filter-dates) -[![Total Downloads](https://poser.pugx.org/traderinteractive/filter-strings/downloads)](https://packagist.org/packages/traderinteractive/filter-strings) -[![Daily Downloads](https://poser.pugx.org/traderinteractive/filter-strings/d/daily)](https://packagist.org/packages/traderinteractive/filter-strings) -[![Monthly Downloads](https://poser.pugx.org/traderinteractive/filter-strings/d/monthly)](https://packagist.org/packages/traderinteractive/filter-strings) +A filtering implementation for verifying correct data and performing typical modifications to common date objects. -## Features +## Requirements -TO DO - -## Example - -TO DO +Requires PHP 7.0 or newer and uses composer to install further PHP dependencies. See the [composer specification](composer.json) for more details. ## Composer To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on -`traderinteractive/filter-strings` to your project's `composer.json` file such as: +`traderinteractive/filter-dates` to your project's `composer.json` file such as: ```sh -composer require traderinteractive/filter-strings +composer require traderinteractive/filter-dates ``` -## Documentation - -TO DO - -#### Specification - -TODO +### Functionality #### DateTime::filter -Aliased in the filterer as `date`, this will filter the value as a `\DateTime` object. The value can be any string that conforms to [PHP's valid date/time formats](http://php.net/manual/en/datetime.formats.php) +This will filter the value as a `\DateTime` object. The value can be any string that conforms to [PHP's valid date/time formats](http://php.net/manual/en/datetime.formats.php) The following checks that `$value` is a date/time. @@ -51,7 +41,7 @@ $dateTime = \TraderInteractive\Filter\DateTime::filter('2014-02-04T11:55:00-0500 #### DateTime::format -Aliased in the filterer as `date-format`, this will filter a given `\DateTime' value to a string based on the given format. +This will filter a given `\DateTime' value to a string based on the given format. The following returns formatted string for a given `\DateTime` `$value` @@ -61,7 +51,7 @@ $formatted = \TraderInteractive\Filter\DateTime::format($value, 'Y-m-d H:i:s'); #### DateTimeZone::filter -Aliased in the filterer as `date`, this will filter the value as a `\DateTimeZone` object. The value can be any [supported timezone name](http://php.net/manual/en/timezones.php) +This will filter the value as a `\DateTimeZone` object. The value can be any [supported timezone name](http://php.net/manual/en/timezones.php) The following checks that `$value` is a timezone @@ -73,14 +63,15 @@ $timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York'); Developers may be contacted at: - * [Pull Requests](https://github.com/traderinteractive/filter-strings-php/pulls) - * [Issues](https://github.com/traderinteractive/filter-strings-php/issues) + * [Pull Requests](https://github.com/traderinteractive/filter-dates-php/pulls) + * [Issues](https://github.com/traderinteractive/filter-dates-php/issues) ## Project Build With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run: ```bash +composer install ./vendor/bin/phpcs ./vendor/bin/phpunit ``` From a244dad056a0251d417c07f42e68d485c495ff4b Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 16:02:10 -0500 Subject: [PATCH 8/9] fix coverage --- tests/Filter/DateTimeTest.php | 1 + tests/Filter/DateTimeZoneTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Filter/DateTimeTest.php b/tests/Filter/DateTimeTest.php index 5eff1f6..40d7dd9 100644 --- a/tests/Filter/DateTimeTest.php +++ b/tests/Filter/DateTimeTest.php @@ -8,6 +8,7 @@ * Unit tests for the \TraderInteractive\Filter\DateTime class. * * @coversDefaultClass \TraderInteractive\Filter\DateTime + * @covers :: */ final class DateTimeTest extends TestCase { diff --git a/tests/Filter/DateTimeZoneTest.php b/tests/Filter/DateTimeZoneTest.php index d82e49f..06a814d 100644 --- a/tests/Filter/DateTimeZoneTest.php +++ b/tests/Filter/DateTimeZoneTest.php @@ -8,6 +8,7 @@ * Unit tests for the \TraderInteractive\Filter\DateTimeZone class. * * @coversDefaultClass \TraderInteractive\Filter\DateTimeZone + * @covers :: */ final class DateTimeZoneTest extends TestCase { From d224e595ffb4db0f800626f66d82e574013b1bc8 Mon Sep 17 00:00:00 2001 From: James Galecki Date: Tue, 6 Mar 2018 16:15:37 -0500 Subject: [PATCH 9/9] pr suggestions --- src/Filter/DateTime.php | 6 +++--- src/Filter/DateTimeZone.php | 1 - tests/Filter/DateTimeTest.php | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Filter/DateTime.php b/src/Filter/DateTime.php index 739278a..ebac276 100644 --- a/src/Filter/DateTime.php +++ b/src/Filter/DateTime.php @@ -31,7 +31,7 @@ public static function filter($value, bool $allowNull = false, DateTimeZoneStand return null; } - if ($value instanceof \DateTime) { + if ($value instanceof DateTimeStandard) { return $value; } @@ -43,7 +43,7 @@ public static function filter($value, bool $allowNull = false, DateTimeZoneStand throw new FilterException('$value is not a non-empty string'); } - return new \DateTime($value, $timezone); + return new DateTimeStandard($value, $timezone); } /** @@ -58,7 +58,7 @@ public static function filter($value, bool $allowNull = false, DateTimeZoneStand */ public static function format(DateTimeInterface $dateTime, string $format = 'c') : string { - if (!is_string($format) || trim($format) === '') { + if (empty(trim($format))) { throw new \InvalidArgumentException('$format is not a non-empty string'); } diff --git a/src/Filter/DateTimeZone.php b/src/Filter/DateTimeZone.php index d144d01..8275821 100644 --- a/src/Filter/DateTimeZone.php +++ b/src/Filter/DateTimeZone.php @@ -17,7 +17,6 @@ class DateTimeZone * * @return \DateTimeZone|null * - * @throws \InvalidArgumentException Thrown if $allowNull was not a boolean value. * @throws FilterException if the value did not pass validation. */ public static function filter($value, bool $allowNull = false) diff --git a/tests/Filter/DateTimeTest.php b/tests/Filter/DateTimeTest.php index 40d7dd9..43320c3 100644 --- a/tests/Filter/DateTimeTest.php +++ b/tests/Filter/DateTimeTest.php @@ -148,4 +148,19 @@ public function format() $now = new \DateTime(); $this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, 'Y-m-d H:i:s')); } + + /** + * Verify basic behavior of format(). + * + * @test + * @covers ::format + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage $format is not a non-empty string + */ + public function formatWithEmptyFormat() + { + $now = new \DateTime(); + $this->assertSame($now->format('Y-m-d H:i:s'), DateTime::format($now, ' ')); + } }