Merge pull request #13232 from JO0st/core-574
[civicrm-core.git] / CRM / Utils / Array.php
index 80b38b5eaeba9fac0cea09ecb70dc0d54279d800..1f1d1d6001b779bf91bbc49a2afa2f538a03776e 100644 (file)
@@ -1,27 +1,11 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 5                                                  |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
+ | Copyright CiviCRM LLC. All rights reserved.                        |
  |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
  +--------------------------------------------------------------------+
  */
 
@@ -29,7 +13,7 @@
  * Provides a collection of static methods for array manipulation.
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 class CRM_Utils_Array {
 
@@ -171,8 +155,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 +241,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 +284,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])
@@ -375,7 +359,7 @@ class CRM_Utils_Array {
   public static function crmInArray($value, $params, $caseInsensitive = TRUE) {
     foreach ($params as $item) {
       if (is_array($item)) {
-        $ret = crmInArray($value, $item, $caseInsensitive);
+        $ret = self::crmInArray($value, $item, $caseInsensitive);
       }
       else {
         $ret = ($caseInsensitive) ? strtolower($item) == strtolower($value) : $item == $value;
@@ -412,7 +396,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 +431,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 +506,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 +558,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 +569,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 +595,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 +628,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 +666,7 @@ class CRM_Utils_Array {
     }
     // Empty string -> empty array
     if ($values === '') {
-      return array();
+      return [];
     }
     return explode($delim, trim((string) $values, $delim));
   }
@@ -802,9 +786,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 +798,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 +834,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 +857,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 +873,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 +905,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 +925,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 +1006,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 +1047,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 +1111,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) {