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

Moved all color methods to a provider, and added

- safeHexColor
- safeColorName
- colorName
This commit is contained in:
lsv 2013-06-17 01:22:10 +02:00
parent ec0566a352
commit 99024404da
5 changed files with 164 additions and 63 deletions

View File

@ -154,9 +154,6 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
locale // en_UK
countryCode // UK
languageCode // en
hexcolor // #fa3cc2
rgbcolor() // 0,255,122
rgbColorAsArray() // array(0,255,122)
### `Faker\Provider\Base`
@ -189,6 +186,14 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
fileExtension // 'avi'
mimeType // 'video/x-msvideo'
### `Faker\Provider\Color`
hexcolor // '#fa3cc2'
rgbcolor // '0,255,122'
rgbColorAsArray // array(0,255,122)
safeColorName // 'fuchsia'
colorName // 'Gainsbor'
## Localization
`Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN).

View File

@ -0,0 +1,124 @@
<?php
/**
* Created by lsv
* Date: 6/17/13
* Time: 12:49 AM
*/
namespace Faker\Provider;
class Color extends Base
{
protected static $hexcodes = array(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f'
);
protected static $oldBrowserNames = array(
'black', 'maroon', 'green', 'navy', 'olive',
'purple', 'teal', 'lime', 'blue', 'silver',
'gray', 'yellow', 'fuchsia', 'aqua', 'white'
);
protected static $newBrowserNames = array(
'AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine',
'Azure', 'Beige', 'Bisque', 'Black', 'BlanchedAlmond',
'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue',
'Chartreuse', 'Chocolate', 'Coral', 'CornflowerBlue',
'Cornsilk', 'Crimson', 'Cyan', 'DarkBlue', 'DarkCyan',
'DarkGoldenRod', 'DarkGray', 'DarkGreen', 'DarkKhaki',
'DarkMagenta', 'DarkOliveGreen', 'Darkorange', 'DarkOrchid',
'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue',
'DarkSlateGray', 'DarkTurquoise', 'DarkViolet', 'DeepPink',
'DeepSkyBlue', 'DimGray', 'DimGrey', 'DodgerBlue', 'FireBrick',
'FloralWhite', 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite',
'Gold', 'GoldenRod', 'Gray', 'Green', 'GreenYellow', 'HoneyDew',
'HotPink', 'IndianRed ', 'Indigo ', 'Ivory', 'Khaki', 'Lavender',
'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue', 'LightCoral',
'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGreen', 'LightPink',
'LightSalmon', 'LightSeaGreen', 'LightSkyBlue', 'LightSlateGray', 'LightSteelBlue',
'LightYellow', 'Lime', 'LimeGreen', 'Linen', 'Magenta', 'Maroon', 'MediumAquaMarine',
'MediumBlue', 'MediumOrchid', 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue',
'MediumSpringGreen', 'MediumTurquoise', 'MediumVioletRed', 'MidnightBlue',
'MintCream', 'MistyRose', 'Moccasin', 'NavajoWhite', 'Navy', 'OldLace', 'Olive',
'OliveDrab', 'Orange', 'OrangeRed', 'Orchid', 'PaleGoldenRod', 'PaleGreen',
'PaleTurquoise', 'PaleVioletRed', 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum',
'PowderBlue', 'Purple', 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Salmon',
'SandyBrown', 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
'SlateGray', 'Snow', 'SpringGreen', 'SteelBlue', 'Tan', 'Teal', 'Thistle', 'Tomato',
'Turquoise', 'Violet', 'Wheat', 'White', 'WhiteSmoke', 'Yellow', 'YellowGreen'
);
/**
* @example '#fa3cc2'
*/
public static function hexColor()
{
return sprintf('#%s%s%s%s%s%s',
static::randomElement(static::$hexcodes),
static::randomElement(static::$hexcodes),
static::randomElement(static::$hexcodes),
static::randomElement(static::$hexcodes),
static::randomElement(static::$hexcodes),
static::randomElement(static::$hexcodes)
);
}
/**
* @example '#ff0044'
*/
public static function safeHexColor()
{
$color1 = static::randomElement(static::$hexcodes);
$color2 = static::randomElement(static::$hexcodes);
$color3 = static::randomElement(static::$hexcodes);
return sprintf('#%s%s%s%s%s%s',
$color1,
$color1,
$color2,
$color2,
$color3,
$color3
);
}
/**
* @example 'array(0,255,122)'
*/
public static function rgbColorAsArray()
{
$rgb = array();
$color = str_replace('#', '', static::hexColor());
$rgb[] = hexdec(substr($color,0,2));
$rgb[] = hexdec(substr($color,2,2));
$rgb[] = hexdec(substr($color,4,2));
return $rgb;
}
/**
* @example '0,255,122'
*/
public static function rgbColor()
{
return implode(',', static::rgbColorAsArray());
}
/**
* @example 'blue'
*/
public static function safeColorName()
{
return static::randomElement(static::$oldBrowserNames);
}
/**
* @example 'NavajoWhite'
*/
public static function colorName()
{
return static::randomElement(static::$newBrowserNames);
}
}

View File

@ -148,41 +148,4 @@ class Miscellaneous extends \Faker\Provider\Base
{
return static::randomElement(static::$languageCode);
}
/**
* @example '#fa3cc2'
*/
public static function hexColor()
{
$hexcodes = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
return sprintf('#%s%s%s%s%s%s',
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes),
static::randomElement($hexcodes)
);
}
/**
* @example 'array(0,255,122)'
*/
public static function rgbColorAsArray()
{
$rgb = array();
$color = str_replace('#', '', static::hexColor());
$rgb[] = hexdec(substr($color,0,2));
$rgb[] = hexdec(substr($color,2,2));
$rgb[] = hexdec(substr($color,4,2));
return $rgb;
}
/**
* @example '0,255,122'
*/
public static function rgbColor()
{
return implode(',', static::rgbColorAsArray());
}
}

View File

@ -3,7 +3,6 @@
namespace Faker\Test\Provider;
use Faker\Provider\Base as BaseProvider;
use Faker;
class BaseTest extends \PHPUnit_Framework_TestCase
{
@ -125,26 +124,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase
{
$this->assertRegExp('/foo[a-z]Ba\dr/', BaseProvider::bothify('foo?Ba#r'));
}
public function testHexColor()
{
$faker = Faker\Factory::create();
$color = $faker->hexColor;
$this->assertRegExp('/^#[a-f0-9]{6}$/i', $color);
}
public function testRgbColorAsArray()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColorAsArray;
$this->assertEquals(3, count($color));
}
public function testRgbColor()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColor();
$regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
$this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace Faker\Test\Provider;
use Faker;
class ColorTest extends \PHPUnit_Framework_TestCase
{
public function testHexColor()
{
$faker = Faker\Factory::create();
$color = $faker->hexColor;
$this->assertRegExp('/^#[a-f0-9]{6}$/i', $color);
}
public function testRgbColorAsArray()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColorAsArray;
$this->assertEquals(3, count($color));
}
public function testRgbColor()
{
$faker = Faker\Factory::create();
$color = $faker->rgbColor();
$regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])';
$this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color);
}
}