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;
}