IsSet() публичный статический Метод

Implementation of PHP isset operator.
public static IsSet ( PhpValue value ) : bool
value PhpValue
Результат bool
Пример #1
0
            //static Delegate Create_T_to_PhpValue()
            //{
            //    var type = typeof(T);

            //    if (type == typeof(PhpValue)) return Utilities.FuncExtensions.Identity<PhpValue>();
            //    if (type == typeof(int)) return new Func<int, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(long)) return new Func<long, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(bool)) return new Func<bool, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(double)) return new Func<double, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(float)) return new Func<float, PhpValue>(x => PhpValue.Create((double)x));
            //    if (type == typeof(string)) return new Func<string, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(uint)) return new Func<uint, PhpValue>(x => PhpValue.Create((long)x));
            //    if (type == typeof(byte[])) return new Func<byte[], PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpNumber)) return new Func<PhpNumber, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpArray)) return new Func<PhpArray, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpString)) return new Func<PhpString, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpAlias)) return new Func<PhpAlias, PhpValue>(x => PhpValue.Create(x));
            //    //if (type == typeof(object)) return new Func<object, PhpValue>(x => PhpValue.FromClass(x));
            //    throw new NotImplementedException();
            //}

            static Delegate Create_PhpValue_to_T()
            {
                if (typeof(T) == typeof(PhpValue))
                {
                    return(Utilities.FuncExtensions.Identity <PhpValue>());
                }
                if (typeof(T) == typeof(PhpAlias))
                {
                    return(new Func <PhpValue, PhpAlias>(x => x.AsPhpAlias()));
                }
                if (typeof(T) == typeof(string))
                {
                    return(new Func <PhpValue, string>(x => x.ToString()));
                }
                if (typeof(T) == typeof(double))
                {
                    return(new Func <PhpValue, double>(x => x.ToDouble()));
                }
                if (typeof(T) == typeof(float))
                {
                    return(new Func <PhpValue, float>(x => (float)x.ToDouble()));
                }
                if (typeof(T) == typeof(long))
                {
                    return(new Func <PhpValue, long>(x => x.ToLong()));
                }
                if (typeof(T) == typeof(int))
                {
                    return(new Func <PhpValue, int>(x => (int)x.ToLong()));
                }
                if (typeof(T) == typeof(bool))
                {
                    return(new Func <PhpValue, bool>(x => x.ToBoolean()));
                }
                if (typeof(T) == typeof(PhpArray))
                {
                    return(new Func <PhpValue, PhpArray>(x => x.ToArray()));
                }
                if (typeof(T) == typeof(byte))
                {
                    return(new Func <PhpValue, byte>(x => (byte)x.ToLong()));
                }
                if (typeof(T) == typeof(char))
                {
                    return(new Func <PhpValue, char>(x => Convert.ToChar(x)));
                }
                if (typeof(T) == typeof(uint))
                {
                    return(new Func <PhpValue, uint>(x => (uint)x.ToLong()));
                }
                if (typeof(T) == typeof(ulong))
                {
                    return(new Func <PhpValue, ulong>(x => (ulong)x.ToLong()));
                }
                if (typeof(T) == typeof(DateTime))
                {
                    return(new Func <PhpValue, DateTime>(Convert.ToDateTime));
                }
                if (typeof(T) == typeof(byte[]))
                {
                    return(new Func <PhpValue, byte[]>(x => x.ToBytesOrNull() ?? throw new InvalidCastException()));
                }

                if (typeof(T).IsValueType)
                {
                    if (typeof(T).IsGenericType)
                    {
                        var def = typeof(T).GetGenericTypeDefinition();
                        if (def == typeof(Nullable <>))
                        {
                            // Nullable<U>
                            var typeU = typeof(T).GenericTypeArguments[0];
                            if (typeU == typeof(bool))
                            {
                                return(new Func <PhpValue, Nullable <bool> >(x => Operators.IsSet(x) ? (bool?)x.ToBoolean() : null));
                            }
                            if (typeU == typeof(int))
                            {
                                return(new Func <PhpValue, Nullable <int> >(x => Operators.IsSet(x) ? (int?)(int)x.ToLong() : null));
                            }
                            if (typeU == typeof(long))
                            {
                                return(new Func <PhpValue, Nullable <long> >(x => Operators.IsSet(x) ? (long?)x.ToLong() : null));
                            }
                            if (typeU == typeof(double))
                            {
                                return(new Func <PhpValue, Nullable <double> >(x => Operators.IsSet(x) ? (double?)x.ToDouble() : null));
                            }
                        }
                    }
                }
                else // type.IsReferenceType
                {
                    // Delegate
                    if (typeof(T).BaseType == typeof(MulticastDelegate))
                    {
                        // Error: needs Context
                        throw new ArgumentException();
                    }
                    else if (typeof(T) == typeof(IDictionary <IntStringKey, PhpValue>))
                    {
                        return(new Func <PhpValue, IDictionary <IntStringKey, PhpValue> >(x => x.ToArray()));
                    }
                    else if (typeof(T) == typeof(IList <PhpValue>))
                    {
                        return(new Func <PhpValue, IList <PhpValue> >(x => x.ToArray()));
                    }
                    else if (typeof(T).IsGenericType)
                    {
                        var def = typeof(T).GetGenericTypeDefinition();
                        if (def == typeof(Dictionary <,>) || def == typeof(IDictionary <,>))
                        {
                            // Convert.ToDictionary<K, V>( PhpValue )
                            return(DictionaryConverter <T> .s_fromPhpValue);
                        }
                    }

                    // Object
                    return(new Func <PhpValue, T>(x => (T)StrictConvert.AsObject(x)));
                }

                throw new NotImplementedException($"Convert '{nameof(PhpValue)}' to '{typeof(T)}'.");
            }
