PHP code example of ws-packages / combination-generate

1. Go to this page and download the library: Download ws-packages/combination-generate 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/ */

    

ws-packages / combination-generate example snippets


'providers' => [
    // ...
    WsPackages\CombinationGenerate\Providers\CombinationServiceProvider::class,
],



namespace App\Http\Controllers;

use Illuminate\Http\Controller;
use WsPackages\CombinationGenerate\Services\CombinationService;

class ProductController extends Controller
{
    public function generateVariants(CombinationService $combinationService)
    {
        $productOptions = [
            ['Small', 'Medium', 'Large'],      // Sizes
            ['Red', 'Blue', 'Green'],          // Colors
            ['Cotton', 'Polyester']            // Materials
        ];
        
        $variants = $combinationService->generateCombination($productOptions);
        
        return response()->json([
            'total_variants' => count($variants),
            'variants' => $variants
        ]);
    }
}



namespace App\Services;

use WsPackages\CombinationGenerate\Services\CombinationService;

class ProductVariantService
{
    public function createAllVariants($attributes)
    {
        // Resolve from container
        $combinationService = app(CombinationService::class);
        
        return $combinationService->generateCombination($attributes);
    }
}



namespace App\Http\Controllers;

class InventoryController extends Controller
{
    public function generateCombinations()
    {
        // Using the singleton binding
        $service = app('combination-service');
        
        $options = [
            ['XS', 'S', 'M', 'L', 'XL'],
            ['Black', 'White', 'Navy'],
        ];
        
        return $service->generateCombination($options);
    }
}



namespace App\Http\Controllers;

use WsPackages\CombinationGenerate\Facades\Combination;

class QuickController extends Controller
{
    public function generate()
    {
        $variants = Combination::generateCombination([
            ['Red', 'Blue'],
            ['S', 'M', 'L']
        ]);
        
        return $variants;
    }
}

'aliases' => [
    // ... other aliases
    'Combination' => WsPackages\CombinationGenerate\Facades\Combination::class,
],

$variants = Combination::generateCombination($arrays);



namespace App\Console\Commands;

use Illuminate\Console\Command;
use WsPackages\CombinationGenerate\Services\CombinationService;

class GenerateProductVariants extends Command
{
    protected $signature = 'products:generate-variants {product}';
    protected $description = 'Generate all variants for a product';

    public function handle(CombinationService $combinationService)
    {
        $productId = $this->argument('product');
        
        // Get product attributes from database
        $attributes = [
            ['S', 'M', 'L'],
            ['Red', 'Blue'],
            ['Cotton', 'Silk']
        ];
        
        $variants = $combinationService->generateCombination($attributes);
        
        $this->info('Generated ' . count($variants) . ' variants');
        $this->table(['Size', 'Color', 'Material'], $variants);
    }
}



namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use WsPackages\CombinationGenerate\Services\CombinationService;

class ProcessProductVariants implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    private $productData;

    public function __construct($productData)
    {
        $this->productData = $productData;
    }

    public function handle(CombinationService $combinationService)
    {
        $variants = $combinationService->generateCombination($this->productData);
        
        // Process each variant...
        foreach ($variants as $variant) {
            // Save to database, update inventory, etc.
        }
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use WsPackages\CombinationGenerate\Services\CombinationService;

class Product extends Model
{
    public function generateVariants()
    {
        $service = app(CombinationService::class);
        
        $attributes = [
            $this->sizes()->pluck('name')->toArray(),
            $this->colors()->pluck('name')->toArray(),
            $this->materials()->pluck('name')->toArray(),
        ];
        
        return $service->generateCombination($attributes);
    }
}



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use WsPackages\CombinationGenerate\Services\CombinationService;

class ConfigurationController extends Controller
{
    public function getConfigOptions(CombinationService $service)
    {
        $options = [
            ['Basic', 'Premium', 'Enterprise'],    // Plans
            ['Monthly', 'Yearly'],                 // Billing
            ['1 User', '5 Users', '10 Users'],     // User limits
        ];
        
        $combinations = $service->generateCombination($options);
        
        return view('configuration', compact('combinations'));
    }
}



namespace Tests\Feature;

use Tests\TestCase;
use WsPackages\CombinationGenerate\Services\CombinationService;

class ProductTest extends TestCase
{
    public function test_all_product_configurations()
    {
        $service = app(CombinationService::class);
        
        $testCases = $service->generateCombination([
            ['enabled', 'disabled'],        // Feature flags
            ['guest', 'user', 'admin'],     // User roles
            ['mobile', 'desktop'],          // Platforms
        ]);
        
        foreach ($testCases as $case) {
            [$feature, $role, $platform] = $case;
            // Run test for each combination...
        }
    }
}

// In a controller
public function index(CombinationService $service)
{
    $result = $service->generateCombination([
        ['A', 'B'],
        [1, 2]
    ]);
    
    return response()->json($result);
    // Returns: [['A', 1], ['A', 2], ['B', 1], ['B', 2]]
}