This repository was archived by the owner on Dec 11, 2020. It is now read-only.
File tree 3 files changed +33
-13
lines changed
3 files changed +33
-13
lines changed Original file line number Diff line number Diff line change @@ -328,7 +328,7 @@ The populator uses name and column type guessers to populate each column with re
328
328
``` php
329
329
<?php
330
330
$populator->addEntity('Book', 5, array(
331
- 'ISBN' => function() use ($generator) { return $generator->randomNumber(13 ); }
331
+ 'ISBN' => function() use ($generator) { return $generator->ean13( ); }
332
332
));
333
333
```
334
334
Original file line number Diff line number Diff line change @@ -9,21 +9,22 @@ class Barcode extends \Faker\Provider\Base
9
9
{
10
10
private function ean ($ length = 13 )
11
11
{
12
- $ code = array ();
13
- for ($ i = 0 ; $ i < $ length - 1 ; $ i ++) {
14
- $ code [] = static ::randomDigit ();
15
- }
12
+ $ code = $ this ->numerify (str_repeat ('# ' , $ length - 1 ));
16
13
17
- $ sequence = $ length == 8 ? array (3 , 1 ) : array (1 , 3 );
14
+ return $ code . static ::eanChecksum ($ code );
15
+ }
18
16
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 );
19
23
$ sums = 0 ;
20
- foreach ($ code as $ n => $ digit ) {
24
+ foreach (str_split ( $ input ) as $ n => $ digit ) {
21
25
$ sums += $ digit * $ sequence [$ n % 2 ];
22
26
}
23
-
24
- $ checksum = (10 - $ sums % 10 ) % 10 ;
25
-
26
- return implode ('' , $ code ) . $ checksum ;
27
+ return (10 - $ sums % 10 ) % 10 ;
27
28
}
28
29
29
30
/**
Original file line number Diff line number Diff line change 7
7
8
8
class BarcodeTest extends \PHPUnit_Framework_TestCase
9
9
{
10
+ private $ faker ;
11
+
10
12
public function setUp ()
11
13
{
12
14
$ faker = new Generator ();
13
15
$ faker ->addProvider (new Barcode ($ faker ));
16
+ $ faker ->seed (0 );
14
17
$ this ->faker = $ faker ;
15
18
}
16
19
17
20
public function testEan8 ()
18
21
{
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 );
20
27
}
21
28
22
29
public function testEan13 ()
23
30
{
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 );
25
44
}
26
45
}
You can’t perform that action at this time.
0 commit comments