Pchp.Library.Arrays.MultiSortResolveArgs C# (CSharp) Method

MultiSortResolveArgs() private static method

Resolves arguments passed to MultiSort method according to PHP manual for array_multisort function.
Arrays and comparers can be a null reference. In such a case only number of arrays to be sorted is returned. Otherwise, arrays is filled with these arrays and comparers with comparers defined by appropriate arguments.
private static MultiSortResolveArgs ( Context ctx, PhpArray first, PhpValue args, PhpArray arrays, PhpValue>.IComparer comparers ) : int
ctx Pchp.Core.Context Current runtime context.
first Pchp.Core.PhpArray The first argument of .
args Pchp.Core.PhpValue The rest of arguments of .
arrays Pchp.Core.PhpArray An array to be filled with arrays passed in all arguments.
comparers PhpValue>.IComparer An array to be filled with comparers defined by arguments.
return int
        private static int MultiSortResolveArgs(
            Context ctx,
            PhpArray first,
            PhpValue[] args,
            PhpArray[] arrays,
            IComparer<KeyValuePair<IntStringKey, PhpValue>>[] comparers)
        {
            int col_count = 1;
            int row_count = first.Count;
            ComparisonMethod method = ComparisonMethod.Undefined;
            SortingOrder order = SortingOrder.Undefined;

            if (arrays != null)
            {
                arrays[0] = first;
            }

            for (int i = 0; i < args.Length; i++)
            {
                var arg = args[i];

                if (arg.IsArray)
                {
                    var array = arg.Array;

                    // checks whether the currently processed array has the same length as the first one:
                    if (array.Count != row_count)
                    {
                        //PhpException.Throw(PhpError.Warning, CoreResources.GetString("lengths_are_different", "the first array", string.Format("{0}-th array", col_count)));
                        //return 0;
                        throw new ArgumentException();
                    }
                    // sets next array:
                    if (arrays != null)
                        arrays[col_count] = array;

                    // sets comparer of the previous array:
                    if (comparers != null)
                        comparers[col_count - 1] = GetComparer(ctx, method, order, false);

                    // resets values:
                    method = ComparisonMethod.Undefined;
                    order = SortingOrder.Undefined;

                    col_count++;
                }
                else if (arg.TypeCode == PhpTypeCode.Long)
                {
                    var num = (int)arg.ToLong();

                    switch (num)
                    {
                        case (int)ComparisonMethod.Numeric:
                        case (int)ComparisonMethod.Regular:
                        case (int)ComparisonMethod.String:
                        case (int)ComparisonMethod.LocaleString:
                            if (method != ComparisonMethod.Undefined)
                            {
                                //PhpException.Throw(PhpError.Warning, LibResources.GetString("sorting_flag_already_specified", i));
                                //return 0;
                                throw new ArgumentException();
                            }
                            else
                            {
                                method = (ComparisonMethod)num;
                            }
                            break;

                        case (int)SortingOrder.Ascending:
                        case (int)SortingOrder.Descending:
                            if (order != SortingOrder.Undefined)
                            {
                                //PhpException.Throw(PhpError.Warning, LibResources.GetString("sorting_flag_already_specified", i));
                                //return 0;
                                throw new ArgumentException();
                            }
                            else
                            {
                                order = (SortingOrder)num;
                            }
                            break;

                        default:
                            //PhpException.Throw(PhpError.Warning, LibResources.GetString("argument_not_array_or_sort_flag", i));
                            //return 0;
                            throw new ArgumentException();
                    }
                }
                else
                {
                    //PhpException.Throw(PhpError.Warning, LibResources.GetString("argument_not_array_or_sort_flag", i));
                    //return 0;
                    throw new ArgumentException();
                }
            }

            // sets comparer of the previous array:
            if (comparers != null)
                comparers[col_count - 1] = GetComparer(ctx, method, order, false);

            return col_count;
        }