mirror of
https://github.com/fzaninotto/Faker.git
synced 2025-03-23 08:49:53 +01:00
Merge pull request #278 from nineinchnick/barcodes
add ean barcode provider
This commit is contained in:
commit
4207ee3e51
@ -228,6 +228,11 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
|
||||
imageUrl($width, $height) // 'http://lorempixel.com/800/600/'
|
||||
imageUrl($width, $height, $category) // 'http://lorempixel.com/800/600/person/'
|
||||
|
||||
### `Faker\Provider\Barcode`
|
||||
|
||||
ean13 // '4006381333931'
|
||||
ean8 // '73513537'
|
||||
|
||||
## Unique and Optional modifiers
|
||||
|
||||
Faker provides two special providers, `unique()` and `optional()`, to be called before any provider. `optional()` can be useful for seeding non-required fields, like a mobile telephone number ; `unique()` is required to populate fields that cannot accept twice the same value, like primary identifiers.
|
||||
|
@ -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
|
||||
|
47
src/Faker/Provider/Barcode.php
Normal file
47
src/Faker/Provider/Barcode.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random EAN13 barcode.
|
||||
* @return string
|
||||
* @example '4006381333931'
|
||||
*/
|
||||
public function ean13()
|
||||
{
|
||||
return $this->ean(13);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a random EAN8 barcode.
|
||||
* @return string
|
||||
* @example '73513537'
|
||||
*/
|
||||
public function ean8()
|
||||
{
|
||||
return $this->ean(8);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user