Skip to content

Commit 11ed21b

Browse files
committed
Initial Commit
0 parents  commit 11ed21b

9 files changed

+595
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
.php_cs.cache

.travis.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: php
2+
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- php: 7.1
8+
9+
cache:
10+
directories:
11+
- $HOME/.composer/cache/files
12+
13+
before_install:
14+
- composer self-update
15+
16+
install:
17+
- composer install
18+
19+
script:
20+
- vendor/bin/phpunit

CONTRIBUTING.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
If you wish to contribute, please fork it, make your changes, and submit a pull request.
2+
3+
## Pull Requests
4+
5+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**
6+
7+
- **Unit tests** - Where possible, pull requests need to have unit tests available, and the unit tests should not fail.
8+
9+
- **Documentation** - Make sure the README and any other relevant documentation are kept up-to-date.
10+
11+
## Running Tests
12+
13+
To run the unit tests, execute the following command
14+
15+
``` bash
16+
$ vendor/bin/phpunit
17+
```

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) SolidWorx <open-source@solidworx.co>
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
13+
all 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
21+
THE SOFTWARE.

README.md

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Util
2+
3+
[![Build Status](https://travis-ci.org/SolidWorx/Util.svg)](https://travis-ci.org/SolidWorx/Util)
4+
5+
A collection of utility classes for everyday use
6+
7+
# Table of Contents
8+
- [Requirements](#requirements)
9+
- [Installation](#installation)
10+
- [Composer](#composer)
11+
- [Usage](#usage)
12+
- [ArrayUtil] (#array-util)
13+
- [Testing](#testing)
14+
- [Contributing](#contributing)
15+
- [Licence](#licence)
16+
17+
18+
## Requirements
19+
20+
Minimum PHP requirement is PHP 7.1+
21+
22+
## Installation
23+
24+
### Composer
25+
26+
```bash
27+
$ composer require solidworx/util
28+
```
29+
30+
## Usage
31+
32+
### ArrayUtil
33+
34+
<b>column</b>:
35+
36+
Return the values from a single column in the input array. The input can be an array of arrays or objects.
37+
This method is an enhancement to the normal `array_column` function.
38+
This can be useful if you need to get a specific value from a collection.
39+
40+
Using an array input
41+
42+
```php
43+
<?php
44+
$input = [
45+
['test' => 'one'],
46+
['test' => 'two'],
47+
['test' => 'three'],
48+
];
49+
50+
$columns = ArrayUtil::column($input, 'test');
51+
52+
/* $columns = array (
53+
0 => 'one',
54+
1 => 'two',
55+
2 => 'three',
56+
);*/
57+
58+
```
59+
60+
Using an object with public properties
61+
62+
```php
63+
<?php
64+
65+
class Foo {
66+
public $test;
67+
}
68+
69+
$foo1 = new Foo;
70+
$foo1->test = 'one';
71+
72+
$foo2 = new Foo;
73+
$foo2->test = 'two';
74+
75+
$foo3 = new Foo;
76+
$foo3->test = 'three';
77+
78+
$input = [
79+
$foo1,
80+
$foo2,
81+
$foo3,
82+
];
83+
84+
$columns = ArrayUtil::column($input, 'test');
85+
86+
/* $columns = array (
87+
0 => 'one',
88+
1 => 'two',
89+
2 => 'three',
90+
);*/
91+
92+
```
93+
94+
Using an object with methods
95+
96+
```php
97+
<?php
98+
99+
class Foo {
100+
private $value;
101+
102+
public function __construct($value)
103+
{
104+
$this->value = $value;
105+
}
106+
107+
public function test()
108+
{
109+
return $this->>value;
110+
}
111+
}
112+
113+
$input = [
114+
new Foo('one'),
115+
new Foo('two'),
116+
new Foo('three'),
117+
];
118+
119+
$columns = ArrayUtil::column($input, 'test');
120+
121+
/* $columns = array (
122+
0 => 'one',
123+
1 => 'two',
124+
2 => 'three',
125+
);*/
126+
127+
```
128+
129+
Using an object with getters
130+
131+
```php
132+
<?php
133+
134+
class Foo {
135+
private $value;
136+
137+
public function __construct($value)
138+
{
139+
$this->value = $value;
140+
}
141+
142+
public function getTest()
143+
{
144+
return $this->value;
145+
}
146+
}
147+
148+
$input = [
149+
new Foo('one'),
150+
new Foo('two'),
151+
new Foo('three'),
152+
];
153+
154+
$columns = ArrayUtil::column($input, 'test');
155+
156+
/* $columns = array (
157+
0 => 'one',
158+
1 => 'two',
159+
2 => 'three',
160+
);*/
161+
162+
```
163+
164+
165+
Getting all the email addresses of your users:
166+
167+
```php
168+
<?php
169+
170+
$users = $userRepository->findAll();
171+
172+
$emails = ArrayUtil::column($users, 'email');
173+
174+
```
175+
176+
By default, all the `null` values are filtered out. If you want to keep the `null` values, pass `false` as the third parameter:
177+
178+
```php
179+
$users = $userRepository->findAll();
180+
181+
$emails = ArrayUtil::column($users, 'email', false); // Will keep empty values in the result
182+
```
183+
184+
185+
## Testing
186+
187+
To run the unit tests, execute the following command
188+
189+
```bash
190+
$ vendor/bin/phpunit
191+
```
192+
193+
## Contributing
194+
195+
See [CONTRIBUTING](https://github.com/SolidWorx/Util/blob/master/CONTRIBUTING.md)
196+
197+
## License
198+
199+
This library is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
200+
201+
Please see the [LICENSE](LICENSE) file for the full license.

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "solidworx/util",
3+
"description": "A collection of utility classes for everyday use",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords" : [
7+
"util",
8+
"utility"
9+
],
10+
"authors": [
11+
{
12+
"name": "Pierre du Plessis",
13+
"email": "pdples@gmail.com"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"SolidWorx\\Util\\": "./src"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"SolidWorx\\Util\\Tests\\": "./tests"
24+
}
25+
},
26+
"require": {
27+
"php": ">=7.1.0",
28+
"symfony/property-access": "^2.7|^3.0"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "^6.0"
32+
},
33+
"extra": {
34+
"branch-alias": {
35+
"dev-master": "1.0-dev"
36+
}
37+
}
38+
}

phpunit.xml.dist

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<testsuites>
10+
<testsuite name="Util Test Suite">
11+
<directory>./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
<filter>
16+
<whitelist>
17+
<directory>./src</directory>
18+
</whitelist>
19+
</filter>
20+
</phpunit>

src/ArrayUtil.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Util package.
7+
*
8+
* (c) SolidWorx <open-source@solidworx.co>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace SolidWorx\Util;
15+
16+
use Symfony\Component\PropertyAccess\PropertyAccess;
17+
18+
class ArrayUtil
19+
{
20+
/**
21+
* Returns a specific column from an array.
22+
*
23+
* @param array|\Traversable $array
24+
* @param string $column
25+
* @param bool $filter To filter out empty values
26+
*
27+
* @return array
28+
*
29+
* @throws \Exception
30+
*/
31+
public static function column($array, string $column, bool $filter = true): array
32+
{
33+
if (!is_array($array) && !$array instanceof \Traversable) {
34+
throw new \Exception(sprintf('Array or instance of Traversable expected, "%s" given', gettype($array)));
35+
}
36+
37+
$filterFunc = function (array $result) use ($filter) {
38+
return $filter ? array_filter($result, function ($item): bool {
39+
return null !== $item;
40+
}) : $result;
41+
};
42+
43+
if (is_array($array)) {
44+
reset($array);
45+
46+
if (is_array($array[key($array)])) {
47+
return $filterFunc(array_column($array, $column));
48+
}
49+
}
50+
51+
$accessor = PropertyAccess::createPropertyAccessor();
52+
53+
$result = [];
54+
55+
foreach ($array as $item) {
56+
$check = $column;
57+
if ((is_array($item) || $item instanceof \ArrayAccess) && '[' !== $column[0]) {
58+
$check = '['.$column.']';
59+
}
60+
61+
$result[] = $accessor->getValue($item, $check);
62+
}
63+
64+
return $filterFunc($result);
65+
}
66+
}

0 commit comments

Comments
 (0)