Skip to content

Commit 7216ae6

Browse files
authored
Merge pull request #1 from chadicus/master
Version 3
2 parents 48a39d8 + 076da25 commit 7216ae6

12 files changed

+412
-2
lines changed

.coveralls.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
service_name: travis-ci
2+
coverage_clover: clover.xml
3+
json_path: coveralls-upload.json

.gitattributes

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Enforce Unix newlines
2+
* text=lf
3+
4+
# Exclude unused files
5+
# see: https://redd.it/2jzp6k
6+
/tests export-ignore
7+
/.github export-ignore
8+
/.gitattributes export-ignore
9+
/.gitignore export-ignore
10+
/.*.yml export-ignore
11+
/*.xml.dist export-ignore
12+
/README.md export-ignore

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
composer.lock
3+
phpcs.xml
4+
phpunit.xml
5+
clover.xml

.scrutinizer.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
filter:
2+
excluded_paths:
3+
- 'vendor/*'
4+
- 'docs/*'
5+
before_commands:
6+
- 'composer install'
7+
tools:
8+
php_analyzer: true
9+
php_mess_detector: true
10+
php_code_sniffer:
11+
config:
12+
standard: PSR2
13+
sensiolabs_security_checker: true
14+
php_loc:
15+
excluded_dirs:
16+
- vendor
17+
- docs
18+
php_pdepend: true
19+
php_sim: true
20+
build_failure_conditions:
21+
- 'elements.rating(< B).new.exists'
22+
- 'issues.label("coding-style").new.exists'
23+
- 'issues.severity(>= MAJOR).new.exists'
24+
- 'project.metric("scrutinizer.quality", < 9)'

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
php:
3+
- 7.0
4+
- 7.1
5+
- 7.2
6+
- nightly
7+
env:
8+
- PREFER_LOWEST="--prefer-lowest --prefer-stable"
9+
- PREFER_LOWEST=""
10+
matrix:
11+
fast_finish: true
12+
allow_failures:
13+
- php: nightly
14+
before_script:
15+
- composer update $PREFER_LOWEST
16+
script:
17+
- ./vendor/bin/phpunit
18+
after_success: ./vendor/bin/coveralls -v

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Trader Interactive
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,68 @@
1-
# filter-arrays-php
2-
A filtering implementation for verifying correct data and performing typical modifications to arrays.
1+
# TraderInteractive Filter\Arrays
2+
3+
[![Build Status](https://travis-ci.org/traderinteractive/filter-arrays-php.svg?branch=master)](https://travis-ci.org/traderinteractive/filter-arrays-php)
4+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/traderinteractive/filter-arrays-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/traderinteractive/filter-arrays-php/?branch=master)
5+
[![Coverage Status](https://coveralls.io/repos/github/traderinteractive/filter-arrays-php/badge.svg?branch=master)](https://coveralls.io/github/traderinteractive/filter-arrays-php?branch=master)
6+
7+
[![Latest Stable Version](https://poser.pugx.org/traderinteractive/filter-arrays/v/stable)](https://packagist.org/packages/traderinteractive/filter-arrays)
8+
[![Latest Unstable Version](https://poser.pugx.org/traderinteractive/filter-arrays/v/unstable)](https://packagist.org/packages/traderinteractive/filter-arrays)
9+
[![License](https://poser.pugx.org/traderinteractive/filter-arrays/license)](https://packagist.org/packages/traderinteractive/filter-arrays)
10+
11+
[![Total Downloads](https://poser.pugx.org/traderinteractive/filter-arrays/downloads)](https://packagist.org/packages/traderinteractive/filter-arrays)
12+
[![Daily Downloads](https://poser.pugx.org/traderinteractive/filter-arrays/d/daily)](https://packagist.org/packages/traderinteractive/filter-arrays)
13+
[![Monthly Downloads](https://poser.pugx.org/traderinteractive/filter-arrays/d/monthly)](https://packagist.org/packages/traderinteractive/filter-arrays)
14+
15+
[![Documentation](https://img.shields.io/badge/reference-phpdoc-blue.svg?style=flat)](https://traderinteractive.github.io/filter-arrays-php/)
16+
17+
A filtering implementation for verifying correct data and performing typical modifications to arrays
18+
19+
## Requirements
20+
21+
Requires PHP 7.0 or newer and uses composer to install further PHP dependencies. See the [composer specification](composer.json) for more details.
22+
23+
## Installation
24+
25+
filter-arrays-php can be installed for use in your project using [composer](http://getcomposer.org).
26+
The recommended way of using this library in your project is to add a `composer.json` file to your project. The following contents would add filter-arrays-php as a dependency:
27+
```sh
28+
composer require traderinteractive/filter-arrays
29+
```
30+
31+
## Included Filters
32+
33+
#### Arrays::in
34+
This filter is a wrapper around `in_array` including support for strict equality testing.
35+
36+
The following does a strict check for `$value` against the 3 accepted values.
37+
```php
38+
\TraderInteractive\Filter\Arrays::in($value, ['a', 'b', 'c']);
39+
```
40+
41+
#### Arrays::filter
42+
43+
This filter verifies that the argument is an array and checks the length of the array against bounds. The
44+
default bounds are 1+, so an empty array fails by default.
45+
46+
The following checks that the `$value` is an array with exactly 3 elements.
47+
```php
48+
\TraderInteractive\Filter\Arrays::filter($value, 3, 3);
49+
```
50+
51+
#### Arrays::flatten
52+
53+
This filter flattens a multi-dimensional array to a single dimension. The order of values will be
54+
maintained, but the keys themselves will not. For example:
55+
```php
56+
$value = \TraderInteractive\Filter\Arrays::flatten([[1, 2], [3, [4, 5]]]);
57+
assert($value === [1, 2, 3, 4, 5]);
58+
```
59+
60+
## Project Build
61+
62+
With a checkout of the code get [Composer](http://getcomposer.org) in your PATH and run:
63+
``sh
64+
composer install
65+
./vendor/bin/phpunit
66+
./vendor/bin/phpcs
67+
```
68+
For more information on our build process, read through out our [Contribution Guidelines](./.github/CONTRIBUTING.md).

composer.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "traderinteractive/filter-arrays",
3+
"description": "A filtering implementation for verifying correct data and performing typical modifications to arrays",
4+
"keywords": [
5+
"utility",
6+
"array",
7+
"filter"
8+
],
9+
"license": "MIT",
10+
"configure": {
11+
"sort-packages": true
12+
},
13+
"require": {
14+
"php": "^7.0",
15+
"traderinteractive/exceptions": "^1.0"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"TraderInteractive\\Filter\\": "src"
20+
}
21+
},
22+
"require-dev": {
23+
"squizlabs/php_codesniffer": "^3.2",
24+
"phpunit/phpunit": "^6.5",
25+
"php-coveralls/php-coveralls": "^1.0"
26+
}
27+
}

phpcs.xml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<file>.</file>
4+
<exclude-pattern>*/vendor/*</exclude-pattern>
5+
<arg name="colors" />
6+
<arg value="p" />
7+
<rule ref="PSR2" />
8+
</ruleset>

phpunit.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<phpunit colors="true" forceCoversAnnotation="true">
2+
<testsuites>
3+
<testsuite name="TraderInteractive Library Test Suite">
4+
<directory suffix="Test.php">tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist>
9+
<directory>src</directory>
10+
</whitelist>
11+
</filter>
12+
<logging>
13+
<log type="coverage-clover" target="./clover.xml"/>
14+
</logging>
15+
</phpunit>

src/Arrays.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace TraderInteractive\Filter;
4+
5+
use TraderInteractive\Exceptions\FilterException;
6+
7+
/**
8+
* A collection of filters for arrays.
9+
*/
10+
final class Arrays
11+
{
12+
/**
13+
* Filter an array by throwing if not an array or count not in the min/max range.
14+
*
15+
* @param mixed $value The value to filter.
16+
* @param integer $minCount The minimum allowed count in the array.
17+
* @param integer $maxCount The maximum allowed count in the array.
18+
*
19+
* @return array
20+
*
21+
* @throws FilterException if $value is not an array
22+
* @throws FilterException if $value count is less than $minCount
23+
* @throws FilterException if $value count is greater than $maxCount
24+
*/
25+
public static function filter($value, int $minCount = 1, int $maxCount = PHP_INT_MAX) : array
26+
{
27+
if (!is_array($value)) {
28+
throw new FilterException("Value '" . trim(var_export($value, true), "'") . "' is not an array");
29+
}
30+
31+
$count = count($value);
32+
if ($count < $minCount) {
33+
throw new FilterException("\$value count of {$count} is less than {$minCount}");
34+
}
35+
36+
if ($count > $maxCount) {
37+
throw new FilterException("\$value count of {$count} is greater than {$maxCount}");
38+
}
39+
40+
return $value;
41+
}
42+
43+
/**
44+
* Filter an array by throwing if $value is not in $haystack adhering to $strict.
45+
*
46+
* @param mixed $value The searched value.
47+
* @param array $haystack The array to be searched.
48+
* @param bool $strict Flag to compare strictly or not. @see in_array()
49+
*
50+
* @return mixed The passed in value
51+
*
52+
* @throws FilterException if $value is not in array $haystack
53+
*/
54+
public static function in($value, array $haystack, bool $strict = true)
55+
{
56+
if (!in_array($value, $haystack, $strict)) {
57+
throw new FilterException(
58+
"Value '" . trim(var_export($value, true), "'") . "' is not in array " . var_export($haystack, true)
59+
);
60+
}
61+
62+
return $value;
63+
}
64+
65+
/**
66+
* Given a multi-dimensional array, flatten the array to a single level.
67+
*
68+
* The order of the values will be maintained, but the keys will not.
69+
*
70+
* For example, given the array [[1, 2], [3, [4, 5]]], this would result in the array [1, 2, 3, 4, 5].
71+
*
72+
* @param array $value The array to flatten.
73+
*
74+
* @return array The single-dimension array.
75+
*/
76+
public static function flatten(array $value) : array
77+
{
78+
$result = [];
79+
80+
array_walk_recursive(
81+
$value,
82+
function ($item) use (&$result) {
83+
$result[] = $item;
84+
}
85+
);
86+
87+
return $result;
88+
}
89+
}

0 commit comments

Comments
 (0)