diff --git a/lib/Alchemy/Phrasea/Core/Provider/RegistrationServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/RegistrationServiceProvider.php index ad0bf7dc61..2e234c863f 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/RegistrationServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/RegistrationServiceProvider.php @@ -13,6 +13,7 @@ namespace Alchemy\Phrasea\Core\Provider; use Silex\Application; use Silex\ServiceProviderInterface; +use Alchemy\Phrasea\Utilities\String\Camelizer; // write tests class RegistrationServiceProvider implements ServiceProviderInterface @@ -21,17 +22,10 @@ class RegistrationServiceProvider implements ServiceProviderInterface { $app['registration.fields'] = $app->share(function (Application $app){ if($app['phraseanet.configuration']->has('registration-fields')) { - return array_map(function($field) { - $chunks = explode('-', $field['name']); + $camelizer = new Camelizer(); - if(count($chunks) > 1) { - $transformedName = ''; - foreach($chunks as $chunk) { - $transformedName .= ucfirst($chunk); - } - - $field['name'] = lcfirst($transformedName); - } + return array_map(function($field) use ($camelizer) { + $field['name'] = $camelizer->camelize($field['name'], '-'); return $field; }, $app['phraseanet.configuration']->get('registration-fields')); diff --git a/lib/Alchemy/Phrasea/Twig/Camelize.php b/lib/Alchemy/Phrasea/Twig/Camelize.php index 3d93446b14..3d2b524ccb 100644 --- a/lib/Alchemy/Phrasea/Twig/Camelize.php +++ b/lib/Alchemy/Phrasea/Twig/Camelize.php @@ -11,8 +11,20 @@ namespace Alchemy\Phrasea\Twig; +use Alchemy\Phrasea\Utilities\String\Camelizer; + class Camelize extends \Twig_Extension { + /** + * @var Camelizer + */ + private $camelizer; + + public function __construct(Camelizer $camelizer = null) + { + $this->camelizer = $camelizer ?: new Camelizer(); + } + /** * * @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); - - if(count($properties) > 1) { - $transformedProperty = ""; - foreach($properties as $chunk) { - $transformedProperty .= ucfirst($chunk); - } - - $property = lcfirst($transformedProperty); - } - - return $property; + return $this->camelizer->camelize($str, $separator, $pascalCase); } } diff --git a/lib/Alchemy/Phrasea/Utilities/String/Camelizer.php b/lib/Alchemy/Phrasea/Utilities/String/Camelizer.php new file mode 100644 index 0000000000..ad6ed66047 --- /dev/null +++ b/lib/Alchemy/Phrasea/Utilities/String/Camelizer.php @@ -0,0 +1,22 @@ +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), + ); + } +}