return array_key_exists($key, $list) ? $list[$key] : $default;
}
if ($list instanceof ArrayAccess) {
- return $list[$key] ?? $default;
+ // ArrayAccess requires offsetExists is implemented for the equivalent to array_key_exists.
+ return $list->offsetExists($key) ? $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 bool TRUE if we have that value (on our default store)
*/
public function offsetExists ($offset): bool {
- $prop = $this->handleLegacyPropNames($offset);
- return isset($this->props['default'][$prop]);
+ $prop = $this->handleLegacyPropNames($offset, TRUE);
+ return $prop && isset($this->props['default'][$prop]);
}
/**
/**
* @param string $prop
+ * @param bool $silent if TRUE return NULL instead of throwing an exception. This is because offsetExists should be safe and not throw exceptions.
* @return string canonical name.
* @throws \InvalidArgumentException if prop name not known.
*/
- protected function handleLegacyPropNames($prop) {
+ protected function handleLegacyPropNames($prop, $silent = FALSE) {
$newName = static::$propMap[$prop] ?? NULL;
if ($newName === TRUE) {
// Good, modern name.
return $prop;
}
if ($newName === NULL) {
+ if ($silent) {
+ // Only for use by offsetExists
+ return;
+ }
throw new \InvalidArgumentException("Unknown property '$prop'.");
}
// Remaining case is legacy name that's been translated.