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\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'));
|
||||||
|
@@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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