1: | <?php |
2: | /** |
3: | * This code is licensed under AGPLv3 license or Afterlogic Software License |
4: | * if commercial version of the product was purchased. |
5: | * For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files. |
6: | */ |
7: | |
8: | namespace Aurora\System\Enums; |
9: | |
10: | /** |
11: | * @license https://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0 |
12: | * @license https://afterlogic.com/products/common-licensing Afterlogic Software License |
13: | * @copyright Copyright (c) 2019, Afterlogic Corp. |
14: | * |
15: | * @package Api |
16: | * @subpackage Enum |
17: | */ |
18: | class EnumConvert |
19: | { |
20: | /** |
21: | * @staticvar array $aClasses |
22: | * @param string $sClassName |
23: | * @return array |
24: | */ |
25: | protected static function GetInst($sClassName) |
26: | { |
27: | static $aClasses = array(); |
28: | |
29: | if (!isset($aClasses[$sClassName]) && class_exists($sClassName)) { |
30: | $aClasses[$sClassName] = new $sClassName(); |
31: | } |
32: | |
33: | return (isset($aClasses[$sClassName])) ? $aClasses[$sClassName]->getMap() : array(); |
34: | } |
35: | |
36: | /** |
37: | * @param mixed $mValue |
38: | * @param string $sClassName |
39: | * @return int |
40: | */ |
41: | public static function validate($mValue, $sClassName) |
42: | { |
43: | $aConsts = self::GetInst($sClassName); |
44: | |
45: | $sResult = null; |
46: | foreach ($aConsts as $mEnumValue) { |
47: | if ($mValue === $mEnumValue) { |
48: | $sResult = $mValue; |
49: | break; |
50: | } |
51: | } |
52: | return $sResult; |
53: | } |
54: | |
55: | /** |
56: | * @param mixed $sXmlValue |
57: | * @param string $sClassName |
58: | * @return int |
59: | */ |
60: | public static function FromXml($sXmlValue, $sClassName) |
61: | { |
62: | $aConsts = self::GetInst($sClassName); |
63: | |
64: | $niResult = null; |
65: | if (isset($aConsts[$sXmlValue])) { |
66: | $niResult = $aConsts[$sXmlValue]; |
67: | } |
68: | |
69: | return self::validate($niResult, $sClassName); |
70: | } |
71: | |
72: | /** |
73: | * @param mixed $sXmlValue |
74: | * @param string $sClassName |
75: | * @return int |
76: | */ |
77: | public static function FromPost($sXmlValue, $sClassName) |
78: | { |
79: | return self::FromXml($sXmlValue, $sClassName); |
80: | } |
81: | |
82: | /** |
83: | * @param mixed $mValue |
84: | * @param string $sClassName |
85: | * @return string |
86: | */ |
87: | public static function ToXml($mValue, $sClassName) |
88: | { |
89: | $aConsts = self::GetInst($sClassName); |
90: | |
91: | $sResult = ''; |
92: | foreach ($aConsts as $sKey => $mEnumValue) { |
93: | if ($mValue === $mEnumValue) { |
94: | $sResult = $sKey; |
95: | break; |
96: | } |
97: | } |
98: | return $sResult; |
99: | } |
100: | |
101: | /** |
102: | * @param mixed $mValue |
103: | * @param string $sClassName |
104: | * @return string |
105: | */ |
106: | public static function ToPost($mValue, $sClassName) |
107: | { |
108: | return self::ToXml($mValue, $sClassName); |
109: | } |
110: | } |
111: |