Pattern.php 2.83 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Setup\Fixtures\AttributeSet;

/**
 * Generate Data for Fixture to create an Attribute Set with specified pattern.
 */
class Pattern
{
    /**
     * @var array
     */
    private $attributePattern = [
        'is_required' => 1,
        'is_visible_on_front' => 1,
        'is_visible_in_advanced_search' => 0,
        'attribute_code' => 'attribute_',
        'backend_type' => '',
        'is_searchable' => 0,
        'is_filterable' => 0,
        'is_filterable_in_search' => 0,
        'frontend_label' => 'Attribute ',
        'frontend_input' => 'select',
    ];

    /**
     * Generate Data for Fixture to create an Attribute Set with specified pattern.
     *
     * @param string $name
     * @param int $attributesPerSet
     * @param int $optionsPerAttribute
     * @param callable $attributePattern  callback in f($index, $attributeData) format
     * @return array
     */
    public function generateAttributeSet(
        $name,
        $attributesPerSet,
        $optionsPerAttribute,
        $attributePattern = null
    ) {
        $attributeSet = [
            'name' => $name,
            'attributes' => []
        ];
        for ($index = 1; $index <= $attributesPerSet; $index++) {
            $attributeData =  $this->generateAttribute(
                $index,
                is_array($optionsPerAttribute) ? $optionsPerAttribute[$index-1] : $optionsPerAttribute
            );
            if (is_callable($attributePattern)) {
                $attributeData = $attributePattern($index, $attributeData);
            }
            $attributeSet['attributes']['attribute'][] = $attributeData;
        }

        return $attributeSet;
    }

    /**
     * Generate Attributes for Set.
     *
     * @param int $index
     * @param int $optionsPerAttribute
     * @return array
     */
    private function generateAttribute($index, $optionsPerAttribute)
    {
        $attribute = $this->attributePattern; // copy pattern
        $attribute['attribute_code'] = $attribute['attribute_code'] . $index;
        $attribute['frontend_label'] = $attribute['frontend_label'] . $index;
        $attribute['options'] = ['option' => $this->generateOptions($optionsPerAttribute)];
        $attribute['default_option'] = $attribute['options']['option'][0]['label'];
        return $attribute;
    }

    /**
     * Generate Options for Attribute.
     *
     * @param int $optionsPerAttribute
     * @return array
     */
    private function generateOptions($optionsPerAttribute)
    {
        $options = [];
        for ($index = 1; $index <= $optionsPerAttribute; $index++) {
            $options[] = [
                'label' => 'option ' . $index,
                'value' => 'option_' . $index
            ];
        }

        return $options;
    }
}