wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.ArrayUtil.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.ArrayUtil.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * @class ArrayUtil
22  * @ingroup Util
23  * @brief ArrayUtil provides support for array manipulation.
24  *
25  * @author ingo herwig <ingo@wemove.com>
26  */
27 class ArrayUtil
28 {
29  /*
30  * Implementation of in_array case insensitive search (see PHP documentation for parameters)
31  */
32  function in_array_i($str, $array)
33  {
34  return (sizeof(ArrayUtil::get_matching_values_i($str, $array)) > 0);
35  }
36  /*
37  * Get array values that match a string case insensitive.
38  * @param str String to match
39  * @param array Array to search in
40  * @return Array of matched values
41  */
42  function get_matching_values_i($str, $array)
43  {
44  return preg_grep('/^' . preg_quote($str, '/') . '$/i', $array);
45  }
46  /*
47  * Implementation of array_slice for assoziative arrays (see PHP documentation for parameters)
48  */
49  function key_array_splice(&$input, $key_ofs, $length=null, $replacement=null)
50  {
51  // Adjust the length if it was negative or not passed
52  if ($length === null || $length < 0)
53  $count = $length + count($input);
54 
55  // Cycle through the array
56  foreach ($input as $key => $value)
57  {
58  if (!$key_found)
59  {
60  if ($key === $key_ofs)
61  {
62  $key_found = true;
63  if ($length !== null && $length >= 0)
64  $count = $length;
65  if (is_array($replacement))
66  foreach($replacement as $r_key => $r_value)
67  $new_array[$r_key] = $r_value;
68  }
69  else
70  $new_array[$key] = $value;
71  }
72  if ($key_found)
73  {
74  if ($count > 0)
75  $ret_array[$key] = $value;
76  else
77  $new_array[$key] = $value;
78  }
79  $count--;
80  }
81 
82  // Finish up
83  $input = $new_array;
84  return $ret_array;
85  }
86 
87  /*
88  * Rename a key in an assoziative array
89  * @param input A reference to the array to rename the key in
90  * @param oldname The old name of the key
91  * @param newname The new name of the key
92  */
93  function key_array_rename(&$input, $oldname, $newname)
94  {
95  ArrayUtil::key_array_splice($input, $oldname, 1,
96  array($newname => $input[$oldname]));
97  unset($input[$oldname]);
98  }
99 
100  /*
101  * Insert a value into an array at arbitrary position
102  * @param input A reference to the array to insert to
103  * @param pos The position (0 < pos < sizeof(input))
104  * @param val A reference to the value to insert
105  * @param unique True/False indicating wether the value should be ignored if it
106  * already exists or not.
107  * @return True/False indicating wether the value was inserted or not
108  */
109  function array_insert(&$input, $pos, &$val, $unique=true)
110  {
111  if (!is_array($input))
112  $input = array();
113  if ($unique && in_array($val, $input, true))
114  return false;
115  array_splice($input, $pos, 0, array(&$val));
116  return true;
117  }
118 
119  /*
120  * Remove a value from an array
121  * @param input A reference to the array to remove from
122  * @param val A reference to the value to remove
123  */
124  function array_remove(&$input, &$val)
125  {
126  $input = array_diff($input, array($val));
127  }
128 
129  /**
130  * Recursivly walk an array
131  * @see http://de.php.net/manual/en/function.array-walk-recursive.php
132  */
133  function array_walk_recursive(&$input, $funcname, $userdata = "")
134  {
135  return array_walk_recursive($input, $funcname, $userdata);
136  }
137 
138  /**
139  * Object Id array helper methods
140  */
141 
142  /*
143  * Remove duplicated entries from an array of objects
144  * @param objArray The array of objects
145  * @return An array of objects made unique by oid
146  */
147  function array_unique_by_oid($objArray)
148  {
149  $out = array();
150  $oidList = array();
151  for($i=0; $i<sizeof($objArray); $i++)
152  {
153  $curObj = &$objArray[$i];
154  if(!in_array($curObj->getOID(), $oidList, true))
155  {
156  $oidList[sizeof($oidList)] = $curObj->getOID();
157  $out[sizeof($out)] = &$curObj;
158  }
159  }
160  return $out;
161  }
162 
163  /**
164  * Get the previous and the next object id from an object list,
165  * based on the given oid. If wrap is false, null is returned
166  * for prevOID if oid is at the beginning of the list and for
167  * the end respectively. The list is wrapped at beginning and
168  * end, if wrap is true.
169  * @param oid The marker object id
170  * @param objList A reference to the object list
171  * @param wrap True/False wether to wrap the list or not
172  * @return An Array with two the previous and the next oid
173  */
174  function getPrevNextOIDs($oid, &$objList, $wrap)
175  {
176  $prevOID = null;
177  $nextOID = null;
178  $listSize = sizeof($objList);
179 
180  if ($listSize > 0)
181  {
182  for ($i=0; $i<$listSize; $i++)
183  {
184  if ($objList[$i]->getOID() == $oid)
185  {
186  // determine previous
187  if ($i == 0)
188  {
189  if ($wrap)
190  $prevOID = $objList[$listSize-1]->getOID();
191  else
192  $prevOID = null;
193  }
194  else
195  $prevOID = $objList[$i-1]->getOID();
196 
197  // determine next
198  if ($i == $listSize-1)
199  {
200  if ($wrap)
201  $nextOID = $objList[0]->getOID();
202  else
203  $nextOID = null;
204  }
205  else
206  $nextOID = $objList[$i+1]->getOID();
207  }
208  }
209  }
210  return array($prevOID, $nextOID);
211  }
212 }
213 ?>
key_array_splice(&$input, $key_ofs, $length=null, $replacement=null)
getPrevNextOIDs($oid, &$objList, $wrap)
array_remove(&$input, &$val)
in_array_i($str, $array)
get_matching_values_i($str, $array)
array_walk_recursive(&$input, $funcname, $userdata="")
array_insert(&$input, $pos, &$val, $unique=true)
key_array_rename(&$input, $oldname, $newname)
ArrayUtil provides support for array manipulation.
array_unique_by_oid($objArray)