/// <summary> /// Performs deep copy of the value. /// If the value is referenced, the method returns this instance of <see cref="PhpAlias"/>, otherwise the value is copied. /// </summary> public PhpValue DeepCopy() => ReferenceCount > 0 ? this.AddRef() : Value.DeepCopy();
/// <summary> /// Gets copy of given value. /// </summary> public static PhpValue DeepCopy(PhpValue value) => value.DeepCopy();
public override PhpArray ToArray(ref PhpValue me) => PhpArray.New(me.DeepCopy());
/// <summary> /// Implementation of <see cref="array_splice(PhpArray,int,int,object)"/> and <see cref="array_splice(PhpArray,int,int,object)"/>. /// </summary> /// <remarks>Whether to make a deep-copy of items in the replacement.</remarks> internal static PhpArray SpliceInternal(PhpArray array, int offset, int length, PhpValue replacement, bool deepCopy) { Debug.Assert(array != null); int count = array.Count; // converts offset and length to interval [first,last]: PhpMath.AbsolutizeRange(ref offset, ref length, count); PhpArray result = new PhpArray(length); // replacement is an array: if (replacement.IsArray) { // provides deep copies: IEnumerable<PhpValue> e = replacement.Array.Values; if (deepCopy) { e = e.Select(Operators.DeepCopy); } // does replacement: array.ReindexAndReplace(offset, length, e, result); } else if (replacement.IsNull) { // replacement is null: array.ReindexAndReplace(offset, length, null, result); } else { // replacement is another type // // creates a deep copy: if (deepCopy) replacement = replacement.DeepCopy(); // does replacement: array.ReindexAndReplace(offset, length, new[] { replacement }, result); } return result; }
public static PhpValue array_reduce(Context ctx, [In, Out] PhpArray array, IPhpCallable function, PhpValue initialValue) { if (array == null) { //PhpException.ReferenceNull("array"); //return PhpValue.Null; throw new ArgumentNullException(nameof(array)); } //if (!PhpArgument.CheckCallback(function, caller, "function", 0, false)) return null; if (array.Count == 0) { return initialValue; } PhpValue[] args = new PhpValue[] { initialValue.DeepCopy(), PhpValue.Null }; var iterator = array.GetFastEnumerator(); while (iterator.MoveNext()) { var item = iterator.CurrentValue; args[1] = item.IsAlias ? item : PhpValue.Create(item.EnsureAlias()); args[0] = function.Invoke(ctx, args); // updates an item if it wasn't alias if (!item.IsAlias) { iterator.CurrentValue = args[1].Alias.Value; } } // dereferences the last returned value: return args[0].GetValue(); }