Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pattern.php
Go to the documentation of this file.
1 <?php
7 
11 class Pattern
12 {
16  private $attributePattern = [
17  'is_required' => 1,
18  'is_visible_on_front' => 1,
19  'is_visible_in_advanced_search' => 0,
20  'attribute_code' => 'attribute_',
21  'backend_type' => '',
22  'is_searchable' => 0,
23  'is_filterable' => 0,
24  'is_filterable_in_search' => 0,
25  'frontend_label' => 'Attribute ',
26  'frontend_input' => 'select',
27  ];
28 
38  public function generateAttributeSet(
39  $name,
40  $attributesPerSet,
42  $attributePattern = null
43  ) {
44  $attributeSet = [
45  'name' => $name,
46  'attributes' => []
47  ];
48  for ($index = 1; $index <= $attributesPerSet; $index++) {
49  $attributeData = $this->generateAttribute(
50  $index,
52  );
53  if (is_callable($attributePattern)) {
54  $attributeData = $attributePattern($index, $attributeData);
55  }
56  $attributeSet['attributes']['attribute'][] = $attributeData;
57  }
58 
59  return $attributeSet;
60  }
61 
69  private function generateAttribute($index, $optionsPerAttribute)
70  {
71  $attribute = $this->attributePattern; // copy pattern
72  $attribute['attribute_code'] = $attribute['attribute_code'] . $index;
73  $attribute['frontend_label'] = $attribute['frontend_label'] . $index;
74  $attribute['options'] = ['option' => $this->generateOptions($optionsPerAttribute)];
75  $attribute['default_option'] = $attribute['options']['option'][0]['label'];
76  return $attribute;
77  }
78 
85  private function generateOptions($optionsPerAttribute)
86  {
87  $options = [];
88  for ($index = 1; $index <= $optionsPerAttribute; $index++) {
89  $options[] = [
90  'label' => 'option ' . $index,
91  'value' => 'option_' . $index
92  ];
93  }
94 
95  return $options;
96  }
97 }
generateAttributeSet( $name, $attributesPerSet, $optionsPerAttribute, $attributePattern=null)
Definition: Pattern.php:38
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14