OperationElementBuilder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Handlers\OperationDefinitionObjectHandler;
  8. use Magento\FunctionalTestingFramework\DataGenerator\Objects\OperationElement;
  9. class OperationElementBuilder
  10. {
  11. /**
  12. * A set of fields which can be operation elements or primitive data (valueName => valueType). By default this
  13. * value contains a set of primitive fields.
  14. *
  15. * @var array
  16. */
  17. private $fields = [
  18. 'name' => 'string',
  19. 'gpa' => 'number',
  20. 'phone' => 'integer',
  21. 'isPrimary' => 'boolean'
  22. ];
  23. /**
  24. * Array of nested metadata, merged to main object via addElement()
  25. *
  26. * @var array
  27. */
  28. private $nestedMetadata = [];
  29. /**
  30. * The key to which the metadata defined will be mapped
  31. * in JSON { key : value }
  32. *
  33. * @var string
  34. */
  35. private $key = 'testType';
  36. /**
  37. * The type of value to which the metadata defined will be mapped (e.g. string, boolean, user defined object).
  38. * in JSON { key : value }
  39. *
  40. * @var string
  41. */
  42. private $type = 'testType';
  43. /**
  44. * The element type to which the metadata defined will be transformed into.
  45. * in JSON:
  46. * { } <- object
  47. * [ ] <- array
  48. * key : value <- field
  49. *
  50. * @var string
  51. */
  52. private $elementType = OperationDefinitionObjectHandler::ENTITY_OPERATION_OBJECT;
  53. /**
  54. * The array of elements which the Operation Element contains to resolve declarations within arrays specifically.
  55. * Arrays can take object references or definitions within themselves. This metadata has to be at a parent level in
  56. * order to resolve properly.
  57. *
  58. * @var array
  59. */
  60. private $nestedElements;
  61. /**
  62. * Build function which takes params defined by the user and returns a new Operation Element.
  63. *
  64. * @return OperationElement
  65. */
  66. public function build()
  67. {
  68. return new OperationElement(
  69. $this->key,
  70. $this->type,
  71. $this->elementType,
  72. null,
  73. $this->nestedElements,
  74. array_merge($this->nestedMetadata, self::buildOperationElementFields($this->fields))
  75. );
  76. }
  77. /**
  78. * Sets a new element type, overwrites any existing.
  79. *
  80. * @param string $elementType
  81. * @return OperationElementBuilder
  82. */
  83. public function withElementType($elementType)
  84. {
  85. $this->elementType = $elementType;
  86. return $this;
  87. }
  88. /**
  89. * Set a new set of fields or operation elements
  90. *
  91. * @param array $fields
  92. * @return OperationElementBuilder
  93. */
  94. public function withFields($fields)
  95. {
  96. $this->fields = $fields;
  97. return $this;
  98. }
  99. /**
  100. * Sets a key for the operation element. See ref to param key for explanation.
  101. *
  102. * @param string $key
  103. * @return OperationElementBuilder
  104. */
  105. public function withKey($key)
  106. {
  107. $this->key = $key;
  108. return $this;
  109. }
  110. /**
  111. * Sets a type for the operation element. See ref to param type for explanation.
  112. *
  113. * @param string $type
  114. * @return OperationElementBuilder
  115. */
  116. public function withType($type)
  117. {
  118. $this->type = $type;
  119. return $this;
  120. }
  121. /**
  122. * Adds a set of new Operation Elements to the nested metadata.
  123. *
  124. * @param array $elementsToAdd
  125. * @return OperationElementBuilder
  126. */
  127. public function addElements($elementsToAdd)
  128. {
  129. foreach ($elementsToAdd as $fieldKey => $metadata) {
  130. $this->nestedMetadata[$fieldKey] = $metadata;
  131. }
  132. return $this;
  133. }
  134. /**
  135. * Adds a new set of fields (value => type) into an object parameter to be converted to Operation Elements.
  136. *
  137. * @param array $fieldsToAdd
  138. * @return OperationElementBuilder
  139. */
  140. public function addFields($fieldsToAdd)
  141. {
  142. foreach ($fieldsToAdd as $fieldKey => $type) {
  143. $this->fields[$fieldKey] = $type;
  144. }
  145. return $this;
  146. }
  147. /**
  148. * Sets an array nested elements to an object property.
  149. *
  150. * @param array $nestedElements
  151. * @return OperationElementBuilder
  152. */
  153. public function withNestedElements($nestedElements)
  154. {
  155. $this->nestedElements = $nestedElements;
  156. return $this;
  157. }
  158. /**
  159. * Takes an array of fields (value => type) and returns an array of Operations Elements of type field.
  160. *
  161. * @param array $fields
  162. * @return array
  163. */
  164. public static function buildOperationElementFields($fields)
  165. {
  166. $operationElements = [];
  167. foreach ($fields as $fieldName => $type) {
  168. $operationElements[] = new OperationElement(
  169. $fieldName,
  170. $type,
  171. null,
  172. OperationDefinitionObjectHandler::ENTITY_OPERATION_ENTRY
  173. );
  174. }
  175. return $operationElements;
  176. }
  177. }