| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Helpers; |
| 4 |
|
| 5 |
class ArrayDataSet |
| 6 |
{ |
| 7 |
/** |
| 8 |
* This function will return array with renamed keys. |
| 9 |
* |
| 10 |
* This function only support one dimensional array. |
| 11 |
* You can pass a multi dimensional array but only zero level array keys will be renamed. |
| 12 |
* |
| 13 |
* @since 2.7.0 |
| 14 |
* @since 4.1.0 Add is_array check |
| 15 |
* |
| 16 |
* @param array $renameTo Pass array of existing key name as key and new key name as value. |
| 17 |
* |
| 18 |
* @param array $array |
| 19 |
* |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
public static function renameKeys($array, $renameTo) |
| 23 |
{ |
| 24 |
if ( ! is_array($array)) { |
| 25 |
return []; |
| 26 |
} |
| 27 |
|
| 28 |
// Rename key if property name exist for them. |
| 29 |
foreach ($renameTo as $oldKey => $newKey) { |
| 30 |
if (array_key_exists($oldKey, $array)) { |
| 31 |
$array[$newKey] = $array[$oldKey]; |
| 32 |
unset($array[$oldKey]); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return $array; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Return whether or not array contains required keys. |
| 41 |
* |
| 42 |
* This function only support one dimensional array. |
| 43 |
* |
| 44 |
* @since 2.7.0 |
| 45 |
* |
| 46 |
* @param array $array |
| 47 |
* @param array $requiredKeys Array of required keys. |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
public static function hasRequiredKeys($array, $requiredKeys) |
| 52 |
{ |
| 53 |
return (bool)array_intersect_key($array, array_flip($requiredKeys)); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Return array with grouped under specific key. |
| 58 |
* |
| 59 |
* @param array $array |
| 60 |
* @param array $itemsToMove |
| 61 |
* @param string $arrayKey |
| 62 |
* |
| 63 |
* @return mixed |
| 64 |
*/ |
| 65 |
public static function moveArrayItemsUnderArrayKey($array, $itemsToMove, $arrayKey) |
| 66 |
{ |
| 67 |
foreach ($itemsToMove as $key) { |
| 68 |
if (array_key_exists($key, $array)) { |
| 69 |
$array[$arrayKey][$key] = $array[$key]; |
| 70 |
unset($array[$key]); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $array; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Creates a new array from the old one with all of the index keys converted to camel case. |
| 79 |
* This is only intended for associative arrays. |
| 80 |
* |
| 81 |
* @since 2.8.0 |
| 82 |
* |
| 83 |
* @param $array |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public static function camelCaseKeys($array) |
| 88 |
{ |
| 89 |
$newArray = []; |
| 90 |
|
| 91 |
foreach ($array as $key => $value) { |
| 92 |
$studlyKey = ucwords(str_replace(['-', '_'], ' ', $key)); |
| 93 |
$studlyKey = lcfirst(str_replace(' ', '', $studlyKey)); |
| 94 |
|
| 95 |
$newArray[$studlyKey] = $value; |
| 96 |
} |
| 97 |
|
| 98 |
return $newArray; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Creates a comma separated string and each value enclosed with single quote from array. |
| 103 |
* |
| 104 |
* @since 2.10.0 |
| 105 |
* |
| 106 |
* @param array $array |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
public static function getStringSeparatedByCommaEnclosedWithSingleQuote($array) |
| 111 |
{ |
| 112 |
return sprintf( |
| 113 |
'\'%s\'', |
| 114 |
implode('\',\'', $array) |
| 115 |
); |
| 116 |
} |
| 117 |
} |
| 118 |
|