N°7157 - Fix usage of Enum case value

This commit is contained in:
Molkobain
2024-03-06 23:05:16 +01:00
parent 02efea4e55
commit e05b4e772c
4 changed files with 31 additions and 8 deletions

View File

@@ -53,7 +53,7 @@ abstract class Trigger extends cmdbAbstractObject
MetaModel::Init_AddAttribute(new AttributeEnumSet("context", array("allowed_values" => null, "possible_values" => new ValueSetEnumPadded($aTags, true), "sql" => "context", "depends_on" => array(), "is_null_allowed" => true, "max_items" => 12)));
// "complement" is a computed field, fed by Trigger sub-classes, in general in ComputeValues method, for eg. the TriggerOnObject fed it with target_class info
MetaModel::Init_AddAttribute(new AttributeString("complement", array("allowed_values" => null, "sql" => "complement", "default_value" => null, "is_null_allowed" => true, "depends_on" => array())));
MetaModel::Init_AddAttribute(new AttributeEnum("subscription_policy", array("allowed_values" => new ValueSetEnum(implode(",", array_map(fn($case) => $case->value, Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::cases()))), "sql" => "subscription_policy", "default_value" => \Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::AllowNoChannel, "is_null_allowed" => false, "depends_on" => array())));
MetaModel::Init_AddAttribute(new AttributeEnum("subscription_policy", array("allowed_values" => new ValueSetEnum(implode(",", array_map(fn($case) => $case->value, Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::cases()))), "sql" => "subscription_policy", "default_value" => \Combodo\iTop\Core\Trigger\Enum\SubscriptionPolicy::AllowNoChannel->value, "is_null_allowed" => false, "depends_on" => array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('finalclass', 'description', 'context', 'subscription_policy', 'action_list', 'complement')); // Attributes to be displayed for the complete details

View File

@@ -471,6 +471,29 @@ class ValueSetEnum extends ValueSetDefinition
*/
protected bool $bSortByValues;
/**
* @param string $sBackedEnumFQCN FQCN of the backed enum to use
* @param bool $bSortValues {@see static::$bSortValues}
*
* @return \ValueSetEnum ValueSetEnum based on the values of the $sBackedEnumFQCN backed enum
* @throws \CoreException
* @since 3.2.0
*/
public static function FromBackedEnum(string $sBackedEnumFQCN, bool $bSortValues = false): ValueSetEnum
{
// First, check that we pass an enum as there is no generic type hint for that yet
if (false === enum_exists($sBackedEnumFQCN)) {
throw new CoreException("Can't instantiate " . __CLASS__ . "::" . __METHOD__ . " from a non-enum argument", [
"argument" => $sBackedEnumFQCN
]);
}
// Implode cases
$sJoinedValues = implode(",", array_map(fn($case) => $case->value, $sBackedEnumFQCN::cases()));
return new ValueSetEnum($sJoinedValues, $bSortValues);
}
/**
* @param array|string $Values
* @param bool $bLocalizedSort