CheckoutFieldsSchema.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Checkout\Schema;
  6. use Magento\Framework\Serialize\Serializer\Json;
  7. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  8. /**
  9. * CheckoutFields
  10. *
  11. * Provide the checkout fields schema in an unserialized, determined format.
  12. *
  13. * @package Temando\Shipping\Model
  14. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  15. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link http://www.temando.com/
  17. */
  18. class CheckoutFieldsSchema
  19. {
  20. /**
  21. * @var ModuleConfigInterface
  22. */
  23. private $moduleConfig;
  24. /**
  25. * @var CheckoutFieldFactory
  26. */
  27. private $fieldFactory;
  28. /**
  29. * @var Json
  30. */
  31. private $decoder;
  32. /**
  33. * CheckoutFieldsDefinition constructor.
  34. * @param ModuleConfigInterface $moduleConfig
  35. * @param CheckoutFieldFactory $fieldFactory
  36. * @param Json $decoder
  37. */
  38. public function __construct(
  39. ModuleConfigInterface $moduleConfig,
  40. CheckoutFieldFactory $fieldFactory,
  41. Json $decoder
  42. ) {
  43. $this->moduleConfig = $moduleConfig;
  44. $this->fieldFactory = $fieldFactory;
  45. $this->decoder = $decoder;
  46. }
  47. /**
  48. * @param string $type
  49. * @return string
  50. */
  51. private function mapFieldType($type)
  52. {
  53. $fieldTypeMap = [
  54. 'inputText' => 'text',
  55. 'inputNumber' => 'number',
  56. ];
  57. if (isset($fieldTypeMap[$type])) {
  58. return $fieldTypeMap[$type];
  59. }
  60. return $type;
  61. }
  62. /**
  63. * @return CheckoutField[]
  64. */
  65. public function getFields()
  66. {
  67. $fields = [];
  68. try {
  69. $fieldDefinitions = $this->decoder->unserialize($this->moduleConfig->getCheckoutFieldsDefinition());
  70. } catch (\InvalidArgumentException $e) {
  71. $fieldDefinitions = [];
  72. }
  73. foreach ($fieldDefinitions as $fieldDefinition) {
  74. $fieldData = [
  75. 'id' => $fieldDefinition['id'],
  76. 'label' => $fieldDefinition['label'],
  77. 'type' => $this->mapFieldType($fieldDefinition['fieldType']),
  78. 'orderPath' => $fieldDefinition['orderPath'],
  79. ];
  80. if (isset($fieldDefinition['defaultValue'])) {
  81. $fieldData['value'] = $fieldDefinition['defaultValue'];
  82. $fieldData['defaultValue'] = $fieldDefinition['defaultValue'];
  83. }
  84. if (isset($fieldDefinition['options'])) {
  85. $fieldData['options'] = $fieldDefinition['options'];
  86. }
  87. $field = $this->fieldFactory->create($fieldData);
  88. $fields[$field->getId()] = $field;
  89. }
  90. return $fields;
  91. }
  92. }