* 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.
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;
}
}
}
+ /**
+ * 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