class-arr.php
2 years ago
class-attachment.php
2 years ago
class-conditional.php
2 years ago
class-db.php
2 years ago
class-html.php
2 years ago
class-param.php
2 years ago
class-str.php
2 years ago
class-url.php
2 years ago
class-wordpress.php
2 years ago
index.php
2 years ago
class-arr.php
151 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Array helpers. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Helpers |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Helpers; |
| 12 | |
| 13 | use ArrayAccess; |
| 14 | |
| 15 | /** |
| 16 | * Arr class. |
| 17 | */ |
| 18 | class Arr { |
| 19 | |
| 20 | /** |
| 21 | * Determine whether the given value is array accessible. |
| 22 | * |
| 23 | * @param mixed $value Value to check. |
| 24 | * |
| 25 | * @return bool |
| 26 | */ |
| 27 | public static function accessible( $value ) { |
| 28 | return is_array( $value ) || $value instanceof ArrayAccess; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Determine if the given key exists in the provided array. |
| 33 | * |
| 34 | * @param ArrayAccess|array $array Array to check key in. |
| 35 | * @param string|int $key Key to check for. |
| 36 | * |
| 37 | * @return bool |
| 38 | */ |
| 39 | public static function exists( $array, $key ) { |
| 40 | if ( $array instanceof ArrayAccess ) { |
| 41 | // @codeCoverageIgnoreStart |
| 42 | return $array->offsetExists( $key ); |
| 43 | // @codeCoverageIgnoreEnd |
| 44 | } |
| 45 | |
| 46 | return array_key_exists( $key, $array ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Check whether an array or [[\Traversable]] contains an element. |
| 51 | * |
| 52 | * This method does the same as the PHP function [in_array()](https://secure.php.net/manual/en/function.in-array.php) |
| 53 | * but additionally works for objects that implement the [[\Traversable]] interface. |
| 54 | * |
| 55 | * @throws \InvalidArgumentException If `$array` is neither traversable nor an array. |
| 56 | * |
| 57 | * @param array|\Traversable $array The set of values to search. |
| 58 | * @param mixed $search The value to look for. |
| 59 | * @param bool $strict Whether to enable strict (`===`) comparison. |
| 60 | * |
| 61 | * @return bool `true` if `$search` was found in `$array`, `false` otherwise. |
| 62 | */ |
| 63 | public static function includes( $array, $search, $strict = true ) { |
| 64 | if ( $array instanceof \Traversable ) { |
| 65 | return self::includes_traversable( $array, $search, $strict ); |
| 66 | } |
| 67 | |
| 68 | $is_array = is_array( $array ); |
| 69 | if ( ! $is_array ) { |
| 70 | throw new \InvalidArgumentException( 'Argument $array must be an array or implement Traversable' ); |
| 71 | } |
| 72 | |
| 73 | return $is_array ? in_array( $search, $array, $strict ) : false; // phpcs:ignore |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Check Traversable contains an element. |
| 78 | * |
| 79 | * @param \Traversable $array The set of values to search. |
| 80 | * @param mixed $search The value to look for. |
| 81 | * @param bool $strict Whether to enable strict (`===`) comparison. |
| 82 | * |
| 83 | * @return bool `true` if `$search` was found in `$array`, `false` otherwise. |
| 84 | */ |
| 85 | private static function includes_traversable( $array, $search, $strict = true ) { |
| 86 | foreach ( $array as $value ) { |
| 87 | if ( ( $strict && $search === $value ) || $search == $value ) { // phpcs:ignore |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Insert a single array item inside another array at a set position |
| 97 | * |
| 98 | * @param array $array Array to modify. Is passed by reference, and no return is needed. |
| 99 | * @param array $new New array to insert. |
| 100 | * @param int $position Position in the main array to insert the new array. |
| 101 | */ |
| 102 | public static function insert( &$array, $new, $position ) { |
| 103 | $before = array_slice( $array, 0, $position - 1 ); |
| 104 | $after = array_diff_key( $array, $before ); |
| 105 | $array = array_merge( $before, $new, $after ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Push an item onto the beginning of an array. |
| 110 | * |
| 111 | * @param array $array Array to add. |
| 112 | * @param mixed $value Value to add. |
| 113 | * @param mixed $key Add with this key. |
| 114 | */ |
| 115 | public static function prepend( &$array, $value, $key = null ) { |
| 116 | if ( is_null( $key ) ) { |
| 117 | array_unshift( $array, $value ); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $array = [ $key => $value ] + $array; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update array add or delete value |
| 126 | * |
| 127 | * @param array $array Array to modify. Is passed by reference, and no return is needed. |
| 128 | * @param array $value Value to add or delete. |
| 129 | */ |
| 130 | public static function add_delete_value( &$array, $value ) { |
| 131 | if ( ( $key = array_search( $value, $array ) ) !== false ) { // @codingStandardsIgnoreLine |
| 132 | unset( $array[ $key ] ); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | $array[] = $value; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Create an array from string. |
| 141 | * |
| 142 | * @param string $string The string to split. |
| 143 | * @param string $separator Specifies where to break the string. |
| 144 | * |
| 145 | * @return array Returns an array after applying the function to each one. |
| 146 | */ |
| 147 | public static function from_string( $string, $separator = ',' ) { |
| 148 | return array_values( array_filter( array_map( 'trim', explode( $separator, $string ) ) ) ); |
| 149 | } |
| 150 | } |
| 151 |