/// <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); }
/// <summary> /// Creates array containing variables and their values. /// </summary> /// <param name="localsData">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(QueryValue <LocalVariables> localsData, params PhpValue[] names) { if (names == null) { //PhpException.ArgumentNull("names"); //return null; throw new ArgumentNullException(nameof(names)); } var result = new PhpArray(names.Length); var locals = localsData.Value.Locals; 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: if (locals.TryGetValue(name, out var value)) { result.Add(name, value.DeepCopy()); } } else if ((array = PhpVariable.ArrayOrNull(names[i])) != null) { // visit array values recursively new CompactVisitor(locals, result).Accept(array); } } // return(result); }