You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_runtime_foreach.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Foreach Runtime Methods count
  4. *
  5. * @package Smarty
  6. * @subpackage PluginsInternal
  7. * @author Uwe Tews
  8. *
  9. **/
  10. class Smarty_Internal_Runtime_Foreach
  11. {
  12. /**
  13. * [util function] counts an array, arrayAccess/traversable or PDOStatement object
  14. *
  15. * @param mixed $value
  16. *
  17. * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0
  18. * for empty elements
  19. */
  20. public function count($value)
  21. {
  22. if (is_array($value) === true || $value instanceof Countable) {
  23. return count($value);
  24. } elseif ($value instanceof IteratorAggregate) {
  25. // Note: getIterator() returns a Traversable, not an Iterator
  26. // thus rewind() and valid() methods may not be present
  27. return iterator_count($value->getIterator());
  28. } elseif ($value instanceof Iterator) {
  29. if ($value instanceof Generator) {
  30. return 1;
  31. }
  32. return iterator_count($value);
  33. } elseif ($value instanceof PDOStatement) {
  34. return $value->rowCount();
  35. } elseif ($value instanceof Traversable) {
  36. return iterator_count($value);
  37. } elseif ($value instanceof ArrayAccess) {
  38. if ($value->offsetExists(0)) {
  39. return 1;
  40. }
  41. } elseif (is_object($value)) {
  42. return count($value);
  43. }
  44. return 0;
  45. }
  46. }