Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ArrayUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class ArrayUtil
41  {
45  public static function getArrayValue($array, $element, $defaultValue = null)
46  {
47  if (isset($array[$element]))
48  {
49  return $array[$element];
50  }
51  return $defaultValue;
52  }
53 
57  public static function getArrayValueAndResolveNullAsZero($array, $element)
58  {
59  if (isset($array[$element]))
60  {
61  return $array[$element];
62  }
63  return 0;
64  }
65 
72  public static function getArrayValueWithExceptionIfNotFound($array, $element)
73  {
74  if (!array_key_exists($element, $array))
75  {
76  throw new NotFoundException($element . " does not exist.");
77  }
78  else
79  {
80  return static::getArrayValue($array, $element);
81  }
82  }
83 
84  public static function resolveArrayToLowerCase($array)
85  {
86  return unserialize(TextUtil::strToLowerWithDefaultEncoding(serialize($array)));
87  }
88 
93  public static function array_iunique($array)
94  {
95  return array_intersect_key($array, array_unique(array_map('strtolower', $array)));
96  }
97 
102  public static function stringify($data)
103  {
104  assert('is_array($data)');
105  $s = null;
106  foreach ($data as $value)
107  {
108  if ($s != null)
109  {
110  $s .= ', ';
111  }
112  $s .= $value;
113  }
114  return $s;
115  }
116 
120  public static function flatten($array, $preserveKeys = false)
121  {
122  //TODO: @Sergio: Add test for the preserveKeys
123  assert('is_bool($preserveKeys)');
124  $flatternArray = array();
125  foreach ($array as $key => $element)
126  {
127  if (is_array($element))
128  {
129  $flatternArray = array_merge($flatternArray, self::flatten($element, $preserveKeys));
130  }
131  else
132  {
133  if ($preserveKeys)
134  {
135  $flatternArray[$key] = $element;
136  }
137  else
138  {
139  $flatternArray[] = $element;
140  }
141  }
142  }
143  return $flatternArray;
144  }
145 
153  public static function subValueSort($array, $subKey, $sortFunctionName)
154  {
155  assert('$sortFunctionName == "sort" || $sortFunctionName == "asort"');
156  $newArray = array();
157  foreach ($array as $key => $value)
158  {
159  $newArray[$key] = strtolower($value[$subKey]);
160  }
161  if (!empty($newArray))
162  {
163  $sortFunctionName($newArray);
164  $finalArray = array();
165  foreach ($newArray as $newKey => $unused)
166  {
167  $finalArray[$newKey] = $array[$newKey];
168  }
169  return $finalArray;
170  }
171  return array();
172  }
173 
174  public static function arrayUniqueRecursive($array)
175  {
176  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
177 
178  foreach ($result as $key => $value)
179  {
180  if (is_array($value))
181  {
182  $result[$key] = ArrayUtil::arrayUniqueRecursive($value);
183  }
184  }
185  return $result;
186  }
187 
188  public static function isValidArrayIndex($index, $array)
189  {
190  return (isset($array[$index]) || array_key_exists($index, $array));
191  }
192 
193  public static function isArrayNotUnique($a)
194  {
195  return (count($a) === count(static::arrayUniqueRecursive($a)));
196  }
197 
198  public static function chopArray(array & $array, $size)
199  {
200  $chop = array_slice($array, 0, $size);
201  $array = array_slice($array, $size);
202  return $chop;
203  }
204 
205  public static function encodeAutoCompleteOptionsArray(array $options)
206  {
207  return base64_encode(serialize($options));
208  }
209 
210  public static function decodeAutoCompleteOptionsArray($options)
211  {
212  if (!isset($options))
213  {
214  return array();
215  }
216  $decodedSerializedOptions = base64_decode($options);
217  if ($decodedSerializedOptions === false)
218  {
219  return array();
220  }
221  $decodedUnserializedOptions = unserialize($decodedSerializedOptions);
222  if ($decodedUnserializedOptions === false)
223  {
224  return array();
225  }
226  return $decodedUnserializedOptions;
227  }
228 
234  public static function isAssoc(array $array)
235  {
236  return (bool)count(array_filter(array_keys($array), 'is_string'));
237  }
238 
246  public static function sortArrayByElementField($compareFunction, $sortFunctionName, & $data, $className)
247  {
248  assert('$sortFunctionName == "usort"');
249  assert('is_string($compareFunction)');
250  assert('is_string($className)');
251  assert('is_array($data)');
252  $sortFunctionName($data, array($className, $compareFunction));
253  }
254 
260  public static function findLastKey(array & $array)
261  {
262  end($array);
263  $lastKey = key($array);
264  // reset point back to start for any code that follows.
265  reset($array);
266  return $lastKey;
267  }
268 
275  public static function getNestedValue(array $array, $key)
276  {
277  $key = str_replace(array("]", "'", '"'), '', $key);
278  $pieces = explode('[', $key);
279  foreach ($pieces as $piece)
280  {
281  if (!is_array($array) || !array_key_exists($piece, $array))
282  {
283  return null;
284  }
285  $array = $array[$piece];
286  }
287  return $array;
288  }
289 
295  public static function recursivelyRemoveEmptyValues(array $haystack)
296  {
297  foreach ($haystack as $key => $value)
298  {
299  if (is_array($value))
300  {
301  $haystack[$key] = static::recursivelyRemoveEmptyValues($haystack[$key]);
302  }
303 
304  if (empty($haystack[$key]))
305  {
306  unset($haystack[$key]);
307  }
308  }
309  return $haystack;
310  }
311 
318  public static function setToDefaultValueIfMissing(array & $array, $key, $defaultValue = false)
319  {
320  if (!isset($array[$key]))
321  {
322  $array[$key] = $defaultValue;
323  }
324  }
325 
332  public static function arrayHasKeys(array $array, array $keys)
333  {
334  return count(array_intersect($keys, array_keys($array))) == count($keys);
335  }
336  }
337 ?>
static recursivelyRemoveEmptyValues(array $haystack)
Definition: ArrayUtil.php:295
static sortArrayByElementField($compareFunction, $sortFunctionName, &$data, $className)
Definition: ArrayUtil.php:246
static setToDefaultValueIfMissing(array &$array, $key, $defaultValue=false)
Definition: ArrayUtil.php:318
static findLastKey(array &$array)
Definition: ArrayUtil.php:260
static flatten($array, $preserveKeys=false)
Definition: ArrayUtil.php:120
static isAssoc(array $array)
Definition: ArrayUtil.php:234
static array_iunique($array)
Definition: ArrayUtil.php:93
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static stringify($data)
Definition: ArrayUtil.php:102
static subValueSort($array, $subKey, $sortFunctionName)
Definition: ArrayUtil.php:153
static getArrayValueAndResolveNullAsZero($array, $element)
Definition: ArrayUtil.php:57
static getNestedValue(array $array, $key)
Definition: ArrayUtil.php:275
static arrayHasKeys(array $array, array $keys)
Definition: ArrayUtil.php:332
static strToLowerWithDefaultEncoding($string)
Definition: TextUtil.php:47
static getArrayValueWithExceptionIfNotFound($array, $element)
Definition: ArrayUtil.php:72
Generated on Wed Dec 2 2020 07:10:24
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.