mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-15 22:13:13 +00:00
Refactor code by adding camelizer utility
This commit is contained in:

committed by
Romain Neutron

parent
383a1b0c0c
commit
45fe839cfe
@@ -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'));
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
22
lib/Alchemy/Phrasea/Utilities/String/Camelizer.php
Normal file
22
lib/Alchemy/Phrasea/Utilities/String/Camelizer.php
Normal 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);
|
||||
}
|
||||
}
|
@@ -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),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user