Пример #1
0
        private static object GetItemEpilogue(object var, object key, GetItemKinds kind)
        {
            bool quiet = kind != GetItemKinds.Get;

            // empty:
            if (PhpVariable.IsEmpty(var))
            {
                /* silently returns null, see PHP specs, issue 22019 */
                //if (!quiet) PhpException.Throw(PhpError.Notice, CoreResources.GetString("empty_used_as_array"));

                return null;
            }

            // an item of a PhpArray (check inherited types):
            PhpArray array;
            if ((array = var as PhpArray) != null)
                return array.GetArrayItem(key, quiet);

            // object behaving as array:
            DObject dobj = var as DObject;
            if (dobj != null)
            {
                var realObject = dobj.RealObject;

                if (realObject is Library.SPL.ArrayAccess)
                    return Library.SPL.PhpArrayObject.GetUserArrayItem(dobj, key, kind);

                if (realObject is IList)
                    return GetListItem((IList)realObject, key, kind);

                if (realObject is IDictionary)
                    return GetDictionaryItem((IDictionary)realObject, key, kind);
            }

            // warnings (DObject, scalar type):
            /* silently returns null, see PHP specs, issue 22019 */
            //PhpException.VariableMisusedAsArray(var, false);

            return null;
        }
Пример #2
0
        private static object GetStringItemEpilogue(object var, string key, GetItemKinds kind)
        {
            bool quiet = kind != GetItemKinds.Get;
            int index;

            if (var == null)
                return null;

            // a character of a string:
            if (var.GetType() == typeof(string))
                return (CheckStringIndexRange(index = Convert.StringToInteger(key), ((string)var).Length, quiet)) ? ((string)var)[index].ToString() : null;

            // a character of a PhpString:
            if (var.GetType() == typeof(PhpString))
                return (CheckStringIndexRange(index = Convert.StringToInteger(key), ((PhpString)var).Length, quiet)) ? ((PhpString)var).GetCharUnchecked(index).ToString() : null;

            // a byte of a string of bytes:
            if (var.GetType() == typeof(PhpBytes))
                return (CheckStringIndexRange(index = Convert.StringToInteger(key), ((PhpBytes)var).Length, quiet)) ? new PhpBytes(new byte[] { ((PhpBytes)var)[index] }) : null;

            return GetItemEpilogue(var, key, kind);
        }
Пример #3
0
        public static object GetItemExact(object var, string/*!*/ key, GetItemKinds kind, int hashcode)
        {
            Debug.Assert(!(var is PhpReference) && key != null);

            if (var != null && var.GetType() == typeof(PhpArray))   // derived types checked in Epilogue
                return ((PhpArray)var).GetArrayItemExact(key, kind != GetItemKinds.Get, hashcode);

            return GetStringItemEpilogue(var, key, kind);
        }
Пример #4
0
        private static object GetItemEpilogue(object var, int key, GetItemKinds kind)
        {
            Debug.Assert(!(var is PhpReference));
            Debug.Assert(var == null || var.GetType() != typeof(PhpArray));

            // handle null reference:
            if (var == null)
                return null;

            //
            bool quiet = kind != GetItemKinds.Get;

            // a character of a string:
            if (var.GetType() == typeof(string))
                return (CheckStringIndexRange(key, ((string)var).Length, quiet)) ? ((string)var)[key].ToString() : null;

            // a character of a PhpString:
            if (var.GetType() == typeof(PhpString))
                return (CheckStringIndexRange(key, ((PhpString)var).Length, quiet)) ? ((PhpString)var).GetCharUnchecked(key).ToString() : null;

            // a byte of a string of bytes:
            if (var.GetType() == typeof(PhpBytes))
                return (CheckStringIndexRange(key, ((PhpBytes)var).Length, quiet)) ? new PhpBytes(new byte[] { ((PhpBytes)var)[key] }) : null;

            // general GetItem epilogue:
            return GetItemEpilogue(var, (object)key, kind);
        }
Пример #5
0
        public static object GetItem(object var, int key, GetItemKinds kind)
        {
            Debug.Assert(!(var is PhpReference));

            if (var != null && var.GetType() == typeof(PhpArray))   // derived types checked later in Epilogue
                // an item of a PhpArray:
                return ((PhpArray)var).GetArrayItem(key, kind != GetItemKinds.Get);
            else
                // the rest:
                return GetItemEpilogue(var, key, kind);
        }
Пример #6
0
        public static object GetItem(object var, object key, GetItemKinds kind)
        {
            Debug.Assert(!(var is PhpReference) && !(key is PhpReference));

            // an item of a PhpArray (fast check):
            if (var != null && var.GetType() == typeof(PhpArray))   // derived types checked in Epilogue
                return ((PhpArray)var).GetArrayItem(key, kind != GetItemKinds.Get);
            else
                return GetItemNonPhpArray(var, key, kind);
        }