Skip to content
Snippets Groups Projects
Commit 9ae3708e authored by Mikael Meulle's avatar Mikael Meulle Committed by Pierre Dureau
Browse files

Issue #3491369 by just_like_good_vibes, pdureau, grimreaper, walli: Props with...

Issue #3491369 by just_like_good_vibes, pdureau, grimreaper, walli: Props with enum should handle empty string or NULL value (2)
parent 37c8dd09
Branches
Tags
1 merge request!288Issue #3491369 by just_like_good_vibes, grimreaper, walli: Props with enum should handle empty string or NULL value
Pipeline #361106 passed
......@@ -101,15 +101,49 @@ trait EnumTrait {
protected static function enumDefaultValue(?array $definition = NULL): mixed {
// First get the enum array.
$enum = (!is_array($definition)) ? [] : ($definition['enum'] ?? []);
if (!is_array($enum)) {
$enum = [];
if (!is_array($enum) || empty($enum)) {
return NULL;
}
// Fall back to default value (if defined)
if (is_array($definition) && isset($definition['default'])) {
if (isset($definition['default'])) {
return $definition['default'];
}
// If a type is defined, return the first enum value.
return (is_array($definition) && isset($definition['type']) && count($enum) > 0) ? $enum[0] : NULL;
// Return the first value, when:
// - a value is required
// - or the type is defined and not string compatible.
if (static::isEnumRequired($definition) ||
(isset($definition['type']) && !static::isEnumTypeStringCompatible($definition))) {
return (count($enum) > 0) ? $enum[0] : NULL;
}
return "";
}
/**
* Check if the enum type accepts string values.
*
* @param array $definition
* The definition.
*
* @return bool
* Whether the enum type accepts string values.
*/
protected static function isEnumTypeStringCompatible(array $definition): bool {
$type = $definition['type'] ?? [];
return ($type === "string") || (is_array($type) && in_array('string', $definition['type'], TRUE));
}
/**
* Check if the enum has a required value.
*
* @param array $definition
* The definition.
*
* @return bool
* Whether the enum has a required value.
*/
protected static function isEnumRequired(array $definition): bool {
$required_prop = $definition['required'] ?? FALSE;
return $required_prop === TRUE;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment