Array.php 1016 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. /**
  11. * @author Ricard Clau <ricard.clau@gmail.com>
  12. */
  13. class Twig_Extensions_Extension_Array extends Twig_Extension
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function getFilters()
  19. {
  20. $filters = array(
  21. new Twig_SimpleFilter('shuffle', 'twig_shuffle_filter'),
  22. );
  23. return $filters;
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function getName()
  29. {
  30. return 'array';
  31. }
  32. }
  33. /**
  34. * Shuffles an array.
  35. *
  36. * @param array|Traversable $array An array
  37. *
  38. * @return array
  39. */
  40. function twig_shuffle_filter($array)
  41. {
  42. if ($array instanceof Traversable) {
  43. $array = iterator_to_array($array, false);
  44. }
  45. shuffle($array);
  46. return $array;
  47. }
  48. class_alias('Twig_Extensions_Extension_Array', 'Twig\Extensions\ArrayExtension', false);