Text.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\TemplateEngine\Xhtml\Compiler;
  7. use Magento\Framework\DataObject;
  8. use Magento\Framework\View\TemplateEngine\Xhtml\Compiler\Directive\DirectiveInterface;
  9. /**
  10. * Class Text
  11. */
  12. class Text implements TextInterface
  13. {
  14. /**
  15. * @var DirectiveInterface[]
  16. */
  17. protected $directivePool;
  18. /**
  19. * Constructor
  20. *
  21. * @param DirectiveInterface[] $directivePool
  22. */
  23. public function __construct(array $directivePool)
  24. {
  25. $this->directivePool = $directivePool;
  26. }
  27. /**
  28. * Compiles the Element node
  29. *
  30. * @param \DOMText $node
  31. * @param DataObject $processedObject
  32. * @return void
  33. */
  34. public function compile(\DOMText $node, DataObject $processedObject)
  35. {
  36. $result = $node->textContent;
  37. foreach ($this->directivePool as $directive) {
  38. $result = preg_replace_callback(
  39. $directive->getPattern(),
  40. function ($match) use ($directive, $processedObject) {
  41. return $directive->execute($match, $processedObject);
  42. },
  43. $result
  44. );
  45. }
  46. $newNode = $node->ownerDocument->createTextNode($result);
  47. $node->parentNode->replaceChild($newNode, $node);
  48. }
  49. }