-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
62 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace BikeShare\Credit\CodeGenerator; | ||
|
||
class CodeGenerator implements CodeGeneratorInterface | ||
{ | ||
// exclude problem chars: B8G6I1l0OQDS5Z2 | ||
private $acceptableChars = 'ACEFHJKMNPRTUVWXY4937'; | ||
|
||
public function generate($count, $length, $wastage = 25) | ||
{ | ||
// build array allowing for possible wastage through duplicate values | ||
for ($i = 0; $i <= $count + $wastage + 1; $i++) { | ||
$codes[] = substr(str_shuffle($this->acceptableChars), 0, $length); | ||
} | ||
|
||
return array_slice($codes, 0, ($count)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace BikeShare\Credit\CodeGenerator; | ||
|
||
interface CodeGeneratorInterface | ||
{ | ||
public function generate($count, $length, $wastage); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Test\BikeShare\Credit\CodeGenerator; | ||
|
||
use BikeShare\Credit\CodeGenerator\CodeGenerator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CodeGeneratorTest extends TestCase | ||
{ | ||
private $acceptableChars = 'ACEFHJKMNPRTUVWXY4937'; | ||
|
||
public function testGenerate() | ||
{ | ||
$count = 10; | ||
$length = 8; | ||
$wastage = 25; | ||
|
||
$codeGenerator = new CodeGenerator(); | ||
$codes = $codeGenerator->generate($count, $length, $wastage); | ||
$this->assertCount($count, $codes); | ||
foreach ($codes as $code) { | ||
$this->assertEquals($length, strlen($code)); | ||
$this->assertRegExp('/^[' . $this->acceptableChars . ']{' . $length . '}$/', $code); | ||
} | ||
} | ||
} |