ArrayUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2008 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 
00015     final class ArrayUtils extends StaticFactory
00016     {
00018         public static function regularizeList($ids, $objects)
00019         {
00020             if (!$objects)
00021                 return array();
00022             
00023             $result = array();
00024             
00025             $objects = self::convertObjectList($objects);
00026             
00027             foreach ($ids as $id)
00028                 if (isset($objects[$id]))
00029                     $result[] = $objects[$id];
00030             
00031             return $result;
00032         }
00033         
00034         public static function convertObjectList($list = null)
00035         {
00036             $out = array();
00037             
00038             if (!$list)
00039                 return $out;
00040             
00041             foreach ($list as $obj)
00042                 $out[$obj->getId()] = $obj;
00043             
00044             return $out;
00045         }
00046         
00047         public static function getIdsArray($objectsList)
00048         {
00049             $out = array();
00050             
00051             if (!$objectsList)
00052                 return $out;
00053             
00054             Assert::isInstance(
00055                 current($objectsList), 'Identifiable',
00056                 'only identifiable lists accepted'
00057             );
00058             
00059             foreach ($objectsList as $object)
00060                 $out[] = $object->getId();
00061             
00062             return $out;
00063         }
00064         
00065         public static function &convertToPlainList($list, $key)
00066         {
00067             $out = array();
00068             
00069             foreach ($list as $obj)
00070                 $out[] = $obj[$key];
00071             
00072             return $out;
00073         }
00074         
00075         public static function getArrayVar(&$array, $var)
00076         {
00077             if (isset($array[$var]) && !empty($array[$var])) {
00078                 $out = &$array[$var];
00079                 return $out;
00080             }
00081             
00082             return null;
00083         }
00084         
00085         public static function columnFromSet($column, $array)
00086         {
00087             Assert::isArray($array);
00088             $result = array();
00089             
00090             foreach ($array as $row)
00091                 if (isset($row[$column]))
00092                     $result[] = $row[$column];
00093             
00094             return $result;
00095         }
00096         
00097         public static function mergeUnique(/* ... */)
00098         {
00099             $arguments = func_get_args();
00100             
00101             Assert::isArray(reset($arguments));
00102             
00103             return array_unique(
00104                 call_user_func_array(
00105                     'array_merge',
00106                     $arguments
00107                 )
00108             );
00109         }
00110         
00111         public static function countNonemptyValues($array)
00112         {
00113             Assert::isArray($array);
00114             $result = 0;
00115             
00116             foreach ($array as $value)
00117                 if (!empty($value))
00118                     ++$result;
00119             
00120             return $result;
00121         }
00122         
00123         public static function isEmpty(array $array)
00124         {
00125             foreach ($array as $key => $value)
00126                 if ($value !== null)
00127                     return false;
00128             
00129             return true;
00130         }
00131         
00136         public static function flatToDimensional($array)
00137         {
00138             if (!$array)
00139                 return null;
00140             
00141             Assert::isArray($array);
00142             
00143             $first = array_shift($array);
00144             
00145             if (!$array)
00146                 return $first;
00147             
00148             return array($first => self::flatToDimensional($array));
00149         }
00150         
00151         public static function mergeRecursiveUnique($one, $two)
00152         {
00153             if (!$one)
00154                 return $two;
00155             
00156             Assert::isArray($one);
00157             Assert::isArray($two);
00158             
00159             $result = $one;
00160             
00161             foreach ($two as $key => $value) {
00162                 
00163                 if (is_integer($key)) {
00164                     $result[] = $value;
00165                 } elseif (
00166                     isset($one[$key])
00167                     && is_array($one[$key])
00168                     && is_array($value)
00169                 ) {
00170                     $result[$key] = self::mergeRecursiveUnique($one[$key], $value);
00171                 } else {
00172                     $result[$key] = $value;
00173                 }
00174             }
00175             
00176             return $result;
00177         }
00178         
00182         public static function getMirrorValues($array)
00183         {
00184             Assert::isArray($array);
00185             
00186             $result = array();
00187             
00188             foreach ($array as $value) {
00189                 Assert::isTrue(
00190                     is_integer($value) || is_string($value),
00191                     'only integer or string values accepted'
00192                 );
00193                 
00194                 $result[$value] = $value;
00195             }
00196             
00197             return $result;
00198         }
00199 
00200         // TODO: drop Reflection
00201         public static function mergeSortedLists(
00202             $list1,
00203             $list2,
00204             Comparator $comparator,
00205             $compareValueGetter = null,
00206             $limit = null
00207         )
00208         {
00209             $list1Size = count($list1);
00210             $list2Size = count($list2);
00211 
00212             $i = $j = $k = 0;
00213 
00214             $newList = array();
00215 
00216             while ($i < $list1Size && $j < $list2Size) {
00217                 if (
00218                     $limit
00219                     && $k == $limit
00220                 )
00221                     return $newList;
00222 
00223                 if (!$compareValueGetter)
00224                     $compareResult = $comparator->compare(
00225                         $list1[$i], $list2[$j]
00226                     );
00227                 else
00228                     $compareResult = $comparator->compare(
00229                         $list1[$i]->{$compareValueGetter}(),
00230                         $list2[$j]->{$compareValueGetter}()
00231                     );
00232                 
00233                 // list1 elt < list2 elt
00234                 if ($compareResult < 0)
00235                     $newList[$k++] = $list2[$j++];
00236                 else
00237                     $newList[$k++] = $list1[$i++];
00238             }
00239 
00240             while ($i < $list1Size) {
00241                 if (
00242                     $limit
00243                     && $k == $limit
00244                 )
00245                     return $newList;
00246                 
00247                 $newList[$k++] = $list1[$i++];
00248             }
00249 
00250             while ($j < $list2Size) {
00251                 if (
00252                     $limit
00253                     && $k == $limit
00254                 )
00255                     return $newList;
00256                 
00257                 $newList[$k++] = $list2[$j++];
00258             }
00259 
00260             return $newList;
00261         }
00262     }
00263 ?>