Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 3 #76

Merged
merged 44 commits into from Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2428de7
remove build.php
Mar 2, 2018
f75733b
remove composer.lock
Mar 2, 2018
cd7570d
github files
Mar 2, 2018
aaa5f48
phpunit files
Mar 2, 2018
20e3b46
phpcs files
Mar 2, 2018
b9cc1f0
actually gitignore this time
Mar 2, 2018
bceb050
coveralls
Mar 2, 2018
f345d84
travis
Mar 2, 2018
fedbf9d
scrutinizer
Mar 2, 2018
fdba355
phpunit passing
Mar 2, 2018
3846611
scrutinizer tweak
Mar 2, 2018
0c2926b
de to ti
Mar 2, 2018
d7a5125
update license
Mar 2, 2018
ffa03f9
removed old contributing file
Mar 2, 2018
ccfa7fc
refactor filterertest
Mar 2, 2018
b5c3b3a
refactor unit tests
Mar 2, 2018
30758f9
filterer to 7.0
Mar 2, 2018
cbf1615
arrays refactor
Mar 2, 2018
fc9293f
refactor booleans
Mar 2, 2018
2db4c0a
refactor datetime
Mar 2, 2018
49cd500
update datetime
Mar 2, 2018
417dde4
update datetimezone
Mar 2, 2018
ac61013
update email
Mar 2, 2018
1eb4bb9
update floats
Mar 2, 2018
b1ad537
update ints
Mar 2, 2018
8bdfe27
update strings
Mar 2, 2018
762cefb
update unsigned int
Mar 2, 2018
5989a96
update url
Mar 2, 2018
116f798
refactor filter
Mar 2, 2018
c63ce93
scrutinizer score
Mar 5, 2018
8c41a58
Add DEFAULT_FILTER_ALIASES constant
chadicus Mar 6, 2018
b2b9873
Move some Arrays methods to Filterer
chadicus Mar 6, 2018
bd840e4
Add traderinteractive/filter-strings dependency
chadicus Mar 6, 2018
e996e53
Add traderinteractive/filter-arrays dependency
chadicus Mar 6, 2018
557baeb
Add traderinteractive/filter-floats dependency
chadicus Mar 6, 2018
f9ea261
Add traderinteractive/filter-dates dependency
chadicus Mar 6, 2018
a8dfc75
Add traderinteractive/filter-bools dependency
chadicus Mar 6, 2018
8d1441d
Add traderinteractive/filter-ints dependency
chadicus Mar 6, 2018
5be04e9
Remove Filter\Exception
chadicus Mar 6, 2018
ac612f4
Update README.md
chadicus Mar 6, 2018
5263787
Cover private methods by default in unit tests
chadicus Mar 7, 2018
321c71b
Update scrutinizer config
chadicus Mar 6, 2018
8c638f7
Use FilterException in FIlterer
chadicus Mar 7, 2018
3d072f3
Update README badges
chadicus Mar 7, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .coveralls.yml
@@ -1,4 +1,3 @@
service_name: travis-ci
src_dir: .
coverage_clover: clover.xml
json_path: coveralls-upload.json
13 changes: 13 additions & 0 deletions .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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
@@ -0,0 +1 @@
* @traderinteractive/opensource
24 changes: 24 additions & 0 deletions .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-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/).

6 changes: 6 additions & 0 deletions .github/ISSUE_TEMPLATE.md
@@ -0,0 +1,6 @@
## Expected Behavior

## Actual Behavior

## Steps to reproduce the behavior

9 changes: 9 additions & 0 deletions .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

