OperationDefinitionBuilder.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace tests\unit\Util;
  7. use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationDefinitionObject;
  8. class OperationDefinitionBuilder
  9. {
  10. /**
  11. * Name of the operation definition
  12. *
  13. * @var string
  14. */
  15. private $name;
  16. /**
  17. * Name of the operation for the operation definition
  18. *
  19. * @var string
  20. */
  21. private $operation;
  22. /**
  23. * Type of the operation for the operation defintions (e.g. create, delete)
  24. *
  25. * @var string
  26. */
  27. private $type;
  28. /**
  29. * An array containing the metadata definition for an object to be mapped against the API.
  30. *
  31. * @var array
  32. */
  33. private $metadata = [];
  34. /**
  35. * Determines if api URL should remove magento_backend_name.
  36. * @var boolean
  37. */
  38. private $removeBackend;
  39. /**
  40. * Function which builds an operation defintions based on the fields set by the user.
  41. *
  42. * @return OperationDefinitionObject
  43. */
  44. public function build()
  45. {
  46. return new OperationDefinitionObject(
  47. $this->name,
  48. $this->operation,
  49. $this->type,
  50. null,
  51. null,
  52. null,
  53. null,
  54. null,
  55. $this->metadata,
  56. null,
  57. false
  58. );
  59. }
  60. /**
  61. * Sets the name of the operation definition to be built.
  62. *
  63. * @param string $name
  64. * @return OperationDefinitionBuilder
  65. */
  66. public function withName($name)
  67. {
  68. $this->name = $name;
  69. return $this;
  70. }
  71. /**
  72. * Sets the name of the operation for the object to be built.
  73. *
  74. * @param string $operation
  75. * @return OperationDefinitionBuilder
  76. */
  77. public function withOperation($operation)
  78. {
  79. $this->operation = $operation;
  80. return $this;
  81. }
  82. /**
  83. * Sets the name of the type of operation (e.g. create, delete)
  84. *
  85. * @param string $type
  86. * @return OperationDefinitionBuilder
  87. */
  88. public function withType($type)
  89. {
  90. $this->type = $type;
  91. return $this;
  92. }
  93. /**
  94. * Takes an array of values => type or an array of operation elements and transforms into operation metadata.
  95. *
  96. * @param array $metadata
  97. * @return OperationDefinitionBuilder
  98. */
  99. public function withMetadata($metadata)
  100. {
  101. $primitives = [];
  102. foreach ($metadata as $fieldName => $value) {
  103. // type check here TODO
  104. if (is_string($value)) {
  105. $primitives[$fieldName] = $value;
  106. } else {
  107. $this->metadata[] = $value;
  108. }
  109. }
  110. $this->metadata = array_merge(
  111. $this->metadata,
  112. OperationElementBuilder::buildOperationElementFields($primitives)
  113. );
  114. return $this;
  115. }
  116. }