Pchp.Library.Variables.count C# (CSharp) Method

count() public static method

Counts items in a variable.
If any item of the variable contains infinite recursion skips items that are repeating because of such recursion.
public static count ( PhpValue variable, int mode = COUNT_NORMAL ) : long
variable Pchp.Core.PhpValue The variable which items to count.
mode int Whether to count recursively.
return long
        public static long count(PhpValue variable, int mode = COUNT_NORMAL)
        {
            // null or uninitialized variable:
            if (variable.IsNull)
            {
                return 0;
            }
            else if (variable.IsArray)
            {
                // PHP array
                return (mode == COUNT_RECURSIVE)
                    ? variable.Array.RecursiveCount
                    : variable.Array.Count;
            }
            else if (variable.IsObject)
            {
                // PHP Countable
                var countable = variable.Object as Countable;
                if (countable != null)
                {
                    return countable.count();
                }

                // CLR ICollection
                var collection = variable.Object as System.Collections.ICollection;
                if (collection != null)
                {
                    return collection.Count;
                }
            }
            else if (variable.IsAlias)
            {
                return count(variable.Alias.Value, mode);
            }

            // count not supported
            return 1;
        }