iwouldrathercode/php-simple-coupons

PHP package to help generates simple and unique coupon codes using random_bytes

v2.0 2022-04-10 11:49 UTC

This package is auto-updated.

Last update: 2024-04-14 07:00:35 UTC


README

Latest Version on Packagist Total Downloads

PHP package to help generates simple and unique coupon codes.

Installation

composer require iwouldrathercode/php-simple-coupons

Usage

Generating single coupon codes

use Iwouldrathercode\SimpleCoupons\Coupon;

.
.
.
// Somewhere in your code...

$code = new Coupon();

// Coupon code only
$coupon = $code->generate();

// Coupon code with a prefix of `ABC`
$coupon = $code->prepend('ABC')->generate();

// Coupon code with a prefix of `ABC` and suffix of `XYZ`
$coupon = $code->prepend('ABC')->append('XYZ')->generate();

// Coupon code with a prefix of `ABC` and suffix of `XYZ` and max. char. length as - 10
$coupon = $code->limit(10)->generate();
.
.

Generating multiple coupon codes

use Iwouldrathercode\SimpleCoupons\Coupon;

.
.
.

// Somewhere in your code...

function generateMultipleCodes($limit, $table)
{
    $couponsArray = [];
    $code = new Coupon();

    // Looping through unti limit is reached
    for($i=1; $i<=$limit; $i++) {

        // VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
        gc_enable(); 
        
        // Call to function to generate one coupon code
        generate($code, $couponsArray);

        // VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
        gc_disable();
    }

    // VERY VERY IMPORTANT - TO AVOID MEMORY_LIMIT ISSUES
    unset($code);

    return $array;
}

function generate($code, $couponsArray)
{
    $coupon = $code->limit(12)->generate();
    // echo $coloredOutput->apply("color_15", $coupon.PHP_EOL);
    array_push($couponsArray, $coupon);
    $code->__destruct();
}

// Generate 10 coupon codes
$multipleCoupons = generateMultipleCodes(10, $table);

For more examples do refer example.php and performance.php

Credits

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.