InputFactory.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl\Config\Element;
  8. use Magento\Framework\GraphQl\Config\ConfigElementFactoryInterface;
  9. use Magento\Framework\GraphQl\Config\ConfigElementInterface;
  10. use Magento\Framework\ObjectManagerInterface;
  11. /**
  12. * Factory for config elements of 'input' type.
  13. */
  14. class InputFactory implements ConfigElementFactoryInterface
  15. {
  16. /**
  17. * @var ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * @var FieldsFactory
  22. */
  23. private $fieldsFactory;
  24. /**
  25. * @param ObjectManagerInterface $objectManager
  26. * @param FieldsFactory $fieldsFactory
  27. */
  28. public function __construct(
  29. ObjectManagerInterface $objectManager,
  30. FieldsFactory $fieldsFactory
  31. ) {
  32. $this->objectManager = $objectManager;
  33. $this->fieldsFactory = $fieldsFactory;
  34. }
  35. /**
  36. * Instantiate an object representing 'input' GraphQL config element.
  37. *
  38. * @param array $data
  39. * @return ConfigElementInterface
  40. */
  41. public function createFromConfigData(array $data): ConfigElementInterface
  42. {
  43. $fields = isset($data['fields']) ? $this->fieldsFactory->createFromConfigData($data['fields']) : [];
  44. return $this->create(
  45. $data,
  46. $fields
  47. );
  48. }
  49. /**
  50. * Create input type object based off array of configured GraphQL InputType data.
  51. *
  52. * Type data must contain name and the type's fields. Optional data includes description.
  53. *
  54. * @param array $typeData
  55. * @param array $fields
  56. * @return Input
  57. */
  58. private function create(
  59. array $typeData,
  60. array $fields
  61. ): Input {
  62. return $this->objectManager->create(
  63. Input::class,
  64. [
  65. 'name' => $typeData['name'],
  66. 'fields' => $fields,
  67. 'description' => isset($typeData['description']) ? $typeData['description'] : ''
  68. ]
  69. );
  70. }
  71. }