Merge pull request #14036 from eileenmcnaughton/5.13
[civicrm-core.git] / CRM / Utils / Array.php
index dbcb100e16e37312797e31dc46b2296295e9ff2d..4cda076ea3fc531a289cc4c3276f2fc99f029856 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -29,7 +29,7 @@
  * Provides a collection of static methods for array manipulation.
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2018
+ * @copyright CiviCRM LLC (c) 2004-2019
  */
 class CRM_Utils_Array {
 
@@ -171,8 +171,8 @@ class CRM_Utils_Array {
     static $dst = NULL;
 
     if (!$src) {
-      $src = array('&', '<', '>', '\ 1');
-      $dst = array('&amp;', '&lt;', '&gt;', ',');
+      $src = ['&', '<', '>', '\ 1'];
+      $dst = ['&amp;', '&lt;', '&gt;', ','];
     }
 
     return str_replace($src, $dst, $value);
@@ -257,14 +257,14 @@ class CRM_Utils_Array {
    *   Array-encoded tree
    */
   public function unflatten($delim, &$arr) {
-    $result = array();
+    $result = [];
     foreach ($arr as $key => $value) {
       $path = explode($delim, $key);
       $node = &$result;
       while (count($path) > 1) {
         $key = array_shift($path);
         if (!isset($node[$key])) {
-          $node[$key] = array();
+          $node[$key] = [];
         }
         $node = &$node[$key];
       }
@@ -300,7 +300,7 @@ class CRM_Utils_Array {
       return $a1;
     }
 
-    $a3 = array();
+    $a3 = [];
     foreach ($a1 as $key => $value) {
       if (array_key_exists($key, $a2) &&
         is_array($a2[$key]) && is_array($a1[$key])
@@ -412,7 +412,7 @@ class CRM_Utils_Array {
     $look = $reverse ? array_flip($lookup) : $lookup;
 
     // trim lookup array, ignore . ( fix for CRM-1514 ), eg for prefix/suffix make sure Dr. and Dr both are valid
-    $newLook = array();
+    $newLook = [];
     foreach ($look as $k => $v) {
       $newLook[trim($k, ".")] = $v;
     }
@@ -447,7 +447,7 @@ class CRM_Utils_Array {
    * @return bool
    *   True if the array is empty.
    */
-  public static function crmIsEmptyArray($array = array()) {
+  public static function crmIsEmptyArray($array = []) {
     if (!is_array($array)) {
       return TRUE;
     }
@@ -522,7 +522,7 @@ class CRM_Utils_Array {
    * @return array
    *   Sorted array.
    */
-  public static function asort($array = array()) {
+  public static function asort($array = []) {
     $lcMessages = CRM_Utils_System::getUFLocale();
 
     if ($lcMessages && $lcMessages != 'en_US' && class_exists('Collator')) {
@@ -574,7 +574,7 @@ class CRM_Utils_Array {
   public static function index($keys, $records) {
     $final_key = array_pop($keys);
 
-    $result = array();
+    $result = [];
     foreach ($records as $record) {
       $node = &$result;
       foreach ($keys as $key) {
@@ -585,7 +585,7 @@ class CRM_Utils_Array {
           $keyvalue = isset($record->{$key}) ? $record->{$key} : NULL;
         }
         if (isset($node[$keyvalue]) && !is_array($node[$keyvalue])) {
-          $node[$keyvalue] = array();
+          $node[$keyvalue] = [];
         }
         $node = &$node[$keyvalue];
       }
@@ -611,7 +611,7 @@ class CRM_Utils_Array {
    *   Keys are the original keys of $records; values are the $prop values.
    */
   public static function collect($prop, $records) {
-    $result = array();
+    $result = [];
     if (is_array($records)) {
       foreach ($records as $key => $record) {
         if (is_object($record)) {
@@ -644,11 +644,11 @@ class CRM_Utils_Array {
    * @return array
    *   Keys are the original keys of $objects; values are the method results.
    */
-  public static function collectMethod($method, $objects, $args = array()) {
-    $result = array();
+  public static function collectMethod($method, $objects, $args = []) {
+    $result = [];
     if (is_array($objects)) {
       foreach ($objects as $key => $object) {
-        $result[$key] = call_user_func_array(array($object, $method), $args);
+        $result[$key] = call_user_func_array([$object, $method], $args);
       }
     }
     return $result;
@@ -682,7 +682,7 @@ class CRM_Utils_Array {
     }
     // Empty string -> empty array
     if ($values === '') {
-      return array();
+      return [];
     }
     return explode($delim, trim((string) $values, $delim));
   }
@@ -802,9 +802,9 @@ class CRM_Utils_Array {
    *   {fg => blue, bg => black}
    *   }
    */
-  public static function product($dimensions, $template = array()) {
+  public static function product($dimensions, $template = []) {
     if (empty($dimensions)) {
-      return array($template);
+      return [$template];
     }
 
     foreach ($dimensions as $key => $value) {
@@ -814,7 +814,7 @@ class CRM_Utils_Array {
     }
     unset($dimensions[$key]);
 
-    $results = array();
+    $results = [];
     foreach ($firstValues as $firstValue) {
       foreach (self::product($dimensions, $template) as $result) {
         $result[$firstKey] = $firstValue;
@@ -850,7 +850,7 @@ class CRM_Utils_Array {
    * @return array
    */
   public static function subset($array, $keys) {
-    $result = array();
+    $result = [];
     foreach ($keys as $key) {
       if (isset($array[$key])) {
         $result[$key] = $array[$key];
@@ -873,9 +873,9 @@ class CRM_Utils_Array {
    *   Ex: [0 => ['key' => 'foo', 'value' => 'bar']].
    */
   public static function makeNonAssociative($associative, $keyName = 'key', $valueName = 'value') {
-    $output = array();
+    $output = [];
     foreach ($associative as $key => $val) {
-      $output[] = array($keyName => $key, $valueName => $val);
+      $output[] = [$keyName => $key, $valueName => $val];
     }
     return $output;
   }
@@ -889,7 +889,7 @@ class CRM_Utils_Array {
    * @return array
    */
   public static function multiArrayDiff($array1, $array2) {
-    $arrayDiff = array();
+    $arrayDiff = [];
     foreach ($array1 as $mKey => $mValue) {
       if (array_key_exists($mKey, $array2)) {
         if (is_array($mValue)) {
@@ -921,9 +921,9 @@ class CRM_Utils_Array {
    * @return array
    */
   public static function filterColumns($matrix, $columns) {
-    $newRows = array();
+    $newRows = [];
     foreach ($matrix as $pos => $oldRow) {
-      $newRow = array();
+      $newRow = [];
       foreach ($columns as $column) {
         $newRow[$column] = CRM_Utils_Array::value($column, $oldRow);
       }
@@ -941,7 +941,7 @@ class CRM_Utils_Array {
    * @return array
    */
   public static function rekey($array, $indexBy) {
-    $result = array();
+    $result = [];
     foreach ($array as $key => $value) {
       $newKey = is_callable($indexBy) ? $indexBy($key, $value) : $value[$indexBy];
       $result[$newKey] = $value;
@@ -1022,7 +1022,7 @@ class CRM_Utils_Array {
     $last = array_pop($pathParts);
     foreach ($pathParts as $part) {
       if (!isset($r[$part])) {
-        $r[$part] = array();
+        $r[$part] = [];
       }
       $r = &$r[$part];
     }
@@ -1063,7 +1063,8 @@ class CRM_Utils_Array {
       (count($keys) == 1 &&
         (current($keys) > 1 ||
           is_string(current($keys)) ||
-          (current($keys) == 1 && $array[1] == 1) // handle (0 => 4), (1 => 1)
+          // handle (0 => 4), (1 => 1)
+          (current($keys) == 1 && $array[1] == 1)
         )
       )
     ) {
@@ -1126,7 +1127,7 @@ class CRM_Utils_Array {
    * @return array
    */
   public static function buildTree($elements, $parentId = NULL) {
-    $branch = array();
+    $branch = [];
 
     foreach ($elements as $element) {
       if ($element['parent_id'] == $parentId) {