Converter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Payment Config Converter
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Payment\Model\Config;
  9. class Converter implements \Magento\Framework\Config\ConverterInterface
  10. {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function convert($source)
  15. {
  16. $xpath = new \DOMXPath($source);
  17. return [
  18. 'credit_cards' => $this->convertCreditCards($xpath),
  19. 'groups' => $this->convertGroups($xpath),
  20. 'methods' => $this->convertMethods($xpath)
  21. ];
  22. }
  23. /**
  24. * Convert credit cards xml tree to array
  25. *
  26. * @param \DOMXPath $xpath
  27. * @return array
  28. */
  29. protected function convertCreditCards(\DOMXPath $xpath)
  30. {
  31. $creditCards = [];
  32. /** @var \DOMNode $type */
  33. foreach ($xpath->query('/payment/credit_cards/type') as $type) {
  34. $typeArray = [];
  35. /** @var $typeSubNode \DOMNode */
  36. foreach ($type->childNodes as $typeSubNode) {
  37. switch ($typeSubNode->nodeName) {
  38. case 'label':
  39. $typeArray['name'] = $typeSubNode->nodeValue;
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. $typeAttributes = $type->attributes;
  46. $typeArray['order'] = $typeAttributes->getNamedItem('order')->nodeValue;
  47. $ccId = $typeAttributes->getNamedItem('id')->nodeValue;
  48. $creditCards[$ccId] = $typeArray;
  49. }
  50. uasort($creditCards, [$this, '_compareCcTypes']);
  51. $config = [];
  52. foreach ($creditCards as $code => $data) {
  53. $config[$code] = $data['name'];
  54. }
  55. return $config;
  56. }
  57. /**
  58. * Compare sort order of CC Types
  59. *
  60. * @SuppressWarnings(PHPMD.UnusedPrivateMethod) Used in callback.
  61. *
  62. * @param array $left
  63. * @param array $right
  64. * @return int
  65. */
  66. private function _compareCcTypes($left, $right)
  67. {
  68. return $left['order'] - $right['order'];
  69. }
  70. /**
  71. * Convert groups xml tree to array
  72. *
  73. * @param \DOMXPath $xpath
  74. * @return array
  75. */
  76. protected function convertGroups(\DOMXPath $xpath)
  77. {
  78. $config = [];
  79. /** @var \DOMNode $group */
  80. foreach ($xpath->query('/payment/groups/group') as $group) {
  81. $groupAttributes = $group->attributes;
  82. $id = $groupAttributes->getNamedItem('id')->nodeValue;
  83. /** @var $groupSubNode \DOMNode */
  84. foreach ($group->childNodes as $groupSubNode) {
  85. switch ($groupSubNode->nodeName) {
  86. case 'label':
  87. $config[$id] = $groupSubNode->nodeValue;
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. }
  94. return $config;
  95. }
  96. /**
  97. * Convert methods xml tree to array
  98. *
  99. * @param \DOMXPath $xpath
  100. * @return array
  101. */
  102. protected function convertMethods(\DOMXPath $xpath)
  103. {
  104. $config = [];
  105. /** @var \DOMNode $method */
  106. foreach ($xpath->query('/payment/methods/method') as $method) {
  107. $name = $method->attributes->getNamedItem('name')->nodeValue;
  108. /** @var $methodSubNode \DOMNode */
  109. foreach ($method->childNodes as $methodSubNode) {
  110. if ($methodSubNode->nodeType != XML_ELEMENT_NODE) {
  111. continue;
  112. }
  113. $config[$name][$methodSubNode->nodeName] = $methodSubNode->nodeValue;
  114. }
  115. }
  116. return $config;
  117. }
  118. }