Refactor code by adding camelizer utility

This commit is contained in:
Nicolas Le Goff
2013-03-27 15:06:47 +01:00
committed by Romain Neutron
parent 383a1b0c0c
commit 45fe839cfe
4 changed files with 70 additions and 23 deletions

View File

@@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Core\Provider;
use Silex\Application; use Silex\Application;
use Silex\ServiceProviderInterface; use Silex\ServiceProviderInterface;
use Alchemy\Phrasea\Utilities\String\Camelizer;
// write tests // write tests
class RegistrationServiceProvider implements ServiceProviderInterface class RegistrationServiceProvider implements ServiceProviderInterface
@@ -21,17 +22,10 @@ class RegistrationServiceProvider implements ServiceProviderInterface
{ {
$app['registration.fields'] = $app->share(function (Application $app){ $app['registration.fields'] = $app->share(function (Application $app){
if($app['phraseanet.configuration']->has('registration-fields')) { if($app['phraseanet.configuration']->has('registration-fields')) {
return array_map(function($field) { $camelizer = new Camelizer();
$chunks = explode('-', $field['name']);
if(count($chunks) > 1) { return array_map(function($field) use ($camelizer) {
$transformedName = ''; $field['name'] = $camelizer->camelize($field['name'], '-');
foreach($chunks as $chunk) {
$transformedName .= ucfirst($chunk);
}
$field['name'] = lcfirst($transformedName);
}
return $field; return $field;
}, $app['phraseanet.configuration']->get('registration-fields')); }, $app['phraseanet.configuration']->get('registration-fields'));

View File

@@ -11,8 +11,20 @@
namespace Alchemy\Phrasea\Twig; namespace Alchemy\Phrasea\Twig;
use Alchemy\Phrasea\Utilities\String\Camelizer;
class Camelize extends \Twig_Extension class Camelize extends \Twig_Extension
{ {
/**
* @var Camelizer
*/
private $camelizer;
public function __construct(Camelizer $camelizer = null)
{
$this->camelizer = $camelizer ?: new Camelizer();
}
/** /**
* *
* @return string * @return string
@@ -33,19 +45,8 @@ class Camelize extends \Twig_Extension
); );
} }
public function toCamelCase($property, $separator = '-') public function toCamelCase($str, $separator = '-', $pascalCase = false)
{ {
$properties = explode($separator, $property); return $this->camelizer->camelize($str, $separator, $pascalCase);
if(count($properties) > 1) {
$transformedProperty = "";
foreach($properties as $chunk) {
$transformedProperty .= ucfirst($chunk);
}
$property = lcfirst($transformedProperty);
}
return $property;
} }
} }

View File

@@ -0,0 +1,22 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Utilities\String;
class Camelizer
{
public function camelize($str, $separator = '_', $pascalCase = false)
{
$transformStr = str_replace(' ', '', ucwords(str_replace($separator, ' ', $str)));
return $pascalCase ? $transformStr : lcfirst($transformStr);
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Alchemy\Tests\Phrasea\Utilities\String;
use Alchemy\Phrasea\Utilities\String\Camelizer;
class CamelizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideStrings
* @covers Alchemy\Phrasea\Utilities\String\Camelizer::camelize
*/
public function testCamelize($string, $separator, $expected, $pascalize)
{
$camelizer = new Camelizer();
$result = $camelizer->camelize($string, $separator, $pascalize);
$this->assertEquals($expected, $result);
}
public function provideStrings()
{
return array(
array('string-test', '-', 'stringTest', false),
array('string test', ' ', 'stringTest', false),
array('string_test', '_', 'stringTest', false),
array('string#test', '#', 'StringTest', true),
);
}
}