Guest User

Untitled

a guest
Mar 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. namespace Drupalbamboo_twig_extensionsTwigExtension;
  2.  
  3. /**
  4. * Provides bridge for Array functions and filters.
  5. *
  6. * Expose the features of Twig_Extensions_Extension_Array.
  7. */
  8. class TwigArray extends Twig_Extension {
  9.  
  10. /**
  11. * List of all Twig functions.
  12. */
  13. public function getFilters() {
  14. return [
  15. new Twig_SimpleFilter('bamboo_extensions_shuffle', [$this, 'shuffle']),
  16. ];
  17. }
  18.  
  19. /**
  20. * Unique identifier for this Twig extension.
  21. */
  22. public function getName() {
  23. return 'bamboo_twig_extensions.twig.array';
  24. }
  25.  
  26. /**
  27. * Shuffles an array.
  28. *
  29. * Can't use the Twig filter callback cause the shuffle function is
  30. * actually declared as a global function and not method of
  31. * Twig_Extensions_Extension_Array.
  32. *
  33. * @param array|Traversable $iterator
  34. * An array.
  35. *
  36. * @return array|bool
  37. * The shuffled array; or FALSE on failure.
  38. */
  39. public function shuffle($iterator) {
  40. $extension = new Twig_Extensions_Extension_Array();
  41. $filters = $extension->getFilters();
  42.  
  43. foreach ($filters as $filter) {
  44. if ($filter->getName() == 'shuffle') {
  45. $callable = $filter->getCallable();
  46. return $callable($iterator);
  47. }
  48. }
  49.  
  50. return FALSE;
  51. }
  52.  
  53. }
Add Comment
Please, Sign In to add comment