AmazonNameFactory.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Core\Domain;
  17. use Amazon\Core\Api\Data\AmazonNameInterface;
  18. use Magento\Framework\Exception\LocalizedException;
  19. use Magento\Framework\ObjectManagerInterface;
  20. use Magento\Store\Model\StoreManagerInterface;
  21. class AmazonNameFactory
  22. {
  23. /**
  24. * @var AmazonNameInterface
  25. */
  26. private $amazonName;
  27. /**
  28. * @var ObjectManagerInterface
  29. */
  30. private $objectManager = null;
  31. /**
  32. * @var array
  33. */
  34. private $perCountryNameHandlers;
  35. /**
  36. * @param ObjectManagerInterface $objectManager
  37. * @param AmazonNameInterface $amazonName
  38. * @param array $perCountryNameHandlers Per-country custom handlers of incoming name data.
  39. * The key as an "ISO 3166-1 alpha-2" country code and
  40. * the value as an FQCN of a child of AmazonAddress.
  41. */
  42. public function __construct(
  43. ObjectManagerInterface $objectManager,
  44. AmazonNameInterface $amazonName,
  45. array $perCountryNameHandlers = []
  46. ) {
  47. $this->objectManager = $objectManager;
  48. $this->amazonName = $amazonName;
  49. $this->perCountryNameHandlers = $perCountryNameHandlers;
  50. }
  51. /**
  52. * @param array $data
  53. * @return AmazonName
  54. * @throws LocalizedException
  55. */
  56. public function create(array $data = [])
  57. {
  58. $nameParts = explode(' ', trim($data['name']), 2);
  59. $data[AmazonNameInterface::FIRST_NAME] = $nameParts[0];
  60. $data[AmazonNameInterface::LAST_NAME] = $nameParts[1] ?? '.';
  61. $amazonName = $this->objectManager->create(AmazonName::class, ['data' => $data]);
  62. $countryCode = strtoupper($data['country']);
  63. if (empty($this->nameDecoratorPool[$countryCode])) {
  64. return $amazonName;
  65. }
  66. $amazonName = $this->objectManager->create(
  67. $this->nameDecoratorPool[$countryCode],
  68. [
  69. 'amazonName' => $amazonName,
  70. ]
  71. );
  72. if (!$amazonName instanceof AmazonNameInterface) {
  73. throw new LocalizedException(
  74. __(
  75. 'Address country handler %1 must be of type %2',
  76. [$this->nameDecoratorPool[$countryCode], AmazonName::class]
  77. )
  78. );
  79. }
  80. return $amazonName;
  81. }
  82. }