mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-24 09:19:50 +01:00
add ean barcode provider
This commit is contained in:
parent
9933d3a056
commit
86e8ad2c08
@ -6,7 +6,7 @@ class Factory
|
||||
{
|
||||
const DEFAULT_LOCALE = 'en_US';
|
||||
|
||||
protected static $defaultProviders = array('Address', 'Color', 'Company', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid');
|
||||
protected static $defaultProviders = array('Address', 'Barcode', 'Color', 'Company', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid');
|
||||
|
||||
public static function create($locale = self::DEFAULT_LOCALE)
|
||||
{
|
||||
|
@ -19,6 +19,9 @@ namespace Faker;
|
||||
* @property float latitude
|
||||
* @property float longitude
|
||||
*
|
||||
* @property string ean13
|
||||
* @property string ean8
|
||||
*
|
||||
* @property string phoneNumber
|
||||
*
|
||||
* @property string company
|
||||
|
37
src/Faker/Provider/Barcode.php
Normal file
37
src/Faker/Provider/Barcode.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Faker\Provider;
|
||||
|
||||
/**
|
||||
* @see http://en.wikipedia.org/wiki/EAN-13
|
||||
*/
|
||||
class Barcode extends \Faker\Provider\Base
|
||||
{
|
||||
private function ean($length=13)
|
||||
{
|
||||
$code = array();
|
||||
for($i = 0; $i < $length - 1; $i++) {
|
||||
$code[] = static::randomDigit();
|
||||
}
|
||||
|
||||
$sequence = $length == 8 ? array(3, 1) : array(1, 3);
|
||||
|
||||
$sums = 0;
|
||||
foreach($code as $n => $digit) {
|
||||
$sums += $digit * $sequence[$n % 2];
|
||||
}
|
||||
|
||||
$checksum = (10 - $sums % 10) % 10;
|
||||
return implode('', $code) . $checksum;
|
||||
}
|
||||
|
||||
public function ean13()
|
||||
{
|
||||
return $this->ean(13);
|
||||
}
|
||||
|
||||
public function ean8()
|
||||
{
|
||||
return $this->ean(8);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user