ArrayUtils.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 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 /* $Id: ArrayUtils.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class ArrayUtils extends StaticFactory
00017     {
00018         public static function convertObjectList($list = null)
00019         {
00020             $out = array();
00021 
00022             if (!$list)
00023                 return $out;
00024                 
00025             foreach ($list as $obj)
00026                 $out[$obj->getId()] = $obj;
00027 
00028             return $out;
00029         }
00030         
00031         public static function getIdsArray($objectsList)
00032         {
00033             $out = array();
00034             
00035             if (!$objectsList)
00036                 return $out;
00037             
00038             Assert::isTrue(
00039                 current($objectsList) instanceof Identifiable,
00040                 'only identifiable lists accepted'
00041             );
00042             
00043             foreach ($objectsList as $object)
00044                 $out[] = $object->getId();
00045 
00046             return $out;
00047         }
00048         
00049         public static function &convertToPlainList(&$list, $key)
00050         {
00051             $out = array();
00052             
00053             foreach ($list as $obj)
00054                 $out[] = $obj[$key];
00055 
00056             return $out;
00057         }
00058         
00059         public static function getArrayVar(&$array, $var)
00060         {
00061             if (isset($array[$var]) && !empty($array[$var])) {
00062                 $out = &$array[$var];
00063                 return $out;
00064             }
00065 
00066             return null;
00067         }
00068         
00069         public static function columnFromSet($column, &$array)
00070         {
00071             Assert::isArray($array);
00072             $result = array();
00073             
00074             foreach ($array as $row)
00075                 if (isset($row[$column]))
00076                     $result[] = $row[$column];
00077             
00078             return $result;
00079         }
00080         
00081         public static function mergeUnique(/* ... */)
00082         {
00083             $arguments = func_get_args();
00084             
00085             Assert::isArray(reset($arguments));
00086             
00087             return array_unique(
00088                 call_user_func_array(
00089                     'array_merge',
00090                     $arguments
00091                 )
00092             );
00093         }
00094         
00095         public static function countNonemptyValues($array)
00096         {
00097             Assert::isArray($array);
00098             $result = 0;
00099             
00100             foreach ($array as $value)
00101                 if (!empty($value))
00102                     ++$result;
00103             
00104             return $result;
00105         }
00106         
00110         public static function getMirrorValues($array)
00111         {
00112             Assert::isArray($array);
00113 
00114             $result = array();
00115 
00116             foreach ($array as $value) {
00117                 Assert::isTrue(
00118                     is_integer($value) || is_string($value),
00119                     'only integer or string values accepted'
00120                 );
00121 
00122                 $result[$value] = $value;
00123             }
00124 
00125             return $result;
00126         }
00127     }
00128 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4