diff --git a/readme.md b/readme.md index d80f6068..9f2f053f 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/src/Faker/Factory.php b/src/Faker/Factory.php index 53b82260..09e440c3 100644 --- a/src/Faker/Factory.php +++ b/src/Faker/Factory.php @@ -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) { diff --git a/src/Faker/Generator.php b/src/Faker/Generator.php index 9ca58a03..a37481b7 100644 --- a/src/Faker/Generator.php +++ b/src/Faker/Generator.php @@ -19,6 +19,9 @@ namespace Faker; * @property float latitude * @property float longitude * + * @property string ean13 + * @property string ean8 + * * @property string phoneNumber * * @property string company diff --git a/src/Faker/Provider/Barcode.php b/src/Faker/Provider/Barcode.php new file mode 100644 index 00000000..c5d88000 --- /dev/null +++ b/src/Faker/Provider/Barcode.php @@ -0,0 +1,47 @@ + $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); + } +}