|
| 1 | +# Util |
| 2 | + |
| 3 | +[](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. |
0 commit comments