From ecd58f02176dce18c170c2cd038d58f08ac02c55 Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 00:20:30 +0200 Subject: [PATCH 01/10] Added hexcolor, rgbcolor and rgbColorAsArray Changed readme, with the 3 new methods Added 3 new tests --- readme.md | 3 +++ src/Faker/Provider/Miscellaneous.php | 31 ++++++++++++++++++++++++++++ test/Faker/Provider/BaseTest.php | 23 +++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/readme.md b/readme.md index 260c3143..1f3e5ae6 100644 --- a/readme.md +++ b/readme.md @@ -154,6 +154,9 @@ 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` diff --git a/src/Faker/Provider/Miscellaneous.php b/src/Faker/Provider/Miscellaneous.php index 0c4fd32b..9b8b7d0a 100644 --- a/src/Faker/Provider/Miscellaneous.php +++ b/src/Faker/Provider/Miscellaneous.php @@ -148,4 +148,35 @@ class Miscellaneous extends \Faker\Provider\Base { return static::randomElement(static::$languageCode); } + + /** + * @example ' + */ + 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) + ); + } + + 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; + } + + public static function rgbColor() + { + return implode(',', static::rgbColorAsArray()); + } } diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index 8a53ad72..a2d906c3 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -3,6 +3,7 @@ namespace Faker\Test\Provider; use Faker\Provider\Base as BaseProvider; +use Faker; class BaseTest extends \PHPUnit_Framework_TestCase { @@ -124,4 +125,26 @@ 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(false); + $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; + $this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color); + } } From 6f7f40693c6288a019bc8d7748654dd446a492d1 Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 00:23:01 +0200 Subject: [PATCH 02/10] Forgot examples on the methods --- src/Faker/Provider/Miscellaneous.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Faker/Provider/Miscellaneous.php b/src/Faker/Provider/Miscellaneous.php index 9b8b7d0a..ad483152 100644 --- a/src/Faker/Provider/Miscellaneous.php +++ b/src/Faker/Provider/Miscellaneous.php @@ -150,7 +150,7 @@ class Miscellaneous extends \Faker\Provider\Base } /** - * @example ' + * @example '#fa3cc2' */ public static function hexColor() { @@ -165,6 +165,9 @@ class Miscellaneous extends \Faker\Provider\Base ); } + /** + * @example 'array(0,255,122)' + */ public static function rgbColorAsArray() { $rgb = array(); @@ -175,6 +178,9 @@ class Miscellaneous extends \Faker\Provider\Base return $rgb; } + /** + * @example '0,255,122' + */ public static function rgbColor() { return implode(',', static::rgbColorAsArray()); From ec0566a352a88b3cc31e9d81fefdc06acd9ecb01 Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 00:25:48 +0200 Subject: [PATCH 03/10] removed a leftover variable on rgbColor --- test/Faker/Provider/BaseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Faker/Provider/BaseTest.php b/test/Faker/Provider/BaseTest.php index a2d906c3..384433b6 100644 --- a/test/Faker/Provider/BaseTest.php +++ b/test/Faker/Provider/BaseTest.php @@ -143,7 +143,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase public function testRgbColor() { $faker = Faker\Factory::create(); - $color = $faker->rgbColor(false); + $color = $faker->rgbColor(); $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; $this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', $color); } From 99024404da7cffdfaceb2bb7b8cd4907ed2bdfcc Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 01:22:10 +0200 Subject: [PATCH 04/10] Moved all color methods to a provider, and added - safeHexColor - safeColorName - colorName --- readme.md | 11 ++- src/Faker/Provider/Color.php | 124 +++++++++++++++++++++++++++ src/Faker/Provider/Miscellaneous.php | 37 -------- test/Faker/Provider/BaseTest.php | 23 ----- test/Faker/Provider/ColorTest.php | 32 +++++++ 5 files changed, 164 insertions(+), 63 deletions(-) create mode 100644 src/Faker/Provider/Color.php create mode 100644 test/Faker/Provider/ColorTest.php diff --git a/readme.md b/readme.md index 1f3e5ae6..d95a07b8 100644 --- a/readme.md +++ b/readme.md @@ -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). diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php new file mode 100644 index 00000000..ff7b679d --- /dev/null +++ b/src/Faker/Provider/Color.php @@ -0,0 +1,124 @@ +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); - } } diff --git a/test/Faker/Provider/ColorTest.php b/test/Faker/Provider/ColorTest.php new file mode 100644 index 00000000..e9eb67bc --- /dev/null +++ b/test/Faker/Provider/ColorTest.php @@ -0,0 +1,32 @@ +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); + } + +} \ No newline at end of file From 9ce3d44ea260e9bc5260a674bdc616b01346444d Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 01:29:06 +0200 Subject: [PATCH 05/10] fixed unit test and changed arrays name to a better name --- src/Faker/Provider/Color.php | 8 ++++---- test/Faker/Provider/ColorTest.php | 14 ++++---------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php index ff7b679d..1b852383 100644 --- a/src/Faker/Provider/Color.php +++ b/src/Faker/Provider/Color.php @@ -16,13 +16,13 @@ class Color extends Base 'a', 'b', 'c', 'd', 'e', 'f' ); - protected static $oldBrowserNames = array( + protected static $safeColorNames = array( 'black', 'maroon', 'green', 'navy', 'olive', 'purple', 'teal', 'lime', 'blue', 'silver', 'gray', 'yellow', 'fuchsia', 'aqua', 'white' ); - protected static $newBrowserNames = array( + protected static $allColorNames = array( 'AliceBlue', 'AntiqueWhite', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque', 'Black', 'BlanchedAlmond', 'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue', @@ -110,7 +110,7 @@ class Color extends Base */ public static function safeColorName() { - return static::randomElement(static::$oldBrowserNames); + return static::randomElement(static::$safeColorNames); } /** @@ -118,7 +118,7 @@ class Color extends Base */ public static function colorName() { - return static::randomElement(static::$newBrowserNames); + return static::randomElement(static::$allColorNames); } } \ No newline at end of file diff --git a/test/Faker/Provider/ColorTest.php b/test/Faker/Provider/ColorTest.php index e9eb67bc..8010a0aa 100644 --- a/test/Faker/Provider/ColorTest.php +++ b/test/Faker/Provider/ColorTest.php @@ -2,31 +2,25 @@ namespace Faker\Test\Provider; -use Faker; +use Faker\Provider\Color; class ColorTest extends \PHPUnit_Framework_TestCase { public function testHexColor() { - $faker = Faker\Factory::create(); - $color = $faker->hexColor; - $this->assertRegExp('/^#[a-f0-9]{6}$/i', $color); + $this->assertRegExp('/^#[a-f0-9]{6}$/i', Color::hexColor()); } public function testRgbColorAsArray() { - $faker = Faker\Factory::create(); - $color = $faker->rgbColorAsArray; - $this->assertEquals(3, count($color)); + $this->assertEquals(3, count(Color::rgbColorAsArray())); } 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); + $this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', Color::rgbColor()); } } \ No newline at end of file From e088a2e625dc97ab7437dcf4e879d1cc1155f0ea Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 01:35:10 +0200 Subject: [PATCH 06/10] added rgbCssColor method --- readme.md | 1 + src/Faker/Provider/Color.php | 8 ++++++++ test/Faker/Provider/ColorTest.php | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/readme.md b/readme.md index d95a07b8..257b35a2 100644 --- a/readme.md +++ b/readme.md @@ -191,6 +191,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle hexcolor // '#fa3cc2' rgbcolor // '0,255,122' rgbColorAsArray // array(0,255,122) + rgbCssColor // 'rgb(0,255,122)' safeColorName // 'fuchsia' colorName // 'Gainsbor' diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php index 1b852383..ac0fad7b 100644 --- a/src/Faker/Provider/Color.php +++ b/src/Faker/Provider/Color.php @@ -105,6 +105,14 @@ class Color extends Base return implode(',', static::rgbColorAsArray()); } + /** + * @example 'rgb(0,255,122)' + */ + public static function rgbCssColor() + { + return 'rgb(' . static::rgbColor() . ')'; + } + /** * @example 'blue' */ diff --git a/test/Faker/Provider/ColorTest.php b/test/Faker/Provider/ColorTest.php index 8010a0aa..4f20ef34 100644 --- a/test/Faker/Provider/ColorTest.php +++ b/test/Faker/Provider/ColorTest.php @@ -23,4 +23,10 @@ class ColorTest extends \PHPUnit_Framework_TestCase $this->assertRegExp('/^' . $regexp . ',' . $regexp . ',' . $regexp . '$/i', Color::rgbColor()); } + public function testRgbCssColor() + { + $regexp = '([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'; + $this->assertRegExp('/^rgb\(' . $regexp . ',' . $regexp . ',' . $regexp . '\)$/i', Color::rgbCssColor()); + } + } \ No newline at end of file From 6d30f63245ba510b7cf09f9c3d91133beca424f5 Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 20:58:42 +0200 Subject: [PATCH 07/10] changed hexcolor selector from array to decimal for a bit faster query --- src/Faker/Provider/Color.php | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php index ac0fad7b..753e477f 100644 --- a/src/Faker/Provider/Color.php +++ b/src/Faker/Provider/Color.php @@ -11,11 +11,6 @@ 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 $safeColorNames = array( 'black', 'maroon', 'green', 'navy', 'olive', 'purple', 'teal', 'lime', 'blue', 'silver', @@ -57,12 +52,12 @@ class Color extends Base 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) + dechex(static::numberBetween(0,15)), + dechex(static::numberBetween(0,15)), + dechex(static::numberBetween(0,15)), + dechex(static::numberBetween(0,15)), + dechex(static::numberBetween(0,15)), + dechex(static::numberBetween(0,15)) ); } @@ -71,9 +66,9 @@ class Color extends Base */ public static function safeHexColor() { - $color1 = static::randomElement(static::$hexcodes); - $color2 = static::randomElement(static::$hexcodes); - $color3 = static::randomElement(static::$hexcodes); + $color1 = dechex(static::numberBetween(0,15)); + $color2 = dechex(static::numberBetween(0,15)); + $color3 = dechex(static::numberBetween(0,15)); return sprintf('#%s%s%s%s%s%s', $color1, $color1, From af3e337e1cfcbf3ae0bf83bba73e9d20c8c95c47 Mon Sep 17 00:00:00 2001 From: lsv Date: Tue, 18 Jun 2013 14:00:06 +0200 Subject: [PATCH 08/10] fixed hexcolor, down to 0.16 sec for 10.000 --- src/Faker/Provider/Color.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php index 753e477f..66f9dd85 100644 --- a/src/Faker/Provider/Color.php +++ b/src/Faker/Provider/Color.php @@ -51,14 +51,7 @@ class Color extends Base */ public static function hexColor() { - return sprintf('#%s%s%s%s%s%s', - dechex(static::numberBetween(0,15)), - dechex(static::numberBetween(0,15)), - dechex(static::numberBetween(0,15)), - dechex(static::numberBetween(0,15)), - dechex(static::numberBetween(0,15)), - dechex(static::numberBetween(0,15)) - ); + return '#' . dechex(mt_rand(1, 16777215)); } /** From ad3573e1046a7981b0d9623356fa66c069b2af02 Mon Sep 17 00:00:00 2001 From: lsv Date: Sat, 29 Jun 2013 10:10:58 +0200 Subject: [PATCH 09/10] changed safehex color --- src/Faker/Provider/Color.php | 25 ++++++++----------------- timer.php | 0 2 files changed, 8 insertions(+), 17 deletions(-) create mode 100644 timer.php diff --git a/src/Faker/Provider/Color.php b/src/Faker/Provider/Color.php index 66f9dd85..03c45809 100644 --- a/src/Faker/Provider/Color.php +++ b/src/Faker/Provider/Color.php @@ -59,17 +59,8 @@ class Color extends Base */ public static function safeHexColor() { - $color1 = dechex(static::numberBetween(0,15)); - $color2 = dechex(static::numberBetween(0,15)); - $color3 = dechex(static::numberBetween(0,15)); - return sprintf('#%s%s%s%s%s%s', - $color1, - $color1, - $color2, - $color2, - $color3, - $color3 - ); + $color = str_pad(dechex(mt_rand(0,255)), 3, '0', STR_PAD_LEFT); + return '#' . $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2]; } /** @@ -77,12 +68,12 @@ class Color extends Base */ 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; + $color = static::hexColor(); + return array( + hexdec(substr($color,1,2)), + hexdec(substr($color,3,2)), + hexdec(substr($color,5,2)) + ); } /** diff --git a/timer.php b/timer.php new file mode 100644 index 00000000..e69de29b From cba7a8e37aaf834b78a01f0f8602a5e33eb0a84d Mon Sep 17 00:00:00 2001 From: lsv Date: Sat, 29 Jun 2013 10:11:25 +0200 Subject: [PATCH 10/10] changed safehex color --- timer.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 timer.php diff --git a/timer.php b/timer.php deleted file mode 100644 index e69de29b..00000000