/// <summary> /// Zamienia wartość .NET na wartość tekstową PHP /// </summary> /// <param name="value"></param> /// <returns></returns> public static PhpCodeValue ToPhpCodeValue(object value, bool beauty = true) { if (value == null) { return(new PhpCodeValue("", PhpCodeValue.Kinds.Null)); } var type = value.GetType(); if (type.IsEnum) { return(_SingleEnumValueToPhpCode(value)); } if (value is bool) { return(PhpCodeValue.FromBool((bool)value)); } if (value is string) { return(PhpCodeValue.FromString(value as string)); } if (value is int) { return(PhpCodeValue.FromInt((int)value)); } if (value is double) { return(PhpCodeValue.FromDouble((double)value)); } throw new NotSupportedException(); }
public static PhpCodeValue EnumToPhpCode(object enumValue, bool beauty) { var DeclaringType = enumValue.GetType(); if (!DeclaringType.IsEnum) { throw new ArgumentException("value is not enum"); } { var _enumRender = DeclaringType.GetCustomAttributes(true).OfType <EnumRenderAttribute>().FirstOrDefault(); if (_enumRender != null) { if (_enumRender.Option == EnumRenderOptions.OctalNumbers) { return(PhpCodeValue.FromInt((int)enumValue, true)); } if (_enumRender.Option == EnumRenderOptions.OctalNumbers) { return(PhpCodeValue.FromInt((int)enumValue, false)); } } } // var values = Enum.GetValues(DeclaringType); // var names = Enum.GetNames(DeclaringType); var fields = DeclaringType.GetFields(BindingFlags.Static | BindingFlags.Public); string[] txt = enumValue.ToString().Split('|').Select(i => i.Trim()).ToArray(); if (txt.Length == 1) { return(_SingleEnumValueToPhpCode(enumValue)); // not a flag value } var dict = fields.ToDictionary(i => i, i => i.GetValue(null)); for (int i = 0; i < txt.Length; i++) { var fieldInfo = dict.Where(ii => ii.Value.ToString() == txt[i]).Select(ii => ii.Key).FirstOrDefault(); if (fieldInfo == null) { continue; } var fieldValue = fieldInfo.GetValue(null); PhpCodeValue tmp = _SingleEnumValueToPhpCode(fieldValue); txt[i] = tmp.PhpValue; } return(new PhpCodeValue(string.Join(beauty ? " | " : "|", txt), PhpCodeValue.Kinds.Other)); }
static PhpCodeValue _SingleEnumValueToPhpCode(object enumValue) { Type DeclaringType = enumValue.GetType(); { var customAttributes = DeclaringType.GetCustomAttributes(true); #region EnumRenderAttribute var _enumRender = DeclaringType.GetCustomAttributes(true).OfType <EnumRenderAttribute>().FirstOrDefault(); if (_enumRender != null) { string v; switch (_enumRender.Option) { case EnumRenderOptions.TheSameString: v = enumValue.ToString(); break; case EnumRenderOptions.UnderscoreLowercase: v = ConvertX(enumValue.ToString(), "_").ToLower(); break; case EnumRenderOptions.MinusLowercase: v = ConvertX(enumValue.ToString(), "-").ToLower(); break; case EnumRenderOptions.Numbers: return(PhpCodeValue.FromInt((int)enumValue, false)); case EnumRenderOptions.OctalNumbers: return(PhpCodeValue.FromInt((int)enumValue, true)); case EnumRenderOptions.UnderscoreUppercase: v = ConvertX(enumValue.ToString(), "_").ToUpper(); break; default: throw new NotSupportedException(); } if (!_enumRender.DefinedConst) { v = EscapeSingleQuote(v); } return(new PhpCodeValue(v, _enumRender.DefinedConst ? PhpCodeValue.Kinds.DefinedConst : PhpCodeValue.Kinds.StringConstant)); } #endregion } { //field var fi = DeclaringType.GetFields().Where(i => i.Name == enumValue.ToString()).FirstOrDefault(); if (fi != null) { var customAttributes = fi.GetCustomAttributes(true); var _rv = fi.GetCustomAttributes(true).OfType <RenderValueAttribute>().FirstOrDefault(); if (_rv != null) { string str; if (TryGetPhpStringValue(_rv.Name, out str)) { return(PhpCodeValue.FromString(str)); } if (!_rv.Name.StartsWith("$")) { return(new PhpCodeValue(_rv.Name, _rv.Name, PhpCodeValue.Kinds.DefinedConst)); } throw new NotSupportedException(); } if (customAttributes.Length > 0) { throw new NotSupportedException(); } } } return(new PhpCodeValue(EscapeSingleQuote(enumValue.ToString()), enumValue, PhpCodeValue.Kinds.Other)); }