2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
/coverage/
/vendor/
/clover.xml
phpunit.xml
composer.lock
11 changes: 4 additions & 7 deletions .scrutinizer.yml
@@ -1,7 +1,6 @@
filter:
excluded_paths:
- 'vendor/*'
- 'tests/*'
before_commands:
- 'composer install'
tools:
Expand All @@ -14,12 +13,10 @@ tools:
php_loc:
excluded_dirs:
- vendor
- tests
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.label("coding-style").new.count > 5' # More than 5 new coding style issues.
- '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(< C).new.exists'
- 'issues.label("coding-style").new.exists'
- 'issues.severity(>= MAJOR).new.exists'
- 'project.metric("scrutinizer.quality", < 7)'
19 changes: 12 additions & 7 deletions .travis.yml
@@ -1,13 +1,18 @@
sudo: false
language: php
php:
- 5.6
- 7
- hhvm
install: composer install
script: ./vendor/bin/phpunit --coverage-clover clover.xml
after_success: sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then ./vendor/bin/coveralls -v; fi'
- 7.0
- 7.1
- 7.2
- nightly
env:
- PREFER_LOWEST="--prefer-lowest --prefer-stable"
- PREFER_LOWEST=""
matrix:
fast_finish: true
allow_failures:
- php: hhvm
- php: nightly
before_script:
- composer update $PREFER_LOWEST
script: ./vendor/bin/phpunit --coverage-clover clover.xml
after_success: ./vendor/bin/coveralls -v
15 changes: 0 additions & 15 deletions CONTRIBUTING.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Dominion Enterprises
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
Expand Down
115 changes: 61 additions & 54 deletions README.md
@@ -1,15 +1,15 @@
#filter-php
# filter-php
A filtering implementation for verifying correct data and performing typical modifications to data.