Пример #2
0
            //static Delegate Create_T_to_PhpValue()
            //{
            //    var type = typeof(T);

            //    if (type == typeof(PhpValue)) return Utilities.FuncExtensions.Identity<PhpValue>();
            //    if (type == typeof(int)) return new Func<int, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(long)) return new Func<long, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(bool)) return new Func<bool, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(double)) return new Func<double, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(float)) return new Func<float, PhpValue>(x => PhpValue.Create((double)x));
            //    if (type == typeof(string)) return new Func<string, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(uint)) return new Func<uint, PhpValue>(x => PhpValue.Create((long)x));
            //    if (type == typeof(byte[])) return new Func<byte[], PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpNumber)) return new Func<PhpNumber, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpArray)) return new Func<PhpArray, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpString)) return new Func<PhpString, PhpValue>(x => PhpValue.Create(x));
            //    if (type == typeof(PhpAlias)) return new Func<PhpAlias, PhpValue>(x => PhpValue.Create(x));
            //    //if (type == typeof(object)) return new Func<object, PhpValue>(x => PhpValue.FromClass(x));
            //    throw new NotImplementedException();
            //}

            static Delegate Create_PhpValue_to_T()
            {
                var type = typeof(T);

                if (type == typeof(PhpValue))
                {
                    return(Utilities.FuncExtensions.Identity <PhpValue>());
                }
                if (type == typeof(PhpAlias))
                {
                    return(new Func <PhpValue, PhpAlias>(x => x.AsPhpAlias()));
                }
                if (type == typeof(string))
                {
                    return(new Func <PhpValue, string>(x => x.ToString()));
                }
                if (type == typeof(double))
                {
                    return(new Func <PhpValue, double>(x => x.ToDouble()));
                }
                if (type == typeof(float))
                {
                    return(new Func <PhpValue, float>(x => (float)x.ToDouble()));
                }
                if (type == typeof(long))
                {
                    return(new Func <PhpValue, long>(x => x.ToLong()));
                }
                if (type == typeof(int))
                {
                    return(new Func <PhpValue, int>(x => (int)x.ToLong()));
                }
                if (type == typeof(bool))
                {
                    return(new Func <PhpValue, bool>(x => x.ToBoolean()));
                }
                if (type == typeof(PhpArray))
                {
                    return(new Func <PhpValue, PhpArray>(x => x.ToArray()));
                }

                if (type.IsValueType)
                {
                    if (type.IsGenericType)
                    {
                        // Nullable<U>
                        if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            var typeU = type.GenericTypeArguments[0];
                            if (typeU == typeof(bool))
                            {
                                return(new Func <PhpValue, Nullable <bool> >(x => Operators.IsSet(x) ? (bool?)x.ToBoolean() : null));
                            }
                            if (typeU == typeof(int))
                            {
                                return(new Func <PhpValue, Nullable <int> >(x => Operators.IsSet(x) ? (int?)(int)x.ToLong() : null));
                            }
                            if (typeU == typeof(long))
                            {
                                return(new Func <PhpValue, Nullable <long> >(x => Operators.IsSet(x) ? (long?)x.ToLong() : null));
                            }
                            if (typeU == typeof(double))
                            {
                                return(new Func <PhpValue, Nullable <double> >(x => Operators.IsSet(x) ? (double?)x.ToDouble() : null));
                            }
                        }
                    }
                }
                else // type.IsReferenceType
                {
                    // Delegate
                    if (type.BaseType == typeof(MulticastDelegate))
                    {
                        // Error: needs Context
                        throw new ArgumentException();
                    }

                    // Object
                    return(new Func <PhpValue, T>(x => (T)x.AsObject()));
                }

                throw new NotImplementedException();
            }