Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OperationElementBuilder.php
Go to the documentation of this file.
1 <?php
6 namespace tests\unit\Util;
7 
10 
12 {
19  private $fields = [
20  'name' => 'string',
21  'gpa' => 'number',
22  'phone' => 'integer',
23  'isPrimary' => 'boolean'
24  ];
25 
31  private $nestedMetadata = [];
32 
39  private $key = 'testType';
40 
47  private $type = 'testType';
48 
59 
67  private $nestedElements;
68 
74  public function build()
75  {
76  return new OperationElement(
77  $this->key,
78  $this->type,
79  $this->elementType,
80  null,
81  $this->nestedElements,
82  array_merge($this->nestedMetadata, self::buildOperationElementFields($this->fields))
83  );
84  }
85 
92  public function withElementType($elementType)
93  {
94  $this->elementType = $elementType;
95  return $this;
96  }
97 
104  public function withFields($fields)
105  {
106  $this->fields = $fields;
107  return $this;
108  }
109 
116  public function withKey($key)
117  {
118  $this->key = $key;
119  return $this;
120  }
121 
128  public function withType($type)
129  {
130  $this->type = $type;
131  return $this;
132  }
133 
140  public function addElements($elementsToAdd)
141  {
142  foreach ($elementsToAdd as $fieldKey => $metadata) {
143  $this->nestedMetadata[$fieldKey] = $metadata;
144  }
145 
146  return $this;
147  }
148 
155  public function addFields($fieldsToAdd)
156  {
157  foreach ($fieldsToAdd as $fieldKey => $type) {
158  $this->fields[$fieldKey] = $type;
159  }
160 
161  return $this;
162  }
163 
170  public function withNestedElements($nestedElements)
171  {
172  $this->nestedElements = $nestedElements;
173  return $this;
174  }
175 
182  public static function buildOperationElementFields($fields)
183  {
184  $operationElements = [];
185  foreach ($fields as $fieldName => $type) {
186  $operationElements[] = new OperationElement(
187  $fieldName,
188  $type,
189  null,
191  );
192  }
193 
194  return $operationElements;
195  }
196 }