StringUtils.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Argument\Interpreter;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. use Magento\Framework\Stdlib\BooleanUtils;
  9. /**
  10. * Interpreter of string data type that may optionally perform text translation.
  11. */
  12. class StringUtils implements InterpreterInterface
  13. {
  14. /**
  15. * @var BaseStringUtils
  16. */
  17. private $baseStringUtils;
  18. /**
  19. * @var BooleanUtils
  20. */
  21. private $booleanUtils;
  22. /**
  23. * StringUtils constructor.
  24. *
  25. * @param BooleanUtils $booleanUtils
  26. * @param BaseStringUtils $baseStringUtils
  27. */
  28. public function __construct(
  29. BooleanUtils $booleanUtils,
  30. BaseStringUtils $baseStringUtils
  31. ) {
  32. $this->booleanUtils = $booleanUtils;
  33. $this->baseStringUtils = $baseStringUtils;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. * @return string
  38. * @throws \InvalidArgumentException
  39. */
  40. public function evaluate(array $data)
  41. {
  42. $result = $this->baseStringUtils->evaluate($data);
  43. $needTranslation = isset($data['translate'])
  44. ? $this->booleanUtils->toBoolean($data['translate'])
  45. : false;
  46. if ($needTranslation) {
  47. $result = (string)new \Magento\Framework\Phrase($result);
  48. }
  49. return $result;
  50. }
  51. }