-
Notifications
You must be signed in to change notification settings - Fork 4
/
PromoCode.php
73 lines (73 loc) · 1.62 KB
/
PromoCode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* PHPBenelux Promo Code Generator
*
* @copyright PHPBenelux
* @license Creative Commons Attribution-ShareAlike 3.0
* @author DragonBe
*/
class PromoCode implements Countable
{
const CODE_LENGTH = 10;
/**
* @var array
*/
protected $_codes = array ();
/**
* Generates promo codes
*
* @param int $count The amount of codes you want
*/
public function generate($count)
{
for ($i = 0; $i < $count; $i++) {
$this->addCode($this->_createCode());
}
}
/**
* Adds a code to the stack
*
* @param string $code
* @return PromoCode
*/
public function addCode($code)
{
$this->_codes[] = (string) $code;
return $this;
}
/**
* Retrieve a list of promo codes
*
* @return array The list of codes
*/
public function getCodes()
{
return $this->_codes;
}
/**
* Counts the amount of codes in this stack
*
* @return int The count of codes in this list
*/
public function count()
{
return count($this->getCodes());
}
/**
* Creates a single code using random characters
*
* @return string
* @access protected
*/
protected function _createCode()
{
$chars = array ('A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', '0', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$code = '';
for ($i = 0; $i < self::CODE_LENGTH; $i++) {
$code .= $chars[rand(0, count($chars) - 1)];
}
return $code;
}
}