diff --git a/.coveralls.yml b/.coveralls.yml deleted file mode 100644 index 4eecff5..0000000 --- a/.coveralls.yml +++ /dev/null @@ -1,3 +0,0 @@ -service_name: travis-ci -coverage_clover: clover.xml -json_path: coveralls-upload.json diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml new file mode 100644 index 0000000..bb9ac06 --- /dev/null +++ b/.github/workflows/php.yml @@ -0,0 +1,29 @@ +name: PHP Composer + +on: + push: + branches: [ v3.x ] + pull_request: + branches: [ v3.x ] + +jobs: + build: + runs-on: ubuntu-20.04 + strategy: + matrix: + php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4'] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + - name: Validate composer.json and composer.lock + run: composer validate + - name: Install dependencies + run: composer install --prefer-dist --no-progress + - name: Run PHPCS + run: composer run-script lint + - name: Run PHPUnit + run: composer run-script test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index e2194d9..0000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -sudo: false -language: php -php: - - 7.0 - - 7.1 - - 7.2 - - 7.3 - - 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/php-coveralls -v diff --git a/README.md b/README.md index e8d127d..b0cc51d 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,38 @@ $value = '{ "string": "value", "array": [1, 2, 3] }'; assert($value === ['string' => 'value', 'array' => [1, 2, 3]]); ``` +#### UuidFilter::filter + +This filter verifies a given string is a valid universally unique identifier. + +The second parameter can be set to `true` to allow null values through without an error. + +The third parameter can be set to `true` to allow [Nil UUIDs](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.7) values through without an error. + +The fourth parameter determines which UUID version the value will be validated against. By default, the filter will succeed if the values matches version 1, 2, 3, 4, 5, 6, or 7 + +```php +// Filtering an UUID string +$value = '1a42403c-a29d-11ef-b864-0242ac120002'; +$filtered = \TraderInteractive\Filter\UuidFilter::filter($value); +assert($value === $filtered); + +// Filtering null values +$value = null; +$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, true); +assert(null === $filtered); + +// Filtering a nil UUID +$value = '00000000-0000-0000-0000-000000000000'; +$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, false, true); +assert($value === $filtered); + +// Filtering for only UUID v4 +$value = '1a42403c-a29d-41ef-b864-0242ac120002'; +$filtered = \TraderInteractive\Filter\UuidFilter::filter($value, false, false, [4]); +assert($value === $filtered); +``` + #### XmlFilter::filter This filter ensures the given string is valid XML. diff --git a/composer.json b/composer.json index 005000a..1ddd357 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,6 @@ "ext-dom": "*", "ext-json": "*", "ext-libxml": "*", - "php-coveralls/php-coveralls": "^2.0", "phpunit/phpunit": "^6.0", "squizlabs/php_codesniffer": "^3.2" }, @@ -33,5 +32,9 @@ }, "autoload": { "psr-4": { "TraderInteractive\\": "src/" } + }, + "scripts": { + "lint": "vendor/bin/phpcs", + "test": "vendor/bin/phpunit" } } diff --git a/src/Filter/UuidFilter.php b/src/Filter/UuidFilter.php new file mode 100644 index 0000000..3180a6b --- /dev/null +++ b/src/Filter/UuidFilter.php @@ -0,0 +1,121 @@ + + */ +final class UuidFilterTest extends TestCase +{ + /** + * @var string + * @internal + */ + const UUID_V1 = '1a42403c-a29d-11ef-b864-0242ac120002'; + + /** + * @var string + * @internal + */ + const UUID_V4 = 'cc468b36-0b9d-4c93-b8e9-d5e949331ffb'; + + /** + * @var string + * @internal + */ + const UUID_V7 = '01932b4a-af2b-7093-af59-2fb2044d13d8'; + + /** + * @test + * @covers ::filter + */ + public function filterUuidV1() + { + $this->assertSame(self::UUID_V1, UuidFilter::filter(self::UUID_V1)); + } + + /** + * @test + * @covers ::filter + */ + public function filterUuidV4() + { + $this->assertSame(self::UUID_V4, UuidFilter::filter(self::UUID_V4)); + } + + /** + * @test + * @covers ::filter + */ + public function filterUuidV7() + { + $this->assertSame(self::UUID_V7, UuidFilter::filter(self::UUID_V7)); + } + + /** + * @test + * @covers ::filter + */ + public function filterNullAllowedNullIsTrue() + { + $this->assertNull(UuidFilter::filter(null, true)); + } + + /** + * @test + * @covers ::filter + */ + public function filterNullAllowedNullIsFalse() + { + $this->expectException(FilterException::class); + UuidFilter::filter(null, false); + } + + /** + * @test + * @covers ::filter + */ + public function filterWithInvalidVersionSpecified() + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage(sprintf(UuidFilter::UNSUPPORTED_VERSION_ERROR_FORMAT, 0)); + UuidFilter::filter(self::UUID_V7, false, false, [0]); + } + + /** + * @test + * @covers ::filter + */ + public function filterValueDoesNotMatchGivenVersions() + { + $this->expectException(FilterException::class); + $this->expectExceptionMessage( + sprintf( + UuidFilter::FILTER_ERROR_FORMAT, + self::UUID_V4, + implode(', ', [1,7]) + ) + ); + UuidFilter::filter(self::UUID_V4, false, false, [1,7]); + } + + /** + * @test + * @covers ::filter + */ + public function filterNilUuid() + { + $value = UuidFilter::NIL_UUID; + $this->assertSame($value, UuidFilter::filter($value, false, true)); + } + + /** + * @test + * @covers ::filter + */ + public function filterNilUuidNilNotAllowed() + { + $value = UuidFilter::NIL_UUID; + $this->expectException(FilterException::class); + $this->expectExceptionMessage(sprintf(UuidFilter::NIL_NOT_ALLOWED_ERROR_FORMAT, $value)); + UuidFilter::filter($value, false, false); + } +}