Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Added Emoji to Miscellaneous #1175

Merged
merged 7 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
countryCode // UK
languageCode // en
currencyCode // EUR
emoji // 😁

### `Faker\Provider\Biased`

Expand Down
44 changes: 41 additions & 3 deletions src/Faker/Provider/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@

class Miscellaneous extends Base
{
/**
* @link https://en.wikipedia.org/wiki/Emoji#Unicode_blocks
* On date of 2017-03-26
*
* U+1F600 - U+1F637 as their UTF-8 Pairings
*/
protected static $emoji = array(
'\uD83D\uDE00', '\uD83D\uDE01', '\uD83D\uDE02', '\uD83D\uDE03',
'\uD83D\uDE04', '\uD83D\uDE05', '\uD83D\uDE06', '\uD83D\uDE07',
'\uD83D\uDE08', '\uD83D\uDE09', '\uD83D\uDE0A', '\uD83D\uDE0B',
'\uD83D\uDE0C', '\uD83D\uDE0D', '\uD83D\uDE0E', '\uD83D\uDE0F',
'\uD83D\uDE10', '\uD83D\uDE11', '\uD83D\uDE12', '\uD83D\uDE13',
'\uD83D\uDE14', '\uD83D\uDE15', '\uD83D\uDE16', '\uD83D\uDE17',
'\uD83D\uDE18', '\uD83D\uDE19', '\uD83D\uDE1A', '\uD83D\uDE1B',
'\uD83D\uDE1C', '\uD83D\uDE1D', '\uD83D\uDE1E', '\uD83D\uDE1F',
'\uD83D\uDE20', '\uD83D\uDE21', '\uD83D\uDE22', '\uD83D\uDE23',
'\uD83D\uDE24', '\uD83D\uDE25', '\uD83D\uDE26', '\uD83D\uDE27',
'\uD83D\uDE28', '\uD83D\uDE29', '\uD83D\uDE2A', '\uD83D\uDE2B',
'\uD83D\uDE2C', '\uD83D\uDE2D', '\uD83D\uDE2E', '\uD83D\uDE2F',
'\uD83D\uDE30', '\uD83D\uDE31', '\uD83D\uDE32', '\uD83D\uDE33',
'\uD83D\uDE34', '\uD83D\uDE35', '\uD83D\uDE36', '\uD83D\uDE37',
);

/**
* @link https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
* On date of 2016-04-22
Expand Down Expand Up @@ -198,14 +221,16 @@ class Miscellaneous extends Base
'SHP', 'SLL', 'SOS', 'SRD', 'STD', 'SVC', 'SYP', 'SZL', 'THB', 'TJS',
'TMT', 'TND', 'TOP', 'TRY', 'TTD', 'TWD', 'TZS', 'UAH', 'UGX', 'USD',
'UYU', 'UZS', 'VEF', 'VND', 'VUV', 'WST', 'XAF', 'XAG', 'XAU', 'XCD',
'XDR', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMK', 'ZMW', 'ZWL'
'XDR', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMK', 'ZMW', 'ZWL',
);

/**
* Return a boolean, true or false
* Return a boolean, true or false.
*
* @param int $chanceOfGettingTrue Between 0 (always get false) and 100 (always get true)
*
* @param integer $chanceOfGettingTrue Between 0 (always get false) and 100 (always get true).
* @return bool
*
* @example true
*/
public static function boolean($chanceOfGettingTrue = 50)
Expand Down Expand Up @@ -247,6 +272,7 @@ public static function locale()

/**
* @example 'FR'
*
* @link https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
*/
public static function countryCode()
Expand All @@ -256,6 +282,7 @@ public static function countryCode()

/**
* @example 'FRA'
*
* @link https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
*/
public static function countryISOAlpha3()
Expand All @@ -273,10 +300,21 @@ public static function languageCode()

/**
* @example 'EUR'
*
* @link https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
*/
public static function currencyCode()
{
return static::randomElement(static::$currencyCode);
}

/**
* Returns an encoded Unicode Character between U+1F600 and U+1F637.
*
* @link https://en.wikipedia.org/wiki/Emoji#Unicode_blocks
*/
public static function emoji()
{
return json_decode('"' . static::randomElement(static::$emoji) . '"');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use json_decode for that? Why not store unicode directly in the static::$emoji source? That would be faster.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that in my original version 62b87b5 , however the "\u{xxxx}" notation wasn't added to PHP until 7.0, so it failed the 5.x Travis builds.

After research this was apparently the best method to store unicode characters in PHP pre-7.0

}
}
6 changes: 5 additions & 1 deletion test/Faker/Provider/MiscellaneousTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class MiscellaneousTest extends \PHPUnit_Framework_TestCase
{

public function testBoolean()
{
$this->assertContains(Miscellaneous::boolean(), array(true, false));
Expand Down Expand Up @@ -51,4 +50,9 @@ public function testCurrencyCode()
{
$this->assertRegExp('/^[A-Z]{3}$/', Miscellaneous::currencyCode());
}

public function testEmoji()
{
$this->assertRegExp('/^[\x{1F600}-\x{1F636}]$/u', Miscellaneous::emoji());
}
}