PatternGenerator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\MessageGenerators;
  7. /**
  8. * Common implementation of message generator.
  9. * Takes a message template (placeholders for localization also can be used) and list
  10. * of required params, which should persist in input data.
  11. *
  12. * If template contains placeholders, when required params should be specified in the same order as
  13. * placeholders, for example:
  14. * Message is 'Case Update: New score for the order is %1. Previous score was %2.', then the required params order
  15. * should be ['new_score', 'prev_score'].
  16. */
  17. class PatternGenerator implements GeneratorInterface
  18. {
  19. /**
  20. * @var string
  21. */
  22. private $template;
  23. /**
  24. * @var array
  25. */
  26. private $requiredParams;
  27. /**
  28. * PatternGenerator constructor.
  29. *
  30. * @param string $template
  31. * @param array $requiredParams
  32. */
  33. public function __construct($template, array $requiredParams = [])
  34. {
  35. $this->template = $template;
  36. $this->requiredParams = $requiredParams;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function generate(array $data)
  42. {
  43. $placeholders = [];
  44. foreach ($this->requiredParams as $param) {
  45. if (empty($data[$param])) {
  46. throw new GeneratorException(__('The "%1" should not be empty.', $param));
  47. }
  48. $placeholders[] = $data[$param];
  49. }
  50. return __($this->template, ...$placeholders);
  51. }
  52. }