forked from Gregwar/Captcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhraseBuilder.php
43 lines (37 loc) · 884 Bytes
/
PhraseBuilder.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
<?php
namespace Gregwar\Captcha;
/**
* Generates random phrase
*
* @author Gregwar <[email protected]>
*/
class PhraseBuilder implements PhraseBuilderInterface
{
/**
* Constructs a PhraseBuilder with given parameters
*/
public function __construct($length = 5, $charset = 'abcdefghijklmnpqrstuvwxyz123456789')
{
$this->length = $length;
$this->charset = $charset;
}
/**
* Generates random phrase of given length with given charset
*/
public function build()
{
$phrase = '';
$chars = str_split($this->charset);
for ($i = 0; $i < $this->length; $i++) {
$phrase .= $chars[array_rand($chars)];
}
return $phrase;
}
/**
* "Niceize" a code
*/
public function niceize($str)
{
return strtr(strtolower($str), '01', 'ol');
}
}