iwouldrathercode/php-simple-coupons

PHP包,使用random_bytes帮助生成简单且唯一的优惠券代码

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

This package is auto-updated.

Last update: 2024-09-14 07:49:48 UTC


README

Latest Version on Packagist Total Downloads

PHP包,帮助生成简单且唯一的优惠券代码。

安装

composer require iwouldrathercode/php-simple-coupons

使用方法

生成单个优惠券代码

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();
.
.

生成多个优惠券代码

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);

更多示例请参考example.php和performance.php

致谢

贡献

请参阅 CONTRIBUTING 获取详细信息。

许可证

MIT许可证(MIT)。请参阅 许可证文件 获取更多信息。