From ecd58f02176dce18c170c2cd038d58f08ac02c55 Mon Sep 17 00:00:00 2001 From: lsv Date: Mon, 17 Jun 2013 00:20:30 +0200 Subject: [PATCH] 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); + } }