Support PropertyBag in CRM_Utils_Array
authoreileen <emcnaughton@wikimedia.org>
Fri, 6 Mar 2020 09:29:37 +0000 (22:29 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 10 Mar 2020 09:27:06 +0000 (22:27 +1300)
We now use the PropertyBag in payment processors - but as @mattwire discovered the CRM_Utils_Array::value
function is commonly used to access values now potentially 'in the bag' and the 'is_array'
filtering here means it is not being returned

CRM/Utils/Array.php
tests/phpunit/Civi/Payment/PropertyBagTest.php

index 1f1d1d6001b779bf91bbc49a2afa2f538a03776e..5c6b1ac9230b12699b0c29f907b8cf0137ba9089 100644 (file)
@@ -21,12 +21,12 @@ class CRM_Utils_Array {
    * Returns $list[$key] if such element exists, or a default value otherwise.
    *
    * If $list is not actually an array at all, then the default value is
-   * returned.
+   * returned. We hope to deprecate this behaviour.
    *
    *
    * @param string $key
    *   Key value to look up in the array.
-   * @param array $list
+   * @param array|ArrayAccess $list
    *   Array from which to look up a value.
    * @param mixed $default
    *   (optional) Value to return $list[$key] does not exist.
@@ -38,6 +38,11 @@ class CRM_Utils_Array {
     if (is_array($list)) {
       return array_key_exists($key, $list) ? $list[$key] : $default;
     }
+    if ($list instanceof ArrayAccess) {
+      return $list[$key] ?? $default;
+    }
+    // @todo - eliminate these from core & uncomment this line.
+    // CRM_Core_Error::deprecatedFunctionWarning('You have passed an invalid parameter for the "list"');
     return $default;
   }
 
index 797596a418f6bc7a1e7baf6bfdb6570245c18947..770c296fad0b1b2d3b935188ed5a8d198800225b 100644 (file)
@@ -191,6 +191,15 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt
     }
   }
 
+  /**
+   * Test retrieves using CRM_Utils_Array::value still work.
+   */
+  public function testUtilsArray() {
+    $propertyBag = new PropertyBag();
+    $propertyBag->setContactID(123);
+    $this->assertEquals(123, \CRM_Utils_Array::value('contact_id', $propertyBag));
+  }
+
   /**
    *
    * Data provider for testOtherParams