PHP code example of coshi / variator

1. Go to this page and download the library: Download coshi/variator 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/ */

    

coshi / variator example snippets

`
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        foreach($values as $value) {
            print($value); // displays first, second, third            
        }
    }
`
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'int',
            'min' => 0,
            'max' => 2
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }
`
    class DummyClass
    {
        public function someValues()
        {
            return [0, 1, 2];
        }
    }
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'callback',
            'callback' => [DummyClass::class, 'someValues']
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }
`
    class DummyClass
    {
        public function someValues($text)
        {
            $values = [
                'first' => [0, 1],
                'second' => [2, 3],
                'third' => [4, 5],
            ];
    
            return $values[$text];
        }
    }
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'callback',
            'callback' => [DummyClass::class, 'someValues', ['@text']],
            'context_dependent' => true
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }