Exemplo n.º 1
0
 public static IPhpArray EnsureItemArray(IPhpArray array, PhpValue index)
 {
     if (Convert.TryToIntStringKey(index, out IntStringKey key))
     {
         return(array.EnsureItemArray(key));
     }
     else
     {
         throw new ArgumentException();
     }
 }
 static IPhpArray EnsureItemArray(IPhpArray array, string key)
 {
     if (string.IsNullOrEmpty(key))
     {
         var newarr = new PhpArray();
         array.AddValue(PhpValue.Create(newarr));
         return(newarr);
     }
     else
     {
         return(array.EnsureItemArray(new IntStringKey(key)));
     }
 }
Exemplo n.º 3
0
 static IPhpArray EnsureItemArray(IPhpArray array, IntStringKey key)
 {
     if (key.Equals(IntStringKey.EmptyStringKey))
     {
         var newarr = new PhpArray();
         array.AddValue(PhpValue.Create(newarr));
         return(newarr);
     }
     else
     {
         return(array.EnsureItemArray(key));
     }
 }
Exemplo n.º 4
0
        public static PhpAlias EnsureItemAlias(IPhpArray array, PhpValue index, bool quiet)
        {
            if (Convert.TryToIntStringKey(index, out IntStringKey key))
            {
                return(array.EnsureItemAlias(key));
            }
            else
            {
                if (!quiet)
                {
                    throw new ArgumentException();
                }

                return(new PhpAlias(PhpValue.Void));
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Converts given array object to <see cref="stdClass"/>.
        /// </summary>
        /// <param name="array">Non-null reference to a PHP array.</param>
        /// <returns>New instance of <see cref="stdClass"/> with runtime fields filled from given array.</returns>
        public static object ToClass(IPhpArray array)
        {
            var convertible = array as IPhpConvertible;

            if (convertible != null)
            {
                return(convertible.ToClass());
            }
            else
            {
                //return new stdClass()
                //{
                //    __peach__runtimeFields = new PhpArray(array);
                //};
                throw new NotImplementedException();
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Adds a variable to auto-global array.
 /// </summary>
 /// <param name="array">The array.</param>
 /// <param name="name">A unparsed name of variable.</param>
 /// <param name="value">A value to be added.</param>
 /// <param name="subname">A name of intermediate array inserted before the value.</param>
 public static void AddVariable(IPhpArray /*!*/ array, string name, string value, string subname = null)
 {
     NameValueCollectionUtils.AddVariable(array, name, value, subname);
 }
Exemplo n.º 7
0
 public static int ToDouble(this IPhpArray value) => ToInt(value);
Exemplo n.º 8
0
 public static int ToLong(this IPhpArray value) => ToInt(value);
Exemplo n.º 9
0
 public static int ToInt(this IPhpArray value) => value.Count;
        /// <summary>
        /// Adds a variable to auto-global array.
        /// Duplicit entries are collected into a sub-array item.
        /// The routine respects <c>[subkey]</c> notation to build a hierarchy of sub-arrays.
        /// </summary>
        /// <param name="array">The array.</param>
        /// <param name="name">A unparsed name of variable.</param>
        /// <param name="value">A value to be added.</param>
        /// <param name="subname">A name of intermediate array inserted before the value.</param>
        public static void AddVariable(this IPhpArray /*!*/ array, string name, string value, string subname = null)
        {
            Debug.Assert(array != null);
            Debug.Assert(name != null);
            Debug.Assert(value != null);

            string key;

            // current left and right square brace positions:
            int left, right;

            // checks pattern {var_name}[{key1}][{key2}]...[{keyn}] where var_name is [^[]* and keys are [^]]*:
            left = name.IndexOf('[');
            if (left > 0 && left < name.Length - 1 && (right = name.IndexOf(']', left + 1)) >= 0)
            {
                // the variable name is a key to the "array", dots are replaced by underscores in top-level name:
                key = EncodeTopLevelName(name.Substring(0, left));

                // ensures that all [] operators in the chain except for the last one are applied on an array:
                for (;;)
                {
                    // adds a level keyed by "key":
                    array = EnsureItemArray(array, key);

                    // adds a level keyed by "subname" (once only):
                    if (subname != null)
                    {
                        array   = EnsureItemArray(array, subname);
                        subname = null;
                    }

                    // next key:
                    key = name.Substring(left + 1, right - left - 1);

                    // breaks if ']' is not followed by '[':
                    left = right + 1;
                    if (left == name.Length || name[left] != '[')
                    {
                        break;
                    }

                    // the next right brace:
                    right = name.IndexOf(']', left + 1);
                }

                if (string.IsNullOrEmpty(key))
                {
                    array.AddValue(PhpValue.Create(value));
                }
                else
                {
                    array.SetItemValue(new IntStringKey(key), PhpValue.Create(value));
                }
            }
            else
            {
                // no array pattern in variable name, "name" is a top-level key:
                name = EncodeTopLevelName(name);

                // inserts a subname on the next level:
                if (subname != null)
                {
                    EnsureItemArray(array, name).SetItemValue(new IntStringKey(subname), PhpValue.Create(value));
                }
                else
                {
                    array.SetItemValue(new IntStringKey(name), PhpValue.Create(value));
                }
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Ensures given variable is not <c>null</c>.
 /// </summary>
 public static IPhpArray EnsureArray(ref IPhpArray arr) => arr ?? (arr = new PhpArray());
Exemplo n.º 12
0
 /// <summary>
 /// Ensures given variable is not <c>null</c>.
 /// </summary>
 public static IPhpArray EnsureArray(ref IPhpArray arr) => arr ?? (arr = new PhpArray());
Exemplo n.º 13
0
 /// <summary>
 /// Converts given array object to <see cref="stdClass"/>.
 /// </summary>
 /// <param name="array">Non-null reference to a PHP array.</param>
 /// <returns>New instance of <see cref="stdClass"/> with runtime fields filled from given array.</returns>
 public static object ToClass(IPhpArray array)
 {
     var convertible = array as IPhpConvertible;
     if (convertible != null)
     {
         return convertible.ToClass();
     }
     else
     {
         //return new stdClass()
         //{
         //    __peach__runtimeFields = new PhpArray(array);
         //};
         throw new NotImplementedException();
     }
 }