Skip to content

Commit

Permalink
Relaxed ISO language code check
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jan 17, 2018
1 parent fee00be commit 459e088
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/mshoplib/src/MShop/Locale/Item/Language/Standard.php
Expand Up @@ -106,14 +106,17 @@ public function getCode()
*/
public function setCode( $key )
{
$matches = [];
$len = strlen( $key );
if( $len < 2 || $len > 5 || preg_match( '/^[a-z]{2,3}((-|_)[a-zA-Z]{2})?$/', $key ) !== 1 ) {

if( $len < 2 || $len > 5 || preg_match( '/^([a-zA-Z]{2,3})((-|_)([a-zA-Z]{2}))?$/', $key, $matches ) !== 1 ) {
throw new \Aimeos\MShop\Locale\Exception( sprintf( 'Invalid characters in ISO language code "%1$s"', $key ) );
}

if( (string) $key !== $this->getCode() )
{
$this->values['locale.language.code'] = (string) $key;
$code = strtolower( $matches[1] ) . ( isset( $matches[4] ) ? $matches[3] . $matches[4] : '' );
$this->values['locale.language.code'] = $code;
$this->modified = true;
}

Expand Down
22 changes: 21 additions & 1 deletion lib/mshoplib/tests/MShop/Locale/Item/Language/StandardTest.php
Expand Up @@ -87,10 +87,30 @@ public function testGetCode()
}


public function testSetCode()
{
$this->object->setCode( 'de' );
$this->assertEquals( 'de', $this->object->getCode() );

$this->object->setCode( 'DE' );
$this->assertEquals( 'de', $this->object->getCode() );
}


public function testSetCodeInvalid()
{
$this->setExpectedException( '\\Aimeos\\MShop\\Locale\\Exception' );
$this->object->setCode( 'XXX' );
$this->object->setCode( 'XXXX' );
}


public function testSetCodeCountry()
{
$this->object->setCode( 'de_DE' );
$this->assertEquals( 'de_DE', $this->object->getCode() );

$this->object->setCode( 'DE_DE' );
$this->assertEquals( 'de_DE', $this->object->getCode() );
}


Expand Down

0 comments on commit 459e088

Please sign in to comment.