Skip to content

Commit 1ea95aa

Browse files
committed
⚡️
0 parents  commit 1ea95aa

File tree

9 files changed

+1104
-0
lines changed

9 files changed

+1104
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
composer.lock
3+
vendor
4+
build

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
env:
9+
matrix:
10+
- COMPOSER_FLAGS="--prefer-lowest"
11+
- COMPOSER_FLAGS=""
12+
13+
before_script:
14+
- travis_retry composer self-update
15+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
16+
17+
script: phpunit --coverage-clover clover
18+
19+
after_success:
20+
- wget https://scrutinizer-ci.com/ocular.phar
21+
- php ocular.phar code-coverage:upload --format=php-clover clover

LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Abdulrahman M <hi@abdumu.com>
4+
5+
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:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
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.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# array-helper
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/awssat/array-helper.svg?style=flat-square)](https://packagist.org/packages/awssat/array-helper)
4+
[![Build Status](https://img.shields.io/travis/awssat/array-helper/master.svg?style=flat-square)](https://travis-ci.org/awssat/array-helper)
5+
6+
7+
⚡️ A flexible, simple & yet powerful array manipulation helper for PHP. It gives you the magic of method chaining and it's easier and shorter to be included in views. It Supports most of [PHP built-in array functions](http://php.net/manual/en/book.array.php)
8+
9+
10+
```php
11+
arr([' ', 'hi ', null, ' welcome'])->map('trim')->filter()->get()
12+
>> ['hi', 'welcome']
13+
```
14+
## Features
15+
- Support All PHP array functions.
16+
- Short methods names, no need to write "array_" like array_map can be only map(..)
17+
- Array items can be retrireve or updated as properties or keys [->key or [key] ]
18+
- Support powefull conditional methods. if, else, if{AnyMethod}, endif.
19+
- Support camelCase and snake-case methods name.
20+
- Useful new methods like equal, exists .. etc
21+
22+
## Install/Use
23+
You can install the package via composer locally in your project folder:
24+
25+
```bash
26+
$ composer require awssat/array-helper
27+
```
28+
29+
After installing it, just start using the helper `arr([...])` or `ArrayHelper::make([...])`
30+
31+
## Examples
32+
33+
34+
Use any array function, no need for "array_", if you like it you can use it as in array_filter or arrayFilter .. all will work.
35+
```php
36+
$x = arr(['', 'item', null, 'item2'])->filter()->get()
37+
>> ['item', 'item2']
38+
```
39+
40+
You can use conditions, if(function() {...}), if{AnyMethod}, else(), endif()
41+
42+
```php
43+
$x = arr(['item', 'item2', null])
44+
->ifContains(null)
45+
->filter()
46+
->endif()
47+
->get()
48+
```
49+
you may also use useful method with `if` like `ifEmpty`, `ifKeyExists` or `ifEqual` etc.
50+
51+
52+
get() will return all items, while get(index) return a item in the array. all() is alias for get(all, true) which will ignore conditions and force return of items anyway.
53+
54+
The example above can be shortened using all() like this:
55+
```php
56+
$x = arr(['item', 'item2', null])
57+
->ifContains(null)
58+
->filter()
59+
->all()
60+
```
61+
62+
You can use do(callback) to run a callback on the array.
63+
64+
```php
65+
$x = arr(['item ', 'item2'])
66+
->do(function() {
67+
return $this->map('trim');
68+
})
69+
->all()
70+
```
71+
72+
73+
PHP built-in array_map, array_walk, array_filter, and array_reduce are the best!
74+
75+
array_map to loop through all items and change them.
76+
```php
77+
$array->map(function($item) {
78+
return 'Hello: '. strip_tags($item) . ' !';
79+
});
80+
```
81+
or for simple functions use, `$array->map('trim')`
82+
83+
array_walk can be used for looping without changing items
84+
```php
85+
$array->walk(function($item, $key) {
86+
print "$key: $item<br />\n";
87+
});
88+
```
89+
90+
or if you want to change the values
91+
```php
92+
$array->walk(function(&$item, $key) {
93+
$item = $item * 2;
94+
});
95+
```
96+
97+
array_filter is great for filtering an array
98+
```php
99+
$array->filter(function($item) {
100+
return $item > 5;
101+
});
102+
```
103+
or just `$array->filter()` to remove any value equal to FALSE.
104+
105+
106+
107+
108+
## Tests
109+
Simply use:
110+
```bash
111+
$ composer test
112+
```
113+
## Credits
114+
- [Abdulrahman M.](https://github.com/abdumu)
115+
- [All Contributors](../../contributors)
116+
117+
## License
118+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "awssat/array-helper",
3+
"description": "A flexible & powerful array manipulation helper for PHP",
4+
"keywords": [
5+
"array",
6+
"manipulation",
7+
"helper",
8+
"arrays",
9+
"pipechain"
10+
],
11+
"homepage": "https://github.com/awssat/array-helper",
12+
"license": "MIT",
13+
"authors": [{
14+
"name": "Awssat",
15+
"email": "hello@awssat.com"
16+
}],
17+
18+
"require": {
19+
"php": "~7.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^6.4"
23+
},
24+
25+
"autoload": {
26+
"psr-4": {
27+
"Awssat\\ArrayHelper\\": "src"
28+
},
29+
"files": [
30+
"src/functions.php"
31+
]
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Awssat\\ArrayHelper\\Test\\": "tests"
36+
}
37+
},
38+
"scripts": {
39+
"test": "vendor/bin/phpunit"
40+
},
41+
"config": {
42+
"sort-packages": true
43+
}
44+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Awssat Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

0 commit comments

Comments
 (0)