PaymentSectionModifier.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\Config\Structure;
  7. /**
  8. * PayPal change structure of payment methods configuration in admin panel.
  9. */
  10. class PaymentSectionModifier
  11. {
  12. /**
  13. * Identifiers of special payment method configuration groups
  14. *
  15. * @var array
  16. */
  17. private static $specialGroups = [
  18. 'account',
  19. 'recommended_solutions',
  20. 'other_paypal_payment_solutions',
  21. 'other_payment_methods',
  22. ];
  23. /**
  24. * Returns changed section structure.
  25. *
  26. * Payment configuration has predefined special blocks:
  27. * - Account information (id = account),
  28. * - Recommended Solutions (id = recommended_solutions),
  29. * - Other PayPal payment solution (id = other_paypal_payment_solutions),
  30. * - Other payment methods (id = other_payment_methods).
  31. * All payment methods configuration should be moved to one of this group.
  32. * To move payment method to specific configuration group specify "displayIn"
  33. * attribute in system.xml file equals to any id of predefined special group.
  34. * If "displayIn" attribute is not specified then payment method moved to "Other payment methods" group
  35. *
  36. * @param array $initialStructure
  37. * @return array
  38. */
  39. public function modify(array $initialStructure)
  40. {
  41. $changedStructure = array_fill_keys(self::$specialGroups, []);
  42. foreach ($initialStructure as $childSection => $childData) {
  43. if (in_array($childSection, self::$specialGroups)) {
  44. if (isset($changedStructure[$childSection]['children'])) {
  45. $children = $changedStructure[$childSection]['children'];
  46. if (isset($childData['children'])) {
  47. $children += $childData['children'];
  48. }
  49. $childData['children'] = $children;
  50. unset($children);
  51. }
  52. $changedStructure[$childSection] = $childData;
  53. } else {
  54. $moveInstructions = $this->getMoveInstructions($childSection, $childData);
  55. if (!empty($moveInstructions)) {
  56. foreach ($moveInstructions as $moveInstruction) {
  57. unset($childData['children'][$moveInstruction['section']]);
  58. unset($moveInstruction['data']['displayIn']);
  59. $changedStructure
  60. [$moveInstruction['parent']]
  61. ['children']
  62. [$moveInstruction['section']] = $moveInstruction['data'];
  63. }
  64. }
  65. if (!isset($moveInstructions[$childSection])) {
  66. $changedStructure['other_payment_methods']['children'][$childSection] = $childData;
  67. }
  68. }
  69. }
  70. return $changedStructure;
  71. }
  72. /**
  73. * Recursively collect groups that should be moved to special section
  74. *
  75. * @param string $section
  76. * @param array $data
  77. * @return array
  78. */
  79. private function getMoveInstructions($section, $data)
  80. {
  81. $moved = [];
  82. if (array_key_exists('children', $data)) {
  83. foreach ($data['children'] as $childSection => $childData) {
  84. $movedChildren = $this->getMoveInstructions($childSection, $childData);
  85. if (isset($movedChildren[$childSection])) {
  86. unset($data['children'][$childSection]);
  87. }
  88. $moved = array_merge($moved, $movedChildren);
  89. }
  90. }
  91. if (isset($data['displayIn']) && in_array($data['displayIn'], self::$specialGroups)) {
  92. $moved = array_merge(
  93. [
  94. $section => [
  95. 'parent' => $data['displayIn'],
  96. 'section' => $section,
  97. 'data' => $data
  98. ]
  99. ],
  100. $moved
  101. );
  102. }
  103. return $moved;
  104. }
  105. }