Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit 9c5c88f

Browse files
committed
Merge pull request #412 from fzaninotto/faster_barcode
Improve barcode formatter speed, add tests for it
2 parents 10d9057 + 7ee6210 commit 9c5c88f

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ The populator uses name and column type guessers to populate each column with re
328328
```php
329329
<?php
330330
$populator->addEntity('Book', 5, array(
331-
'ISBN' => function() use ($generator) { return $generator->randomNumber(13); }
331+
'ISBN' => function() use ($generator) { return $generator->ean13(); }
332332
));
333333
```
334334

src/Faker/Provider/Barcode.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ class Barcode extends \Faker\Provider\Base
99
{
1010
private function ean($length = 13)
1111
{
12-
$code = array();
13-
for ($i = 0; $i < $length - 1; $i++) {
14-
$code[] = static::randomDigit();
15-
}
12+
$code = $this->numerify(str_repeat('#', $length - 1));
1613

17-
$sequence = $length == 8 ? array(3, 1) : array(1, 3);
14+
return $code . static::eanChecksum($code);
15+
}
1816

17+
/**
18+
* Utility function for computing EAN checksums
19+
*/
20+
protected static function eanChecksum($input)
21+
{
22+
$sequence = (strlen($input) - 1) == 8 ? array(3, 1) : array(1, 3);
1923
$sums = 0;
20-
foreach ($code as $n => $digit) {
24+
foreach (str_split($input) as $n => $digit) {
2125
$sums += $digit * $sequence[$n % 2];
2226
}
23-
24-
$checksum = (10 - $sums % 10) % 10;
25-
26-
return implode('', $code) . $checksum;
27+
return (10 - $sums % 10) % 10;
2728
}
2829

2930
/**

test/Faker/Provider/BarcodeTest.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,39 @@
77

88
class BarcodeTest extends \PHPUnit_Framework_TestCase
99
{
10+
private $faker;
11+
1012
public function setUp()
1113
{
1214
$faker = new Generator();
1315
$faker->addProvider(new Barcode($faker));
16+
$faker->seed(0);
1417
$this->faker = $faker;
1518
}
1619

1720
public function testEan8()
1821
{
19-
$this->assertRegExp('/[\d]{8}/', $this->faker->ean8());
22+
$code = $this->faker->ean8();
23+
$this->assertRegExp('/^\d{8}$/i', $code);
24+
$codeWitoutChecksum = substr($code, 0, -1);
25+
$checksum = substr($code, -1);
26+
$this->assertEquals(TestableBarcode::eanChecksum($codeWitoutChecksum), $checksum);
2027
}
2128

2229
public function testEan13()
2330
{
24-
$this->assertRegExp('/[\d]{13}/', $this->faker->ean13());
31+
$code = $this->faker->ean13();
32+
$this->assertRegExp('/^\d{13}$/i', $code);
33+
$codeWitoutChecksum = substr($code, 0, -1);
34+
$checksum = substr($code, -1);
35+
$this->assertEquals(TestableBarcode::eanChecksum($codeWitoutChecksum), $checksum);
36+
}
37+
}
38+
39+
class TestableBarcode extends Barcode
40+
{
41+
public static function eanChecksum($input)
42+
{
43+
return parent::eanChecksum($input);
2544
}
2645
}

0 commit comments

Comments
 (0)