ResultFactory.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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;
  7. use Magento\Framework\Phrase;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Magento\Framework\ObjectManagerInterface;
  10. /**
  11. * Class ResultFactory
  12. */
  13. class ResultFactory
  14. {
  15. /**
  16. * Object manager
  17. *
  18. * @var ObjectManagerInterface
  19. */
  20. protected $objectManager;
  21. /**
  22. * Instance name
  23. *
  24. * @var string
  25. */
  26. protected $instanceName;
  27. /**
  28. * Constructor
  29. *
  30. * @param ObjectManagerInterface $objectManager
  31. * @param string $instanceName
  32. */
  33. public function __construct(
  34. ObjectManagerInterface $objectManager,
  35. $instanceName = \Magento\Framework\View\TemplateEngine\Xhtml\ResultInterface::class
  36. ) {
  37. $this->objectManager = $objectManager;
  38. $this->instanceName = $instanceName;
  39. }
  40. /**
  41. * Create result
  42. *
  43. * @param array $arguments
  44. * @return ResultInterface
  45. * @throws LocalizedException
  46. */
  47. public function create(array $arguments = [])
  48. {
  49. $object = $this->objectManager->create($this->instanceName, $arguments);
  50. if (!($object instanceof ResultInterface)) {
  51. throw new LocalizedException(new Phrase('This class must implement the "ResultInterface"'));
  52. }
  53. return $object;
  54. }
  55. }