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