BuilderComposite.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Payment\Gateway\Request;
  7. use Magento\Framework\ObjectManager\TMap;
  8. use Magento\Framework\ObjectManager\TMapFactory;
  9. /**
  10. * Class BuilderComposite
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class BuilderComposite implements BuilderInterface
  15. {
  16. /**
  17. * @var BuilderInterface[] | TMap
  18. */
  19. private $builders;
  20. /**
  21. * @param TMapFactory $tmapFactory
  22. * @param array $builders
  23. */
  24. public function __construct(
  25. TMapFactory $tmapFactory,
  26. array $builders = []
  27. ) {
  28. $this->builders = $tmapFactory->create(
  29. [
  30. 'array' => $builders,
  31. 'type' => BuilderInterface::class
  32. ]
  33. );
  34. }
  35. /**
  36. * Builds ENV request
  37. *
  38. * @param array $buildSubject
  39. * @return array
  40. */
  41. public function build(array $buildSubject)
  42. {
  43. $result = [];
  44. foreach ($this->builders as $builder) {
  45. // @TODO implement exceptions catching
  46. $result = $this->merge($result, $builder->build($buildSubject));
  47. }
  48. return $result;
  49. }
  50. /**
  51. * Merge function for builders
  52. *
  53. * @param array $result
  54. * @param array $builder
  55. * @return array
  56. */
  57. protected function merge(array $result, array $builder)
  58. {
  59. return array_replace_recursive($result, $builder);
  60. }
  61. }