1
0
mirror of https://github.com/fzaninotto/Faker.git synced 2025-03-19 06:49:50 +01:00

Added Computer provider with support for creating a MAC addresses and internal IP’s

This commit is contained in:
kielabokkie 2014-03-15 23:15:52 +13:00
parent 3bab1d19cf
commit 5f71386645
5 changed files with 75 additions and 3 deletions

View File

@ -92,8 +92,8 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
titleMale // 'Mr.'
titleFemale // 'Ms.'
suffix // 'Jr.'
name($gender = null|'male'|'female') // 'Dr. Zane Stroman'
firstName($gender = null|'male'|'female') // 'Maynard'
name($gender = null|'male'|'female') // 'Dr. Zane Stroman'
firstName($gender = null|'male'|'female') // 'Maynard'
firstNameMale // 'Maynard'
firstNameFemale // 'Rachel'
lastName // 'Zulauf'
@ -168,6 +168,8 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
url // 'http://www.strackeframi.com/'
ipv4 // '109.133.32.252'
ipv6 // '8e65:933d:22ee:a232:f1c1:2741:1f10:117c'
localIpv4 // '10.242.58.8'
macAddress // '43:85:B7:08:10:CA'
### `Faker\Provider\UserAgent`

View File

@ -6,7 +6,7 @@ class Factory
{
const DEFAULT_LOCALE = 'en_US';
protected static $defaultProviders = array('Address', 'Barcode', 'Color', 'Company', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid');
protected static $defaultProviders = array('Address', 'Barcode', 'Color', 'Company', 'Computer', 'DateTime', 'File', 'Image', 'Internet', 'Lorem', 'Miscellaneous', 'Payment', 'Person', 'PhoneNumber', 'Text', 'UserAgent', 'Uuid');
public static function create($locale = self::DEFAULT_LOCALE)
{

View File

@ -57,6 +57,8 @@ namespace Faker;
* @property string $url
* @property string $ipv4
* @property string $ipv6
* @property string internalIpv4
* @property string macAddress
*
* @property int $unixTime
* @property \DateTime $dateTime

View File

@ -0,0 +1,46 @@
<?php
namespace Faker\Provider;
class Computer extends \Faker\Provider\Base
{
private static $macAddressDigits = array(
"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F"
);
/**
* @example '32:F1:39:2F:D6:18'
*/
public static function macAddress()
{
$digits = self::$macAddressDigits;
for ($i=0; $i<6; $i++) {
shuffle($digits);
$mac[] = $digits[0] . $digits[1];
}
$mac = implode(':', $mac);
return $mac;
}
/**
* @example '10.1.1.17'
*/
public static function localIp()
{
$start = ['10','192'];
$ip = $start[rand(0, 1)];
if ($ip === '192') {
$ip .= '.168';
} else {
$ip .= '.' . rand(0, 255);
}
$ip .= sprintf('.%s.%s', rand(0, 255), rand(0, 255));
return $ip;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace Faker\Test\Provider;
use Faker\Provider\Computer;
use Faker\Generator;
class ComputerTest extends \PHPUnit_Framework_TestCase
{
public function testMacAddress()
{
$this->assertRegExp('/^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$/i', Computer::macAddress());
}
public function testLocalIp()
{
$range1 = '(10)(\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|[1][0-9][0-9]|[1-9][0-9]|[0-9])){3}';
$range2 = '(192)\.(168)(\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|[1][0-9][0-9]|[1-9][0-9]|[0-9])){2}';
$this->assertRegExp('/^'.$range1.'|'.$range2.'$/', Computer::localIp());
}
}