Skip to content

Commit

Permalink
Add Label Translator Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Nov 14, 2011
1 parent e1c73e8 commit 425f4bf
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Tests/Translator/NativeLabelTranslatorStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Tests\Translator;

use Sonata\AdminBundle\Translator\NativeLabelTranslatorStrategy;

class NativeTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
{
public function testLabel()
{
$strategy = new NativeLabelTranslatorStrategy;

$this->assertEquals('Is Valid', $strategy->getLabel('isValid'));
$this->assertEquals('Is Valid', $strategy->getLabel('is_Valid'));
$this->assertEquals('Is0 Valid', $strategy->getLabel('is0Valid'));
$this->assertEquals('Is Valid Super Cool', $strategy->getLabel('isValid_SuperCool'));
}
}
24 changes: 24 additions & 0 deletions Tests/Translator/NoopLabelTranslatorStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Tests\Translator;

use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;

class NoopLabelTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
{
public function testLabel()
{
$strategy = new NoopLabelTranslatorStrategy;

$this->assertEquals('isValid', $strategy->getLabel('isValid'));
$this->assertEquals('isValid_SuperCool', $strategy->getLabel('isValid_SuperCool'));
}
}
24 changes: 24 additions & 0 deletions Tests/Translator/UnderscoreLabelTranslatorStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Tests\Translator;

use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy;

class UnderscoreLabelTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
{
public function testLabel()
{
$strategy = new UnderscoreLabelTranslatorStrategy;

$this->assertEquals('label_is_valid', $strategy->getLabel('isValid'));
$this->assertEquals('label_is0_valid', $strategy->getLabel('is0Valid'));
}
}
21 changes: 21 additions & 0 deletions Translator/LabelTranslatorStrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of sonata-project.
*
* (c) 2010 Thomas Rabaix
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Translator;

interface LabelTranslatorStrategyInterface
{
/**
* @abstract
* @param $label
*/
function getLabel($label);
}
27 changes: 27 additions & 0 deletions Translator/NativeLabelTranslatorStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of sonata-project.
*
* (c) 2010 Thomas Rabaix
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Translator;

class NativeLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
{
/**
* @param string $label
* @return string
*/
public function getLabel($label)
{
$label = str_replace('_', ' ', $label);
$label = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label));

return ucwords(str_replace('_', ' ', $label));
}
}
24 changes: 24 additions & 0 deletions Translator/NoopLabelTranslatorStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of sonata-project.
*
* (c) 2010 Thomas Rabaix
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Translator;

class NoopLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
{
/**
* @param string $label
* @return string
*/
public function getLabel($label)
{
return $label;
}
}
24 changes: 24 additions & 0 deletions Translator/UnderscoreLabelTranslatorStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of sonata-project.
*
* (c) 2010 Thomas Rabaix
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Translator;

class UnderscoreLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
{
/**
* @param string $label
* @return string
*/
public function getLabel($label)
{
return sprintf('label_%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
}
}

0 comments on commit 425f4bf

Please sign in to comment.