Skip to content

Commit

Permalink
Merge pull request #5 from cmtatro85/format
Browse files Browse the repository at this point in the history
Format method for creating a string in a certain format.
  • Loading branch information
tomhv committed Mar 14, 2016
2 parents 1478c0f + 1fc4eb5 commit 53502f8
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace Phospr;

use InvalidArgumentException;

/**
* A Value Object representation of 5 character locale string:
*
Expand Down Expand Up @@ -145,4 +147,87 @@ public static function fromCountrySlashLanguage($countrySlashLanguage)

return new static($segments[1], $segments[0]);
}

/**
* From countryCode/languageCode
*
* The use of this code is as follows:
* $locale = Locale::fromString('se_FI');
* 'SE_fi' == $locale->format('%L_%c');
* 'FI/se' == $locale->format('%C/%s');
* 'fi/se' == $locale->format('%c/%s');
* 'fi\se' == $locale->format('%c\\\%s');
*
* Current translattable codes are:
* * %L For uppercase language code
* * %l For lowercase language code
* * %C For uppercase country code
* * %c For lowercase country code
* * any other combination of %{:char:} will throw an
* Invalid argument exception unless the % is escaped with \
* * To get a \ You will need to double escape ( \\\ )
*
* @author Christopher Tatro <c.m.tatro@gmail.com>
* @since 1.0.0
*
* @param string $format
*
* @return string
*/
public function format($format)
{
if (!is_string($format)) {
throw new InvalidArgumentException(sprintf(
'format passed must be a string'
));
}

$translateNext = false;
$escNext = false;
$formatted = '';
foreach (str_split($format) as $char) {
if ($escNext) {
$formatted .= $char;
$escNext = false;
continue;
}

if ($translateNext) {
switch ($char) {
case 'c':
$translated = strtolower($this->getCountryCode());
break;
case 'C':
$translated = strtoupper($this->getCountryCode());
break;
case 'l':
$translated = strtolower($this->getLanguageCode());
break;
case 'L':
$translated = strtoupper($this->getLanguageCode());
break;
default:
throw new InvalidArgumentException(sprintf(
'Unkown format'
));
}

$formatted .= $translated;
$translateNext = false;
continue;
}

if ('\\' == $char) {
$escNext = true;
continue;
} elseif ('%' == $char) {
$translateNext = true;
continue;
}

$formatted .= $char;
}

return $formatted;
}
}
75 changes: 75 additions & 0 deletions tests/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,79 @@ public function testIsSameValueAs()
$this->assertTrue($locale->isSameValueAs($other));
$this->assertFalse($locale->isSameValueAs($yetAnother));
}

/**
* Test format
*
* @author Christopher Tatro <c.m.tatro@gmail.com>
* @since 1.0.0
*
* @dataProvider formatProvider
*/
public function testFormat($locale, $format, $expected)
{
$this->assertSame($expected, $locale->format($format));
}

/**
* format Provider
*
* @author Christopher Tatro <c.m.tatro@gmail.com>
* @since 1.0.0
*
* @return array
*/
public static function formatProvider()
{
return [
[Locale::fromString('en_us'), '%c_%L', 'us_EN'],
[Locale::fromString('en_CA'), '%C_%L', 'CA_EN'],
[Locale::fromString('en_US'), '%c_%l', 'us_en'],
[Locale::fromString('EN_US'), '%c/%L', 'us/EN'],
[Locale::fromString('Es_mx'), '%C\%%L', 'MX%ES'],
[Locale::fromString('eN_uS'), '%C\%L', 'US%L'],
[Locale::fromString('en_us'), '%C\\\%L', 'US\EN'],
[Locale::fromString('en_Gb'), '%C\\:%L', 'GB:EN'],
[Locale::fromString('ru_RU'), '%C:%L', 'RU:RU'],
[Locale::fromString('en_us'), '%C\:%L', 'US:EN']
];
}

/**
* Test formatException
*
* @author Christopher Tatro <c.m.tatro@gmail.com>
* @since 1.0.0
*
* @expectedException InvalidArgumentException
*
* @dataProvider formatExceptionProvider
*/
public function testFormatException($locale, $format)
{
$this->assertSame($locale->format($format));
}

/**
* formatException Provider
*
* @author Christopher Tatro <c.m.tatro@gmail.com>
* @since 1.0.0
*
* @return array
*/
public static function formatExceptionProvider()
{
return [
[Locale::fromString('ru_RU'), '%S_%L'],
[Locale::fromString('en_us'), '%C_%r'],
[Locale::fromString('en_us'), '%Cr%a'],
[Locale::fromString('en_us'), '%c_%J'],
[Locale::fromString('en_us'), '%\%/%L'],
[Locale::fromString('en_GB'), '%\%/%L'],
[Locale::fromString('en_AU'), '%\%/%L'],
[Locale::fromString('en_us'), 123432],
[Locale::fromString('en_us'), ['can', 'do', 'this']]
];
}
}

0 comments on commit 53502f8

Please sign in to comment.