Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
7 
12 class Builder
13 {
17  private $fields = [];
18 
23  public function build()
24  {
25  $document = [];
26  foreach ($this->fields as $field => $value) {
27  $document = $this->addFieldToDocument($document, $field, $value);
28  }
29  $this->clear();
30  return $document;
31  }
32 
36  private function clear()
37  {
38  $this->fields = [];
39  }
40 
47  private function addFieldToDocument($document, $field, $value)
48  {
49  if (is_array($value)) {
50  if (count($value) == 0) {
51  $document = array_merge($document, [$field => $value]);
52  } else {
53  $fields = [];
54  foreach ($value as $key => $val) {
55  $fields[$field][$key] = $val;
56  }
57  $document = array_merge($document, $fields);
58  }
59  } else {
60  $field = [$field => $value];
61  $document = array_merge($document, $field);
62  }
63  return $document;
64  }
65 
72  public function addField($field, $value)
73  {
74  $this->fields[$field] = $value;
75  return $this;
76  }
77 
83  public function addFields(array $fields)
84  {
85  $this->fields = array_merge($this->fields, $fields);
86  return $this;
87  }
88 }
$fields
Definition: details.phtml:14
$value
Definition: gender.phtml:16