Skip to content

Commit

Permalink
Support PropertyBag in CRM_Utils_Array
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eileenmcnaughton committed Mar 10, 2020
1 parent 895b386 commit 42d24af
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -38,6 +38,11 @@ public static function value($key, $list, $default = NULL) {
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;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/phpunit/Civi/Payment/PropertyBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ public function testRequire() {
}
}

/**
* 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
Expand Down

0 comments on commit 42d24af

Please sign in to comment.