From 3b35e8b91ce94c292b46a296d034542ac5f0f6da Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 15 Dec 2008 08:56:18 +0000 Subject: Refresh kohana from upstream svn trunk r3771. During this process, remove a considerable number of files from kohana that we will not be needing in Gallery3, including the following files and directories: kohana/application kohana/example.htaccess kohana/index.php kohana/install.php kohana/kohana.png kohana/modules/archive kohana/modules/auth kohana/modules/flot kohana/modules/gmaps kohana/modules/kodoc kohana/modules/payment kohana/modules/smarty kohana/modules/unit_test/i18n kohana/modules/unit_test/tests/Example_Test.php kohana/modules/unit_test/tests/Valid_Test.php kohana/system/config/captcha.php kohana/system/controllers/captcha.php kohana/system/fonts kohana/system/i18n kohana/system/libraries/Calendar.php kohana/system/libraries/Calendar_Event.php kohana/system/libraries/Captcha.php kohana/system/libraries/Tagcloud.php kohana/system/vendor kohana/system/views/pagination kohana/system/views/kohana_calendar.php --- kohana/helpers/arr.php | 316 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 kohana/helpers/arr.php (limited to 'kohana/helpers/arr.php') diff --git a/kohana/helpers/arr.php b/kohana/helpers/arr.php new file mode 100644 index 00000000..24b03d7f --- /dev/null +++ b/kohana/helpers/arr.php @@ -0,0 +1,316 @@ + $value) + { + $value = ($keep_keys === TRUE) ? $value : array_values($value); + foreach ($value as $k => $v) + { + $new_array[$k][$key] = $v; + } + } + + return $new_array; + } + + /** + * Removes a key from an array and returns the value. + * + * @param string key to return + * @param array array to work on + * @return mixed value of the requested array key + */ + public static function remove($key, & $array) + { + if ( ! array_key_exists($key, $array)) + return NULL; + + $val = $array[$key]; + unset($array[$key]); + + return $val; + } + + + /** + * Extract one or more keys from an array. Each key given after the first + * argument (the array) will be extracted. Keys that do not exist in the + * search array will be NULL in the extracted data. + * + * @param array array to search + * @param string key name + * @return array + */ + public static function extract(array $search, $keys) + { + // Get the keys, removing the $search array + $keys = array_slice(func_get_args(), 1); + + $found = array(); + foreach ($keys as $key) + { + if (isset($search[$key])) + { + $found[$key] = $search[$key]; + } + else + { + $found[$key] = NULL; + } + } + + return $found; + } + + /** + * Because PHP does not have this function. + * + * @param array array to unshift + * @param string key to unshift + * @param mixed value to unshift + * @return array + */ + public static function unshift_assoc( array & $array, $key, $val) + { + $array = array_reverse($array, TRUE); + $array[$key] = $val; + $array = array_reverse($array, TRUE); + + return $array; + } + + /** + * Because PHP does not have this function, and array_walk_recursive creates + * references in arrays and is not truly recursive. + * + * @param mixed callback to apply to each member of the array + * @param array array to map to + * @return array + */ + public static function map_recursive($callback, array $array) + { + foreach ($array as $key => $val) + { + // Map the callback to the key + $array[$key] = is_array($val) ? arr::map_recursive($callback, $val) : call_user_func($callback, $val); + } + + return $array; + } + + /** + * Binary search algorithm. + * + * @param mixed the value to search for + * @param array an array of values to search in + * @param boolean return false, or the nearest value + * @param mixed sort the array before searching it + * @return integer + */ + public static function binary_search($needle, $haystack, $nearest = FALSE, $sort = FALSE) + { + if ($sort === TRUE) + { + sort($haystack); + } + + $high = count($haystack); + $low = 0; + + while ($high - $low > 1) + { + $probe = ($high + $low) / 2; + if ($haystack[$probe] < $needle) + { + $low = $probe; + } + else + { + $high = $probe; + } + } + + if ($high == count($haystack) OR $haystack[$high] != $needle) + { + if ($nearest === FALSE) + return FALSE; + + // return the nearest value + $high_distance = $haystack[ceil($low)] - $needle; + $low_distance = $needle - $haystack[floor($low)]; + + return ($high_distance >= $low_distance) ? $haystack[ceil($low)] : $haystack[floor($low)]; + } + + return $high; + } + + /** + * Emulates array_merge_recursive, but appends numeric keys and replaces + * associative keys, instead of appending all keys. + * + * @param array any number of arrays + * @return array + */ + public static function merge() + { + $total = func_num_args(); + + $result = array(); + for ($i = 0; $i < $total; $i++) + { + foreach (func_get_arg($i) as $key => $val) + { + if (isset($result[$key])) + { + if (is_array($val)) + { + // Arrays are merged recursively + $result[$key] = arr::merge($result[$key], $val); + } + elseif (is_int($key)) + { + // Indexed arrays are appended + array_push($result, $val); + } + else + { + // Associative arrays are replaced + $result[$key] = $val; + } + } + else + { + // New values are added + $result[$key] = $val; + } + } + } + + return $result; + } + + /** + * Overwrites an array with values from input array(s). + * Non-existing keys will not be appended! + * + * @param array key array + * @param array input array(s) that will overwrite key array values + * @return array + */ + public static function overwrite($array1) + { + foreach (array_slice(func_get_args(), 1) as $array2) + { + foreach ($array2 as $key => $value) + { + if (array_key_exists($key, $array1)) + { + $array1[$key] = $value; + } + } + } + + return $array1; + } + + /** + * Fill an array with a range of numbers. + * + * @param integer stepping + * @param integer ending number + * @return array + */ + public static function range($step = 10, $max = 100) + { + if ($step < 1) + return array(); + + $array = array(); + for ($i = $step; $i <= $max; $i += $step) + { + $array[$i] = $i; + } + + return $array; + } + + /** + * Recursively convert an array to an object. + * + * @param array array to convert + * @return object + */ + public static function to_object(array $array, $class = 'stdClass') + { + $object = new $class; + + foreach ($array as $key => $value) + { + if (is_array($value)) + { + // Convert the array to an object + $value = arr::to_object($value, $class); + } + + // Add the value to the object + $object->{$key} = $value; + } + + return $object; + } + +} // End arr \ No newline at end of file -- cgit v1.2.3