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); }
/// <summary> /// Creates array containing variables and their values. /// </summary> /// <param name="locals">The table of defined variables.</param> /// <param name="names">Names of the variables - each chan be either /// <see cref="string"/> or <see cref="PhpArray"/>. Names are retrived recursively from an array.</param> /// <returns>The <see cref="PhpArray"/> which keys are names of variables and values are deep copies of /// their values.</returns> /// <remarks> /// Items in <paramref name="names"/> which are neither of type <see cref="string"/> nor <see cref="PhpArray"/> /// are ignored.</remarks> /// <exception cref="PhpException"><paramref name="names"/> is a <B>null</B> reference.</exception> public static PhpArray compact([ImportLocals] PhpArray locals, params PhpValue[] names) { if (names == null) { //PhpException.ArgumentNull("names"); //return null; throw new ArgumentNullException(nameof(names)); } PhpArray result = new PhpArray(names.Length); for (int i = 0; i < names.Length; i++) { string name; PhpArray array; if ((name = PhpVariable.ToStringOrNull(names[i])) != null) { // if variable exists adds a copy of its current value to the result: var value = locals[name]; if (value.IsSet) { result.Add(name, value.DeepCopy()); } } else if ((array = PhpVariable.ArrayOrNull(names[i])) != null) { // recursively searches for string variable names: using (PhpHashtable.RecursiveEnumerator iterator = array.GetRecursiveEnumerator(false, true)) { while (iterator.MoveNext()) { if ((name = PhpVariable.ToStringOrNull(iterator.Current.Value)) != null) { // if variable exists adds a copy of its current value to the result: var value = locals[name]; if (value.IsSet) { result.Add(name, value.DeepCopy()); } } } } } } // return(result); }
public static int Count(object variable, int mode) { // null or uninitialized variable: if (variable == null) { return(0); } // // hashtable // PhpHashtable ht; if ((ht = variable as PhpHashtable) != null) { // non recursive count: if (mode != CountRecursive) { return(ht.Count); } // recursive count: int result = 0; using (PhpHashtable.RecursiveEnumerator iterator = ht.GetRecursiveEnumerator(true, true)) { while (iterator.MoveNext()) { result++; } } return(result); } // // SPL.Countable // recursive count not supported (not even in PHP) // SPL.Countable countable; if ((countable = variable as SPL.Countable) != null) { object cnt = countable.count(ScriptContext.CurrentContext); return((cnt != null) ? PHP.Core.Convert.ObjectToInteger(cnt) : 0); } PHP.Core.Reflection.DObject dobj; if ((dobj = variable as PHP.Core.Reflection.DObject) != null) { if (dobj.RealObject is SPL.Countable) { object cnt = dobj.InvokeMethod("count", null, ScriptContext.CurrentContext); return((cnt != null) ? PHP.Core.Convert.ObjectToInteger(cnt) : 0); } else if (dobj.RealObject is ICollection) { return(((ICollection)dobj.RealObject).Count); } } // count not supported return(1); }