PHP code example of waqassiwag / laravel-variant-generator

1. Go to this page and download the library: Download waqassiwag/laravel-variant-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

waqassiwag / laravel-variant-generator example snippets


use Waqassiwag\VariantGenerator\Facades\VariantGenerator;

$attributes = [
    'Color' => ['Red', 'Blue'],
    'Size' => ['S', 'M'],
    'Material' => ['Cotton', 'Polyester']
];

$variants = VariantGenerator::make($attributes)->generate();

[
    ['Color' => 'Red', 'Size' => 'S', 'Material' => 'Cotton'],
    ['Color' => 'Red', 'Size' => 'S', 'Material' => 'Polyester'],
    ['Color' => 'Red', 'Size' => 'M', 'Material' => 'Cotton'],
    // ...
]

VariantGenerator::make($attributes)
    ->exclude([
        ['Color' => 'Red', 'Size' => 'S']
    ])
    ->generate();

VariantGenerator::make($attributes)
    ->format(function ($variant) {
        return implode('-', $variant);
    })
    ->generate();

// Output: ['Red-S-Cotton', 'Red-S-Polyester', ...]

VariantGenerator::make($attributes)
    ->sku(function ($variant) {
        return strtoupper(
            substr($variant['Color'], 0, 3) . substr($variant['Size'], 0, 1)
        );
    })
    ->generate();

[
    [
        'attributes' => ['Color' => 'Red', 'Size' => 'Small'],
        'sku' => 'REDS'
    ],
    // ...
]

VariantGenerator::make($attributes)
    ->exclude([['Color' => 'Red', 'Size' => 'S']])
    ->sku(fn ($variant) => strtoupper(substr($variant['Color'],0,3).substr($variant['Size'],0,1)))
    ->format(fn ($variant, $processed) => $processed['sku'] . '-' . $variant['Material'])
    ->generate();