greenlystapp /
basecommerce-php
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Greenlyst\BaseCommerce\Core; |
||
| 6 | |||
| 7 | use function ArrayHelpers\array_has; |
||
| 8 | use Greenlyst\BaseCommerce\LogicException; |
||
| 9 | |||
| 10 | class Helpers |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param $data |
||
| 14 | * |
||
| 15 | * @return array |
||
| 16 | */ |
||
| 17 | public static function clearArray($data) |
||
| 18 | { |
||
| 19 | return array_filter($data, function ($item) { |
||
| 20 | if ($item === null) { |
||
| 21 | return false; |
||
| 22 | } |
||
| 23 | |||
| 24 | return true; |
||
| 25 | }); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param $array |
||
| 30 | * @param $keys |
||
| 31 | * |
||
| 32 | * @throws LogicException |
||
| 33 | */ |
||
| 34 | public static function validateArray($array, $keys) |
||
| 35 | { |
||
| 36 | foreach ($keys as $key) { |
||
| 37 | if (array_has($array, $key) == false) { |
||
|
0 ignored issues
–
show
|
|||
| 38 | throw LogicException::requiredFieldDoesntExist($key); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get all of the given array except for a specified array of items. |
||
| 45 | * |
||
| 46 | * @param array $array |
||
| 47 | * @param array|string $keys |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public static function array_except($array, $keys) |
||
| 52 | { |
||
| 53 | return array_diff_key($array, array_flip((array) $keys)); |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function replaceArrayKeys($array, $search, $replace) |
||
| 57 | { |
||
| 58 | $newArray = []; |
||
| 59 | |||
| 60 | foreach ($array as $key => $item) { |
||
| 61 | $newArray[str_replace($search, $replace, $key)] = $item; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $newArray; |
||
| 65 | } |
||
| 66 | } |
||
| 67 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.