[![Build Status](http://img.shields.io/travis/dominionenterprises/filter-php.svg?style=flat)](https://travis-ci.org/dominionenterprises/filter-php)
[![Scrutinizer Code Quality](http://img.shields.io/scrutinizer/g/dominionenterprises/filter-php.svg?style=flat)](https://scrutinizer-ci.com/g/dominionenterprises/filter-php/)
[![Code Coverage](http://img.shields.io/coveralls/dominionenterprises/filter-php.svg?style=flat)](https://coveralls.io/r/dominionenterprises/filter-php)
[![Build Status](http://img.shields.io/travis/traderinteractive/filter-php.svg?style=flat)](https://travis-ci.org/traderinteractive/filter-php)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[![Build Status](https://travis-ci.org/traderinteractive/filter-php.svg?branch=master)](https://travis-ci.org/traderinteractive/filter-php)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/filter-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/filter-php/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/filter-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/filter-php?branch=master)

[![Latest Stable Version](https://poser.pugx.org/traderinteractive/filter/v/stable)](https://packagist.org/packages/traderinteractive/filter)
[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/filter/v/unstable)](https://packagist.org/packages/traderinteractive/filter)
[![License](https://poser.pugx.org/traderinteractive/filter/license)](https://packagist.org/packages/traderinteractive/filter)

[![Total Downloads](https://poser.pugx.org/traderinteractive/filter/downloads)](https://packagist.org/packages/traderinteractive/filter)
[![Daily Downloads](https://poser.pugx.org/traderinteractive/filter/d/daily)](https://packagist.org/packages/traderinteractive/filter)
[![Monthly Downloads](https://poser.pugx.org/traderinteractive/filter/d/monthly)](https://packagist.org/packages/traderinteractive/filter)

[![Scrutinizer Code Quality](http://img.shields.io/scrutinizer/g/traderinteractive/filter-php.svg?style=flat)](https://scrutinizer-ci.com/g/traderinteractive/filter-php/)
[![Code Coverage](http://img.shields.io/coveralls/traderinteractive/filter-php.svg?style=flat)](https://coveralls.io/r/traderinteractive/filter-php)

[![Latest Stable Version](http://img.shields.io/packagist/v/dominionenterprises/filter.svg?style=flat)](https://packagist.org/packages/dominionenterprises/filter)
[![Total Downloads](http://img.shields.io/packagist/dt/dominionenterprises/filter.svg?style=flat)](https://packagist.org/packages/dominionenterprises/filter)
[![License](http://img.shields.io/packagist/l/dominionenterprises/filter.svg?style=flat)](https://packagist.org/packages/dominionenterprises/filter)
[![Latest Stable Version](http://img.shields.io/packagist/v/traderinteractive/filter.svg?style=flat)](https://packagist.org/packages/traderinteractive/filter)
[![Total Downloads](http://img.shields.io/packagist/dt/traderinteractive/filter.svg?style=flat)](https://packagist.org/packages/traderinteractive/filter)
[![License](http://img.shields.io/packagist/l/traderinteractive/filter.svg?style=flat)](https://packagist.org/packages/traderinteractive/filter)

##Features
## Features
* Compact, readable specification
* Filter with any php callable such as
* Anonymous function
Expand All @@ -21,7 +21,18 @@ A filtering implementation for verifying correct data and performing typical mod
* Optionally returns unknown fields
* Filter alias support

##Example
## Components

This package is a partial metapackage aggregating the following components:

* [traderinteractive/filter-arrays](https://github.com/traderinteractive/filter-arrays-php)
* [traderinteractive/filter-bools](https://github.com/traderinteractive/filter-bools-php)
* [traderinteractive/filter-dates](https://github.com/traderinteractive/filter-dates-php)
* [traderinteractive/filter-floats](https://github.com/traderinteractive/filter-floats-php)
* [traderinteractive/filter-ints](https://github.com/traderinteractive/filter-ints-php)
* [traderinteractive/filter-strings](https://github.com/traderinteractive/filter-strings-php)

## Example
```php
class AppendFilter
{
Expand All @@ -34,7 +45,7 @@ $appendFilter = new AppendFilter();

$trimFunc = function($val) { return trim($val); };

list($status, $result, $error, $unknowns) = DominionEnterprises\Filterer::filter(
list($status, $result, $error, $unknowns) = TraderInteractive\Filterer::filter(
[
'field one' => [[$trimFunc], ['substr', 0, 3], [[$appendFilter, 'filter'], 'boo']],
'field two' => ['required' => true, ['floatval']],
Expand Down Expand Up @@ -66,19 +77,15 @@ array(0) {
}
```

##Composer
## Composer
To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on
`dominionenterprises/filter` to your project's `composer.json` file such as:
`traderinteractive/filter` to your project's `composer.json` file such as:

```json
{
"require": {
"dominionenterprises/filter": "~1.0"
}
}
```sh
composer require traderinteractive/filter
```

##Documentation
## Documentation
Found in the [source](src/Filterer.php) itself, take a look!

### Filterer
Expand Down Expand Up @@ -112,51 +119,51 @@ The example above should help clarify all this.
### Included Filters
Of course, any function can potentially be used as a filter, but we include some useful filters with aliases for common circumstances.

#### Arrays::in
Aliased in the filterer as `in`, this filter is a wrapper around `in_array` including support for strict equality testing.

The following does a strict check for `$value` against the 3 accepted values.
```php
\DominionEnterprises\Filter\Arrays::in($value, ['a', 'b', 'c']);
```

#### Arrays::filter
Aliased in the filterer as `array`, this filter verifies that the argument is an array and checks the length of the array against bounds. The
default bounds are 1+, so an empty array fails by default.

The following checks that the `$value` is an array with exactly 3 elements.
```php
\DominionEnterprises\Filter\Arrays::filter($value, 3, 3);
```

#### Arrays::ofScalars
#### Filterer::ofScalars
Aliased in the filterer as `ofScalars`, this filter verifies that the argument is an array (possibly empty) of scalar items that each pass the
given filters (given in the same format as used by `Filterer::filter`.

The following checks that `$value` is an array of unsigned integers.
```php
$value = \DominionEnterprises\Filter\Arrays::ofScalars($value, [['uint']]);
$value = \TraderInteractive\Filter\Filterer::ofScalars($value, [['uint']]);
```

#### Arrays::ofArrays
#### Filterer::ofArrays
Aliased in the filterer as `ofArrays`, this filter verifies that the argument is an array (possibly empty) of arrays that each pass the given
filters (given in the same format as used by `Filterer::filter`.

The following checks that `$value` is an array of items that each have an `id` key with a numeric value. No other keys would be allowed. For
example, the following is valid input: `[['id' => '1'], ['id' => '2']]`.
```php
$value = \DominionEnterprises\Filter\Arrays::ofArrays($value, ['id' => [['uint']]]);
$value = \TraderInteractive\Filter\Filterer::ofArrays($value, ['id' => [['uint']]]);
```

#### Arrays::ofArray
#### Filterer::ofArray
Aliased in the filterer as `ofArray`, this filter verifies that the argument is an array that passes the given specification. This is
essentially a flipped version of `Filterer::filter` that allows for testing nested associative arrays.

#### Arrays::in
Aliased in the filterer as `in`, this filter is a wrapper around `in_array` including support for strict equality testing.

The following does a strict check for `$value` against the 3 accepted values.
```php
\TraderInteractive\Filter\Arrays::in($value, ['a', 'b', 'c']);
```

#### Arrays::filter
Aliased in the filterer as `array`, this filter verifies that the argument is an array and checks the length of the array against bounds. The
default bounds are 1+, so an empty array fails by default.

The following checks that the `$value` is an array with exactly 3 elements.
```php
\TraderInteractive\Filter\Arrays::filter($value, 3, 3);
```

#### Arrays::flatten
Aliased in the filterer as `flatten`, this filter flattens a multi-dimensional array to a single dimension. The order of values will be
maintained, but the keys themselves will not. For example:
```php
$value = \DominionEnterprises\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
$value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
assert($value === [1, 2, 3, 4, 5]);
```

Expand All @@ -167,15 +174,15 @@ are lists of strings for true values and false values. By default, the strings

The following example converts `$value` to a boolean allowing the strings "on" and "of".
```php
$enabled = \DominionEnterprises\Filter\Booleans::filter($value, false, ['on'], ['off']);
$enabled = \TraderInteractive\Filter\Booleans::filter($value, false, ['on'], ['off']);
```
#### Booleans::convert
Aliased in the filterer as `bool-convert`, this filter will convert a given boolean value into the provided true or false conditions. By default the
return values are the strings 'true' and 'false'

The following converts the boolean `$value` to either 'yes' or 'no'
```php
$answer = \DominionEnterprises\Filter\Booleans::convert($value, 'yes', 'no');
$answer = \TraderInteractive\Filter\Booleans::convert($value, 'yes', 'no');
```

#### Floats/Ints/UnsignedInt::filter
Expand All @@ -191,7 +198,7 @@ validation.

The following checks that `$value` is an integer between 1 and 100 inclusive, and returns the integer (after casting it if it was a string).
```php
$value = \DominionEnterprises\Filter\UnsignedInt::filter($value, false, 1, 100);
$value = \TraderInteractive\Filter\UnsignedInt::filter($value, false, 1, 100);
```

#### Strings::filter
Expand All @@ -201,14 +208,14 @@ string. The default bounds are 1+, so an empty string fails by default.

The following checks that `$value` is a non-empty string.
```php
\DominionEnterprises\Filter\Strings::filter($value);
\TraderInteractive\Filter\Strings::filter($value);
```

#### Strings::explode
Aliased in the filterer as `explode`, this filter is essentially a wrapper around the built-in [`explode`](http://www.php.net/explode) method
with the value first in order to work with the `Filterer`. It also defaults to using `,` as a delimiter. For example:
```php
$value = \DominionEnterprises\Filter\Strings::explode('abc,def,ghi');
$value = \TraderInteractive\Filter\Strings::explode('abc,def,ghi');
assert($value === ['abc', 'def', 'ghi']);
```

Expand All @@ -219,46 +226,46 @@ null values through without an error (they will stay null and not get converted

The following checks that `$value` is a URL.
```php
\DominionEnterprises\Filter\Url::filter($value);
\TraderInteractive\Filter\Url::filter($value);
```

#### Email::filter
Aliased in the filterer as `email`, this filter verifies that the argument is an email.

The following checks that `$value` is an email.
```php
\DominionEnterprises\Filter\Email::filter($value);
\TraderInteractive\Filter\Email::filter($value);
```

#### 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 = \DominionEnterprises\Filter\DateTime::filter('2014-02-04T11:55:00-0500');
$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 = \DominionEnterprises\Filter\DateTime::format($value, 'Y-m-d H:i:s');
$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 = \DominionEnterprises\Filter\DateTimeZone::filter('America/New_York');
$timezone = \TraderInteractive\Filter\DateTimeZone::filter('America/New_York');
```

##Contact
Developers may be contacted at:

* [Pull Requests](https://github.com/dominionenterprises/filter-php/pulls)
* [Issues](https://github.com/dominionenterprises/filter-php/issues)
* [Pull Requests](https://github.com/traderinteractive/filter-php/pulls)
* [Issues](https://github.com/traderinteractive/filter-php/issues)

##Project Build
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:
Expand Down