HelperMethod.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Layout\Argument\Interpreter;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * Interpreter that returns invocation result of a helper method
  11. */
  12. class HelperMethod implements InterpreterInterface
  13. {
  14. /**
  15. * @var ObjectManagerInterface
  16. */
  17. private $objectManager;
  18. /**
  19. * @var NamedParams
  20. */
  21. private $paramsInterpreter;
  22. /**
  23. * @param ObjectManagerInterface $objectManager
  24. * @param NamedParams $paramsInterpreter
  25. */
  26. public function __construct(ObjectManagerInterface $objectManager, NamedParams $paramsInterpreter)
  27. {
  28. $this->objectManager = $objectManager;
  29. $this->paramsInterpreter = $paramsInterpreter;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. * @throws \InvalidArgumentException
  34. */
  35. public function evaluate(array $data)
  36. {
  37. if (!isset($data['helper']) || substr_count($data['helper'], '::') != 1) {
  38. throw new \InvalidArgumentException('Helper method name in format "\Class\Name::methodName" is expected.');
  39. }
  40. $helperMethod = $data['helper'];
  41. list($helperClass, $methodName) = explode('::', $helperMethod, 2);
  42. if (!method_exists($helperClass, $methodName)) {
  43. throw new \InvalidArgumentException("Helper method '{$helperMethod}' does not exist.");
  44. }
  45. $methodParams = $this->paramsInterpreter->evaluate($data);
  46. $methodParams = array_values($methodParams);
  47. // Use positional argument binding instead of named binding
  48. $helperInstance = $this->objectManager->get($helperClass);
  49. return call_user_func_array([$helperInstance, $methodName], $methodParams);
  50. }
  51. }