diff --git a/core/valuesetdef/ValueSetEnum.php b/core/valuesetdef/ValueSetEnum.php new file mode 100644 index 0000000000..574a9cfc09 --- /dev/null +++ b/core/valuesetdef/ValueSetEnum.php @@ -0,0 +1,94 @@ +m_values = $Values; + $this->bSortByValues = $bSortByValues; + } + + /** + * @return bool + * @see \ValueSetEnum::$bSortByValues + * @since 3.1.0 N°1646 + */ + public function IsSortedByValues(): bool + { + return $this->bSortByValues; + } + + // Helper to export the data model + public function GetValueList() + { + $this->LoadValues(null); + return $this->m_aValues; + } + + /** + * @inheritDoc + * @since 3.1.0 N°1646 Overload method + */ + public function SortValues(array &$aValues): void + { + // Force sort by values only if necessary + if ($this->bSortByValues) { + natcasesort($aValues); + return; + } + + // Don't sort values as we rely on the order defined during compilation + return; + } + + /** + * @param array|string $aArgs + * + * @return true + */ + protected function LoadValues($aArgs) + { + $aValues = []; + if (is_array($this->m_values)) { + foreach ($this->m_values as $key => $value) { + // Handle backed-enum case + if (is_object($value) && enum_exists(get_class($value))) { + $aValues[$value->value] = $value->value; + continue; + } + + $aValues[$key] = $value; + } + } elseif (is_string($this->m_values) && strlen($this->m_values) > 0) { + foreach (explode(",", $this->m_values) as $sVal) { + $sVal = trim($sVal); + $sKey = $sVal; + $aValues[$sKey] = $sVal; + } + } else { + $aValues = []; + } + $this->m_aValues = $aValues; + return true; + } +} \ No newline at end of file