| @@ -1,117 +1,106 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Give\Helpers; |
| 4 | 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 | - } | |
| 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 | + } | |
| 27 | 28 | |
| 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 | - } | |
| 29 | + return $array; | |
| 30 | + } | |
| 35 | 31 | |
| 36 | - return $array; | |
| 37 | - } | |
| 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 | + } | |
| 38 | 47 | |
| 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 | - } | |
| 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 | + } | |
| 55 | 64 | |
| 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 | - } | |
| 65 | + return $array; | |
| 66 | + } | |
| 73 | 67 | |
| 74 | - return $array; | |
| 75 | - } | |
| 76 | 68 | |
| 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 = []; | |
| 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 = []; | |
| 90 | 81 | |
| 91 | - foreach ($array as $key => $value) { | |
| 92 | - $studlyKey = ucwords(str_replace(['-', '_'], ' ', $key)); | |
| 93 | - $studlyKey = lcfirst(str_replace(' ', '', $studlyKey)); | |
| 82 | + foreach ( $array as $key => $value ) { | |
| 83 | + $studlyKey = ucwords( str_replace( [ '-', '_' ], ' ', $key ) ); | |
| 84 | + $studlyKey = lcfirst( str_replace( ' ', '', $studlyKey ) ); | |
| 94 | 85 | |
| 95 | - $newArray[$studlyKey] = $value; | |
| 96 | - } | |
| 86 | + $newArray[ $studlyKey ] = $value; | |
| 87 | + } | |
| 97 | 88 | |
| 98 | - return $newArray; | |
| 99 | - } | |
| 89 | + return $newArray; | |
| 90 | + } | |
| 100 | 91 | |
| 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 | - } | |
| 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 | + } | |
| 117 | 106 | } |