public static PhpArray array_pad(PhpArray array, int length, PhpValue value)
{
if (array == null)
{
// PhpException.ArgumentNull("array");
// return null;
throw new ArgumentNullException();
}
// number of items to add:
int remains = Math.Abs(length) - array.Count;
// returns unchanged array (or its deep copy if called from PHP):
if (remains <= 0) return array;
PhpArray result = new PhpArray(array.Count + remains);
// prepends items:
if (length < 0)
{
while (remains-- > 0) result.Add(value);
}
// inserts items from source array
// if a key is a string inserts it unchanged otherwise inserts value with max. integer key:
var iterator = array.GetFastEnumerator();
while (iterator.MoveNext())
{
var key = iterator.CurrentKey;
if (key.IsString)
result.Add(key, iterator.CurrentValue);
else
result.Add(iterator.CurrentValue);
}
// appends items:
if (length > 0)
{
while (remains-- > 0) result.Add(value);
}
// the result is inplace deeply copied on return to PHP code:
//result.InplaceCopyOnReturn = true;
return result;
}