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.

Array.php 999B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. * @author Ricard Clau <ricard.clau@gmail.com>
  11. */
  12. class Twig_Extensions_Extension_Array extends Twig_Extension
  13. {
  14. /**
  15. * Returns a list of filters.
  16. *
  17. * @return array
  18. */
  19. public function getFilters()
  20. {
  21. $filters = array(
  22. new Twig_SimpleFilter('shuffle', 'twig_shuffle_filter'),
  23. );
  24. return $filters;
  25. }
  26. /**
  27. * Name of this extension.
  28. *
  29. * @return string
  30. */
  31. public function getName()
  32. {
  33. return 'array';
  34. }
  35. }
  36. /**
  37. * Shuffles an array.
  38. *
  39. * @param array|Traversable $array An array
  40. *
  41. * @return array
  42. */
  43. function twig_shuffle_filter($array)
  44. {
  45. if ($array instanceof Traversable) {
  46. $array = iterator_to_array($array, false);
  47. }
  48. shuffle($array);
  49. return $array;
  50. }