PHP code example of macocci7 / php-combination

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

    

macocci7 / php-combination example snippets


Macocci7\PhpCombination\Combination

Macocci7\PhpCombination\CombinationGenerator

    2 ** $n - 1
    

    

    ci7\PhpCombination\Combination;

    $c = new Combination();
    $items = [ 'A', 'B', 'C', ];

    foreach ($c->all($items) as $index => $item) {
        echo sprintf(
            "%d: (%s)\n",
            $index,
            implode(', ', $item)
        );
    }
    

    

    ci7\PhpCombination\CombinationGenerator;

    $c = new CombinationGenerator();
    $items = [ 'A', 'B', 'C', ];

    foreach ($c->all($items) as $index => $item) {
        echo sprintf(
            "%d: (%s)\n",
            $index,
            implode(', ', $item)
        );
    }
    

    

    ci7\PhpCombination\Combination;

    // Create an Instance
    $c = new Combination();

    // All Items
    $items = ['A', 'B', 'C', 'D', 'E', ];
    echo sprintf("All Items:\n\t(%s)\n\n", implode(", ", $items));

    // Call back
    $f = function (array $array): string {
        return sprintf("(%s)", implode(', ', $array));
    };

    // All Combinations
    echo sprintf(
        "All Combinations:\n\t%s\n\n",
        implode("\n\t", array_map($f, $c->all($items)))
    );

    // All Pairs
    echo sprintf(
        "All Pairs:\n\t%s\n\n",
        implode("\n\t", array_map($f, $c->pairs($items)))
    );

    // All Combinations of $n elements
    $n = 3;
    echo sprintf(
        "All Combinations of %d elements:\n\t%s\n\n",
        $n,
        implode("\n\t", array_map($f, $c->ofN($items, $n)))
    );

    // All Combinations of $a to $b elements
    $a = 3;
    $b = 4;
    echo sprintf(
        "All Combinations of %d to %d elements:\n\t%s\n\n",
        $a,
        $b,
        implode("\n\t", array_map($f, $c->ofA2B($items, $a, $b)))
    );

    // All Combinations of $a1, $a2 and $a3
    $a1 = ['A1', 'A2', ];
    $a2 = ['B1', 'B2', 'B3', ];
    $a3 = ['C1', 'C2', 'C3', 'C4', ];

    echo "All Combinations of multiple arrays:\n";
    echo sprintf("\tArray1: (%s)\n", implode(', ', $a1));
    echo sprintf("\tArray2: (%s)\n", implode(', ', $a2));
    echo sprintf("\tArray3: (%s)\n", implode(', ', $a3));

    $r = $c->fromArrays([$a1, $a2, $a3, ]);
    $count = count($a1) * count($a2) * count($a3);
    $n = strlen((string) $count);
    echo sprintf("\tThere're %d patterns:\n", $count);
    foreach ($r as $i => $e) {
        echo sprintf("\t%" . $n . "d: (%s)\n", $i + 1, implode(', ', $e));
    }
    

    

    ci7\PhpCombination\Combination;

    // Create an Instance
    $c = new Combination();

    // All Items
    $items = ['A', 'B', 'C', 'D', 'E', ];
    echo sprintf("All Items:\n\t(%s)\n\n", implode(", ", $items));

    // Set a flag to sort
    $sort = true;

    // Call back
    $f = function (array $array): string {
        return sprintf("(%s)", implode(', ', $array));
    };

    // All Combinations
    echo sprintf(
        "All Combinations:\n\t%s\n\n",
        implode("\n\t", array_map($f, $c->all($items, $sort)))
    );

    // All Pairs: does not support sorting
    echo sprintf(
        "All Pairs:\n\t%s\n\n",
        implode("\n\t", array_map($f, $c->pairs($items)))
    );

    // All Combinations of $n elements
    $n = 3;
    echo sprintf(
        "All Combinations of %d elements:\n\t%s\n\n",
        $n,
        implode("\n\t", array_map($f, $c->ofN($items, $n, $sort)))
    );

    // All Combinations of $a to $b elements
    $a = 3;
    $b = 4;
    echo sprintf(
        "All Combinations of %d to %d elements:\n\t%s\n\n",
        $a,
        $b,
        implode("\n\t", array_map($f, $c->ofA2B($items, $a, $b, $sort)))
    );

    // All Combinations of $a1, $a2 and $a3: does not support sorting
    $a1 = ['A1', 'A2', ];
    $a2 = ['B1', 'B2', 'B3', ];
    $a3 = ['C1', 'C2', 'C3', 'C4', ];

    echo "All Combinations of multiple arrays:\n";
    echo sprintf("\tArray1: (%s)\n", implode(', ', $a1));
    echo sprintf("\tArray2: (%s)\n", implode(', ', $a2));
    echo sprintf("\tArray3: (%s)\n", implode(', ', $a3));

    $r = $c->fromArrays([$a1, $a2, $a3, ]);
    $count = count($a1) * count($a2) * count($a3);
    $n = strlen((string) $count);
    echo sprintf("\tThere're %d patterns:\n", $count);
    foreach ($r as $i => $e) {
        echo sprintf("\t%" . $n . "d: (%s)\n", $i + 1, implode(', ', $e));
    }
    

    

    ci7\PhpCombination\CombinationGenerator;

    // Create an Instance
    $c = new CombinationGenerator();

    // All Items
    $items = ['A', 'B', 'C', 'D', 'E', ];
    echo sprintf("All Items:\n\t(%s)\n\n", implode(", ", $items));

    // Common Format
    $fmt = "\t(%s)\n";

    // All Combinations
    echo "All Combinations:\n";
    foreach ($c->all($items) as $e) {
        echo sprintf($fmt, implode(', ', $e));
    }
    echo "\n";

    // All Pairs
    echo "All Pairs:\n";
    foreach ($c->pairs($items) as $e) {
        echo sprintf($fmt, implode(', ', $e));
    }
    echo "\n";

    // All Combinations of $n elements
    $n = 3;
    echo sprintf("All Combinations of %d elements:\n", $n);
    foreach ($c->ofN($items, $n) as $e) {
        echo sprintf($fmt, implode(', ', $e));
    }
    echo "\n";

    // All Combinations of $a to $b elements
    $a = 3;
    $b = 4;
    echo sprintf(
        "All Combinations of %d to %d elements:\n",
        $a,
        $b,
    );
    foreach ($c->ofA2B($items, $a, $b) as $e) {
        echo sprintf($fmt, implode(', ', $e));
    }
    echo "\n";

    // All Combinations of $a1, $a2 and $a3
    $a1 = [ 'A1', 'A2', ];
    $a2 = [ 'B1', 'B2', 'B3', ];
    $a3 = [ 'C1', 'C2', 'C3', 'C4', ];

    echo "All Combinations of multiple arrays:\n";
    echo sprintf("\tArray1: (%s)\n", implode(', ', $a1));
    echo sprintf("\tArray2: (%s)\n", implode(', ', $a2));
    echo sprintf("\tArray3: (%s)\n", implode(', ', $a3));

    $r = $c->fromArrays([$a1, $a2, $a3, ]);
    $count = count($a1) * count($a2) * count($a3);
    $n = strlen((string) $count);
    echo sprintf("\tThere're %d patterns:\n", $count);
    foreach ($r as $i => $e) {
        echo sprintf("\t%" . $n . "d: (%s)\n", $i + 1, implode(', ', $e));
    }
    

    

    namespace Macocci7\PhpCombination\Examples;

    use Monolog\Level;
    use Monolog\Logger;
    use Monolog\Handler\StreamHandler;

    class UseInPhpUnit
    {
        private Logger $log;

        public function __construct()
        {
            $this->log = new Logger('UseInPhpUnit');
            $this->log->pushHandler(
                new StreamHandler(__DIR__ . '/UseInPhpUnit.log', Level::Debug)
            );
        }

        public function order(
            int $productId,
            string $size,
            string $color,
            int $amount
        ) {
            $this->log->info('Adding a new order', [
                'productId' => $productId,
                'size' => $size,
                'color' => $color,
                'amount' => $amount,
            ]);
            return true;
        }
    }
    

    

    declare(strict_types=1);

    namespace Macocci7\PhpCombination;

    ributes\DataProvider;
    use PHPUnit\Framework\TestCase;
    use Macocci7\PhpCombination\Examples\UseInPhpUnit;
    use Macocci7\PhpCombination\Combination;

    final class UseInPhpUnitTest extends TestCase
    {
        public static function provide_order_can_order_correctly(): array
        {
            $products = [ 1101, 1102, ];
            $sizes = [ 'S', 'M', 'L', ];
            $colors = [ 'White', 'Black', ];
            $amount = [ 1, 2, ];
            $c = new Combination();
            $data = [];
            foreach (
                $c->fromArrays([$products, $sizes, $colors, $amount]) as $e
            ) {
                $data[implode(', ', $e)] = $e;
            }
            return $data;
        }

        /**
        * PHPDoc for PHPUnit 9.x
        * @dataProvider provide_order_can_order_correctly
        */
        // Attribute for PHPUnit 10.x or later
        #[DataProvider('provide_order_can_order_correctly')]
        public function test_order_can_order_correctly(
            int $productId,
            string $size,
            string $color,
            int $amount
        ): void {
            $u = new UseInPhpUnit();
            $this->assertTrue($u->order(
                $productId,
                $size,
                $color,
                $amount
            ));
        }
    }
    
bash
composer