TYPO3 API  SVNRelease
Arrays.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * The array functions from the good old t3lib_div plus new code.
00030  *
00031  * @package Extbase
00032  * @subpackage Utility
00033  * @version $Id: Arrays.php 1992 2010-03-09 21:44:11Z jocrau $
00034  (robert) I'm not sure yet if we should use this library statically or as a singleton. The latter might be problematic if we use it from the Core classes.
00035  * @api
00036  */
00037 class Tx_Extbase_Utility_Arrays {
00038 
00039     /**
00040      * Explodes a $string delimited by $delimeter and passes each item in the array through intval().
00041      * Corresponds to explode(), but with conversion to integers for all values.
00042      *
00043      * @param string $delimiter Delimiter string to explode with
00044      * @param string $string The string to explode
00045      * @return array Exploded values, all converted to integers
00046      * @api
00047      */
00048     static public function integerExplode($delimiter, $string) {
00049         $chunksArr = explode($delimiter, $string);
00050         foreach ($chunksArr as $key => $value) {
00051             $chunks[$key] = intval($value);
00052         }
00053         return $chunks;
00054     }
00055 
00056     /**
00057      * Explodes a string and trims all values for whitespace in the ends.
00058      * If $onlyNonEmptyValues is set, then all blank ('') values are removed.
00059      *
00060      * @param string $delimiter Delimiter string to explode with
00061      * @param string $string The string to explode
00062      * @param boolean $onlyNonEmptyValues If set, all empty values (='') will NOT be set in output
00063      * @return array Exploded values
00064      * @api
00065      */
00066     static public function trimExplode($delimiter, $string, $onlyNonEmptyValues = FALSE) {
00067         $chunksArr = explode($delimiter, $string);
00068         $newChunksArr = array();
00069         foreach ($chunksArr as $value) {
00070             if ($onlyNonEmptyValues === FALSE || strcmp('', trim($value))) {
00071                 $newChunksArr[] = trim($value);
00072             }
00073         }
00074         reset($newChunksArr);
00075         return $newChunksArr;
00076     }
00077 
00078     /**
00079      * Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($firstArray) with the values of the second array ($secondArray)
00080      * In case of identical keys, ie. keeping the values of the second.
00081      *
00082      * @param array First array
00083      * @param array Second array, overruling the first array
00084      * @param boolean If set, keys that are NOT found in $firstArray (first array) will not be set. Thus only existing value can/will be overruled from second array.
00085      * @param boolean If set (which is the default), values from $secondArray will overrule if they are empty (according to PHP's empty() function)
00086      * @return array Resulting array where $secondArray values has overruled $firstArray values
00087      * @api
00088      */
00089     static public function arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys = FALSE, $emptyValuesOverride = TRUE) {
00090         foreach ($secondArray as $key => $value) {
00091             if (array_key_exists($key, $firstArray) && is_array($firstArray[$key])) {
00092                 if (is_array($secondArray[$key])) {
00093                     $firstArray[$key] = self::arrayMergeRecursiveOverrule($firstArray[$key], $secondArray[$key], $dontAddNewKeys, $emptyValuesOverride);
00094                 }
00095             } else {
00096                 if ($dontAddNewKeys) {
00097                     if (array_key_exists($key, $firstArray)) {
00098                         if ($emptyValuesOverride || !empty($value)) {
00099                             $firstArray[$key] = $value;
00100                         }
00101                     }
00102                 } else {
00103                     if ($emptyValuesOverride || !empty($value)) {
00104                         $firstArray[$key] = $value;
00105                     }
00106                 }
00107             }
00108         }
00109         reset($firstArray);
00110         return $firstArray;
00111     }
00112 
00113     /**
00114      * Randomizes the order of array values. The array should not be an associative array
00115      * as the key-value relations will be lost.
00116      *
00117      * @param array $array Array to reorder
00118      * @return array The array with randomly ordered values
00119      * @api
00120      */
00121     static public function randomizeArrayOrder(array $array) {
00122         $reorderedArray = array();
00123         if (count($array) > 1) {
00124             $keysInRandomOrder = array_rand($array, count($array));
00125             foreach ($keysInRandomOrder as $key) {
00126                 $reorderedArray[] = $array[$key];
00127             }
00128         } else {
00129             $reorderedArray = $array;
00130         }
00131         return $reorderedArray;
00132     }
00133 
00134     /**
00135      * Returns TRUE if the given array contains elements of varying types
00136      *
00137      * @param array $array
00138      * @return boolean
00139      * @api
00140      */
00141     static public function containsMultipleTypes(array $array) {
00142         if (count($array) > 0) {
00143             foreach ($array as $key => $value) {
00144                 if (!isset($previousType)) {
00145                     $previousType = gettype($value);
00146                 } else if ($previousType !== gettype($value)) {
00147                     return TRUE;
00148                 }
00149             }
00150         }
00151         return FALSE;
00152     }
00153 
00154     /**
00155      * Replacement for array_reduce that allows any type for $initial (instead
00156      * of only integer)
00157      *
00158      * @param array $array the array to reduce
00159      * @param string $function the reduce function with the same order of parameters as in the native array_reduce (i.e. accumulator first, then current array element)
00160      * @param mixed $initial the initial accumulator value
00161      * @return mixed
00162      * @api
00163      */
00164     static public function array_reduce(array $array, $function, $initial = NULL) {
00165         $accumlator = $initial;
00166         foreach($array as $value) {
00167             $accumlator = $function($accumlator, $value);
00168         }
00169         return $accumlator;
00170     }
00171 
00172     /**
00173      * Returns the value of a nested array by following the specifed path.
00174      *
00175      * @param array $array The array to traverse
00176      * @param array $path The path to follow, ie. a simple array of keys
00177      * @return mixed The value found, NULL if the path didn't exist
00178      * @api
00179      */
00180     static public function getValueByPath(array $array, array $path) {
00181         $key = array_shift($path);
00182         if (isset($array[$key])) {
00183             if (count($path) > 0) {
00184                 return (is_array($array[$key])) ? self::getValueByPath($array[$key], $path) : NULL;
00185             } else {
00186                 return $array[$key];
00187             }
00188         } else {
00189             return NULL;
00190         }
00191     }
00192 
00193     /**
00194      * Sorts multidimensional arrays by recursively calling ksort on its elements.
00195      *
00196      * @param array $array the array to sort
00197      * @param integer $sortFlags may be used to modify the sorting behavior using these values (see http://www.php.net/manual/en/function.sort.php)
00198      * @return boolean TRUE on success, FALSE on failure
00199      * @see asort()
00200      * @api
00201      */
00202     static public function sortKeysRecursively(array &$array, $sortFlags = NULL) {
00203         foreach($array as &$value) {
00204             if (is_array($value)) {
00205                 if (self::sortKeysRecursively($value, $sortFlags) === FALSE) {
00206                     return FALSE;
00207                 }
00208             }
00209         }
00210         return ksort($array, $sortFlags);
00211     }
00212 
00213 }
00214 ?>