Esempio n. 1
0
                void WriteArray(PhpHashtable array)
                {
                    // [
                    Write(Tokens.ArrayOpen);

                    //
                    if (array.Count != 0)
                    {
                        bool bFirst = true;

                        var enumerator = array.GetFastEnumerator();
                        while (enumerator.MoveNext())
                        {
                            // ,
                            if (bFirst)
                            {
                                bFirst = false;
                            }
                            else
                            {
                                Write(Tokens.ItemsSeparatorString);
                            }

                            Debug.Assert(enumerator.CurrentKey.IsInteger);

                            // value
                            Accept(enumerator.CurrentValue);
                        }
                    }

                    // ]
                    Write(Tokens.ArrayClose);
                }
Esempio n. 2
0
        /// <summary>
        /// Creates a <see cref="Dictionary{TKey, TValue}"/> from given <see cref="PhpArray"/>.
        /// </summary>
        public static Dictionary <TKey, TElement> ToDictionary <TKey, TElement>(this PhpHashtable source, Func <IntStringKey, TKey> keySelector, Func <PhpValue, TElement> elementSelector, IEqualityComparer <TKey> comparer)
        {
            var result = new Dictionary <TKey, TElement>(source.Count, comparer);

            var enumerator = source.GetFastEnumerator();

            while (enumerator.MoveNext())
            {
                var current = enumerator.Current;
                result[keySelector(current.Key)] = elementSelector(current.Value);
            }

            return(result);
        }
Esempio n. 3
0
        public static PhpArray Compact(Dictionary <string, object> localVariables, params object[] names)
        {
            if (names == null)
            {
                PhpException.ArgumentNull("names");
                return(null);
            }

            PhpArray globals = (localVariables != null) ? null : ScriptContext.CurrentContext.GlobalVariables;
            PhpArray result  = new PhpArray();

            for (int i = 0; i < names.Length; i++)
            {
                string   name;
                PhpArray array;

                if ((name = PhpVariable.AsString(names[i])) != null)
                {
                    // if variable exists adds a copy of its current value to the result:
                    object value;

                    if (PhpHashtable.TryGetValue(globals, localVariables, name, out value))
                    {
                        result.Add(name, PhpVariable.DeepCopy(value));
                    }
                }
                else if ((array = names[i] as PhpArray) != null)
                {
                    // recursively searches for string variable names:
                    using (PhpHashtable.RecursiveEnumerator iterator = array.GetRecursiveEnumerator(false, true))
                    {
                        while (iterator.MoveNext())
                        {
                            if ((name = PhpVariable.AsString(iterator.Current.Value)) != null)
                            {
                                // if variable exists adds a copy of its current value to the result:
                                object value;
                                if (PhpHashtable.TryGetValue(globals, localVariables, name, out value))
                                {
                                    result.Add(name, PhpVariable.DeepCopy(value));
                                }
                            }
                        }
                    }
                }
            }

            return(result);
        }
Esempio n. 4
0
 /// <summary>
 /// Creates a <see cref="Dictionary{TKey, TValue}"/> from given <see cref="PhpArray"/>.
 /// </summary>
 public static Dictionary <TKey, TElement> ToDictionary <TKey, TElement>(this PhpHashtable source, Func <IntStringKey, TKey> keySelector, Func <PhpValue, TElement> elementSelector)
 {
     return(ToDictionary(source, keySelector, elementSelector, EqualityComparer <TKey> .Default));
 }
Esempio n. 5
0
 /// <summary>
 /// Creates a <see cref="Dictionary{TKey, TValue}"/> from given <see cref="PhpArray"/>.
 /// </summary>
 public static Dictionary <TKey, PhpValue> ToDictionary <TKey>(this PhpHashtable source, Func <IntStringKey, TKey> keySelector, IEqualityComparer <TKey> comparer)
 {
     return(ToDictionary(source, keySelector, FuncExtensions.Identity <PhpValue>(), comparer));
 }
Esempio n. 6
0
 /// <summary>
 /// Creates a <see cref="Dictionary{TKey, TValue}"/> from given <see cref="PhpArray"/>.
 /// </summary>
 public static Dictionary <IntStringKey, PhpValue> ToDictionary(this PhpHashtable source)
 {
     return(ToDictionary(source, FuncExtensions.Identity <IntStringKey>(), FuncExtensions.Identity <PhpValue>(), IntStringKey.EqualityComparer.Default));
 }