Composite.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Composite attribute property mapper
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Eav\Model\Entity\Setup\PropertyMapper;
  9. use Magento\Eav\Model\Entity\Setup\PropertyMapperInterface;
  10. use Magento\Framework\ObjectManagerInterface;
  11. class Composite implements PropertyMapperInterface
  12. {
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var array
  19. */
  20. protected $propertyMappers;
  21. /**
  22. * @param ObjectManagerInterface $objectManager
  23. * @param array $propertyMappers
  24. * @codeCoverageIgnore
  25. */
  26. public function __construct(ObjectManagerInterface $objectManager, array $propertyMappers = [])
  27. {
  28. $this->objectManager = $objectManager;
  29. $this->propertyMappers = $propertyMappers;
  30. }
  31. /**
  32. * Map input attribute properties to storage representation
  33. *
  34. * @param array $input
  35. * @param int $entityTypeId
  36. * @return array
  37. * @throws \InvalidArgumentException
  38. */
  39. public function map(array $input, $entityTypeId)
  40. {
  41. $data = [];
  42. foreach ($this->propertyMappers as $class) {
  43. if (!is_subclass_of($class, \Magento\Eav\Model\Entity\Setup\PropertyMapperInterface::class)) {
  44. throw new \InvalidArgumentException(
  45. 'Property mapper ' .
  46. $class .
  47. ' must' .
  48. ' implement \Magento\Eav\Model\Entity\Setup\PropertyMapperInterface'
  49. );
  50. }
  51. $data = array_replace($data, $this->objectManager->get($class)->map($input, $entityTypeId));
  52. }
  53. return $data;
  54. }
  55. }