Mapper.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\EntityManager;
  7. /**
  8. * Class Mapper
  9. */
  10. class Mapper implements MapperInterface
  11. {
  12. /**
  13. * @var array
  14. */
  15. private $config;
  16. /**
  17. * Initialize dependencies.
  18. *
  19. * @param array $config
  20. */
  21. public function __construct(
  22. $config = []
  23. ) {
  24. $this->config = $config;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function entityToDatabase($entityType, $data)
  30. {
  31. if (isset($this->config[$entityType])) {
  32. foreach ($this->config[$entityType] as $databaseFieldName => $entityFieldName) {
  33. if (!$entityFieldName) {
  34. throw new \LogicException('Incorrect configuration for ' . $entityType);
  35. }
  36. if (isset($data[$entityFieldName])) {
  37. $data[$databaseFieldName] = $data[$entityFieldName];
  38. unset($data[$entityFieldName]);
  39. }
  40. }
  41. }
  42. return $data;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function databaseToEntity($entityType, $data)
  48. {
  49. if (isset($this->config[$entityType])) {
  50. foreach ($this->config[$entityType] as $databaseFieldName => $entityFieldName) {
  51. if (!$entityFieldName) {
  52. throw new \LogicException('Incorrect configuration for ' . $entityType);
  53. }
  54. if (isset($data[$databaseFieldName])) {
  55. $data[$entityFieldName] = $data[$databaseFieldName];
  56. unset($data[$databaseFieldName]);
  57. }
  58. }
  59. }
  60. return $data;
  61. }
  62. }