From 73bbd29ea715c7f554ed8b3ec0b67ac845c41378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ro=C3=9Fkamp?= Date: Mon, 28 Dec 2015 15:13:38 +0100 Subject: [PATCH 1/3] Symfony 4, PHP 7.2+ and PHPUnit 5 compatibility * Fix/Add Symfony components * Fix deprecations under Symfony 4.2 * Use autowiring * Update README * Add UPGRADE --- .php_cs | 20 ++ .travis.yml | 16 +- AvroCaseBundle.php | 5 + DependencyInjection/AvroCaseExtension.php | 16 +- DependencyInjection/Configuration.php | 24 +- README.md | 49 ++-- Resources/config/services.yml | 7 +- Resources/config/twig.yml | 11 +- Tests/Twig/Extension/CaseExtensionTest.php | 27 ++- Tests/Util/CaseConverterTest.php | 73 +++--- Twig/Extension/CaseExtension.php | 28 +-- UPGRADE.md | 4 + Util/CaseConverter.php | 252 ++++++++++++--------- composer.json | 22 +- 14 files changed, 340 insertions(+), 214 deletions(-) create mode 100644 .php_cs create mode 100644 UPGRADE.md diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..a0f519b --- /dev/null +++ b/.php_cs @@ -0,0 +1,20 @@ +setRules( + [ + '@Symfony' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => [ + 'imports_order' => [ + 'class', + 'function', + 'const', + ], + 'sort_algorithm' => 'alpha', + ], + 'ordered_class_elements' => true, + 'phpdoc_order' => true, + 'header_comment' => ['header' => "For the full copyright and license information, please view the LICENSE\nfile that was distributed with this source code."], + ] + ) + ->setCacheFile(__DIR__.'/.php_cs.cache'); diff --git a/.travis.yml b/.travis.yml index 57c7da6..2550719 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,16 @@ language: php php: - - 5.3 - - 5.4 + - 7.1 + - 7.2 + - 7.3 -before_script: composer install --dev +before_install: + - | + # php.ini configuration + INI=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + phpenv config-rm xdebug.ini || echo "xdebug not available" + echo memory_limit = -1 >> $INI -notifications: - email: joris.w.dewit@gmail.com +install: + - composer install --no-interaction --prefer-dist diff --git a/AvroCaseBundle.php b/AvroCaseBundle.php index 55b7609..b8785e7 100644 --- a/AvroCaseBundle.php +++ b/AvroCaseBundle.php @@ -1,5 +1,10 @@ */ class AvroCaseExtension extends Extension { /** - * Load bundle config + * Load bundle config. * * @param array $configs Config array * @param ContainerBuilder $container The container builder @@ -32,7 +31,7 @@ public function load(array $configs, ContainerBuilder $container) $processor = new Processor(); $configuration = new Configuration(); - $config = $processor->process($configuration->getConfigTree(), $configs); + $config = $processor->processConfiguration($configuration, $configs); $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); @@ -43,4 +42,3 @@ public function load(array $configs, ContainerBuilder $container) } } } - diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 407bd6f..bf688b8 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -1,6 +1,6 @@ root('avro_case', 'array') + $treeBuilder = new TreeBuilder('avro_case'); + $rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('avro_case'); + + $rootNode ->children() - ->booleanNode('use_twig')->defaultValue(true)->cannotBeEmpty()->end() + ->booleanNode('use_twig')->defaultValue(true)->end() ->end() ->end(); - return $treeBuilder->buildTree(); + return $treeBuilder; } } diff --git a/README.md b/README.md index 4c2aaab..c67b453 100644 --- a/README.md +++ b/README.md @@ -10,39 +10,60 @@ This bundle is listed on packagist. Simply add it to your apps composer.json file -``` js +```json "avro/case-bundle": "0.1.2" ``` -Enable the bundle in the kernel: +Enable the bundle in config/bundles.php: -``` php -// app/AppKernel.php - - new Avro\CaseBundle\AvroCaseBundle +```php + Avro\CaseBundle\AvroCaseBundle::class => ['all' => true], ``` Configuration ------------- -``` yaml + +*Optional:* Add this config + +```yaml +# config/packages/avro_case.yaml avro_case: use_twig: false #disable the twig extension (true by default) ``` Usage ----- -``` php -$converter = $this->container->get('avro_case.converter'); +```php +use Avro\CaseBundle\Util\CaseConverter; + +class SomeClass +{ + private $caseConverter; + + /** + * @param CaseConverter $caseConverter + */ + public function __construct(CaseConverter $caseConverter) + { + $this->caseConverter = $caseConverter; + } -$camelCaseFormat = $converter->toCamelCase($str); -$pascalCaseFormat = $converter->toPascalCase($str); -$titleCaseFormat = $converter->toTitleCase($str); -$underscoreCaseFormat = $converter->toUnderscoreCase($str); + /** + * @param string $str + */ + public function foo(string $str) + { + $camelCaseFormat = $this->converter->toCamelCase($str); + $pascalCaseFormat = $this->converter->toPascalCase($str); + $titleCaseFormat = $this->converter->toTitleCase($str); + $underscoreCaseFormat = $this->converter->toUnderscoreCase($str); + } +} ``` The following filters are also available if you use Twig -``` jinja +```twig {{ var | camel }} {{ var | pascal }} {{ var | title }} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 770e4a9..46d15ff 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,4 +1,7 @@ services: - avro_case.converter: - class: Avro\CaseBundle\Util\CaseConverter + _defaults: + autowire: true + autoconfigure: true + public: false + Avro\CaseBundle\Util\CaseConverter: ~ diff --git a/Resources/config/twig.yml b/Resources/config/twig.yml index 8889af7..f709d2d 100644 --- a/Resources/config/twig.yml +++ b/Resources/config/twig.yml @@ -1,6 +1,9 @@ services: - avro_case.twig.extension: - class: Avro\CaseBundle\Twig\Extension\CaseExtension + _defaults: + autowire: true + autoconfigure: true + public: false + + Avro\CaseBundle\Twig\Extension\CaseExtension: tags: - - { name: twig.extension } - arguments: ["@avro_case.converter"] + - { name: twig.extension } diff --git a/Tests/Twig/Extension/CaseExtensionTest.php b/Tests/Twig/Extension/CaseExtensionTest.php index 757456c..a52f787 100644 --- a/Tests/Twig/Extension/CaseExtensionTest.php +++ b/Tests/Twig/Extension/CaseExtensionTest.php @@ -1,6 +1,6 @@ */ @@ -27,12 +27,12 @@ class CaseExtensionTest extends \PHPUnit_Framework_TestCase public function setup() { - $this->converter = $this->getMock('Avro\CaseBundle\Util\CaseConverter'); + $this->converter = $this->createMock('Avro\CaseBundle\Util\CaseConverter'); $this->extension = new CaseExtension($this->converter); } /** - * @covers Avro\CaseBundle\Twig\Extension\CaseExtension::getName + * @covers \Avro\CaseBundle\Twig\Extension\CaseExtension::getName */ public function testGetName() { @@ -40,7 +40,7 @@ public function testGetName() } /** - * @covers Avro\CaseBundle\Twig\Extension\CaseExtension::getFilters + * @covers \Avro\CaseBundle\Twig\Extension\CaseExtension::getFilters */ public function testGetFilters() { @@ -51,8 +51,8 @@ public function testGetFilters() public function testCamelCase() { $this->converter->expects($this->any()) - ->method('toCamelCase') - ->will($this->returnValue('camelCase')); + ->method('toCamelCase') + ->willReturn('camelCase'); $this->assertEquals( 'camelCase', $this->extension->toCamelCase('camel case') @@ -62,8 +62,8 @@ public function testCamelCase() public function testPascalCase() { $this->converter->expects($this->any()) - ->method('toPascalCase') - ->will($this->returnValue('PascalCase')); + ->method('toPascalCase') + ->will($this->returnValue('PascalCase')); $this->assertEquals( 'PascalCase', $this->extension->toPascalCase('pascal_case') @@ -73,8 +73,8 @@ public function testPascalCase() public function testTitleCase() { $this->converter->expects($this->any()) - ->method('toTitleCase') - ->will($this->returnValue('Title Case')); + ->method('toTitleCase') + ->will($this->returnValue('Title Case')); $this->assertEquals( 'Title Case', $this->extension->toTitleCase('title_case') @@ -84,12 +84,11 @@ public function testTitleCase() public function testUnderscoreCase() { $this->converter->expects($this->any()) - ->method('toUnderscoreCase') - ->will($this->returnValue('underscore_case')); + ->method('toUnderscoreCase') + ->will($this->returnValue('underscore_case')); $this->assertEquals( 'underscore_case', $this->extension->toUnderscoreCase('underscore case') ); } - } diff --git a/Tests/Util/CaseConverterTest.php b/Tests/Util/CaseConverterTest.php index 7a49300..e6b3847 100644 --- a/Tests/Util/CaseConverterTest.php +++ b/Tests/Util/CaseConverterTest.php @@ -1,6 +1,6 @@ assertEquals( @@ -123,8 +125,6 @@ public function testWordsToPascalCase() ); } - - public function testTitleToUnderscoreCase() { $this->assertEquals( @@ -172,47 +172,59 @@ public function testConvert() public function testArrayArguments() { $this->assertEquals( - array( + [ 'underscore_case_format1', 'underscore_case_format1', - ), - $this->converter->convert(array( - 'underscoreCaseFormat1', - 'underscoreCaseFormat1', - ), 'underscore') + ], + $this->converter->convert( + [ + 'underscoreCaseFormat1', + 'underscoreCaseFormat1', + ], + 'underscore' + ) ); $this->assertEquals( - array( + [ 'camelCaseFormat1', 'camelCaseFormat2', - ), - $this->converter->convert(array( - 'camel Case Format1', - 'camel Case Format2', - ), 'camel') + ], + $this->converter->convert( + [ + 'camel Case Format1', + 'camel Case Format2', + ], + 'camel' + ) ); $this->assertEquals( - array( + [ 'PascalCaseFormat1', 'PascalCaseFormat2', - ), - $this->converter->convert(array( - 'pascal_case_format1', - 'pascal_case_format2', - ), 'pascal') + ], + $this->converter->convert( + [ + 'pascal_case_format1', + 'pascal_case_format2', + ], + 'pascal' + ) ); - $this->assertEquals(array( + $this->assertEquals( + [ 'Title Case Format1', 'Title Case Format2', - ), - $this->converter->convert(array( - 'title case format1', - 'title case format2', - ), 'title') + ], + $this->converter->convert( + [ + 'title case format1', + 'title case format2', + ], + 'title' + ) ); } - public function testGetFormat() { $this->assertEquals( @@ -235,7 +247,4 @@ public function testGetFormat() $this->converter->getFormat('Title Case Format') ); } - - } - diff --git a/Twig/Extension/CaseExtension.php b/Twig/Extension/CaseExtension.php index 1568e4e..9d138eb 100644 --- a/Twig/Extension/CaseExtension.php +++ b/Twig/Extension/CaseExtension.php @@ -1,6 +1,6 @@ */ @@ -27,22 +27,22 @@ public function __construct(CaseConverter $caseConverter) } /** - * Get twig filters + * Get twig filters. * * @return array filters */ public function getFilters() { - return array( - new \Twig_SimpleFilter('camel', array($this, 'toCamelCase')), - new \Twig_SimpleFilter('pascal', array($this, 'toPascalCase')), - new \Twig_SimpleFilter('underscore', array($this, 'toUnderscoreCase')), - new \Twig_SimpleFilter('title', array($this, 'toTitleCase')) - ); + return [ + new \Twig_SimpleFilter('camel', [$this, 'toCamelCase']), + new \Twig_SimpleFilter('pascal', [$this, 'toPascalCase']), + new \Twig_SimpleFilter('underscore', [$this, 'toUnderscoreCase']), + new \Twig_SimpleFilter('title', [$this, 'toTitleCase']), + ]; } /** - * Convert string to camel case format + * Convert string to camel case format. * * @param string $input * @@ -54,7 +54,7 @@ public function toCamelCase($input) } /** - * Convert string to pascal case format + * Convert string to pascal case format. * * @param string $input * @@ -66,7 +66,7 @@ public function toPascalCase($input) } /** - * Convert string to underscore case format + * Convert string to underscore case format. * * @param string $input * @@ -78,7 +78,7 @@ public function toUnderscoreCase($input) } /** - * Convert string to title case format + * Convert string to title case format. * * @param string $input * @@ -90,7 +90,7 @@ public function toTitleCase($input) } /** - * Get twig extension name + * Get twig extension name. * * @return string */ diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..23898af --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,4 @@ +Changes in version 0.4 +---------------------- + +* ``CaseConverter::convertToTitleCase`` is now private, use ``toTitleCase`` instead diff --git a/Util/CaseConverter.php b/Util/CaseConverter.php index 93182ff..0e4cfc6 100644 --- a/Util/CaseConverter.php +++ b/Util/CaseConverter.php @@ -1,6 +1,6 @@ - * */ class CaseConverter { /** - * Converts a string into underscore format (e.g. first_name) + * Converts a string into underscore format (e.g. first_name). * * @param string|array $input String * @@ -25,7 +24,7 @@ class CaseConverter public function toUnderscoreCase($input) { if (is_array($input)) { - $result = array(); + $result = []; foreach ($input as $val) { $result[] = $this->convertToUnderscoreCase($val); } @@ -37,27 +36,7 @@ public function toUnderscoreCase($input) } /** - * Converts a string into underscore format (e.g. first_name) - * - * @param string|array $input String - * - * @return string $input In underscore format - */ - private function convertToUnderscoreCase($input) - { - $input = trim(lcfirst($input)); - - // camel case - $input = preg_replace_callback('/([A-Z])/', create_function('$c', 'return "_" . strtolower($c[1]);'), $input); - - // title case - $input = str_replace(' ', '', $input); - - return $input; - } - - /** - * Converts a string or array into title format (e.g. First Name) + * Converts a string or array into title format (e.g. First Name). * * @param string|array $input String * @@ -66,7 +45,7 @@ private function convertToUnderscoreCase($input) public function toTitleCase($input) { if (is_array($input)) { - $result = array(); + $result = []; foreach ($input as $val) { $result[] = $this->convertToTitleCase($val); } @@ -78,30 +57,7 @@ public function toTitleCase($input) } /** - * Converts a string into title format (e.g. First Name) - * - * @param string $input String - * - * @return string $input Translated into title format - */ - public function convertToTitleCase($input) - { - $input = ucfirst($input); - - // camelCase - $input = preg_replace_callback('/([A-Z])/', create_function('$c', 'return " " . ucfirst($c[1]);'), $input); - - // lowercase title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return " " . ucfirst($c[1]);'), $input); - - // underscore - $input = preg_replace_callback('/_([a-z])/', create_function('$c', 'return " " . strtoupper($c[1]);'), $input); - - return trim(preg_replace('/\s+/', ' ', $input)); - } - - /** - * Converts a string to camel case format (e.g. firstName) + * Converts a string to camel case format (e.g. firstName). * * @param string|array $input String or array argument * @@ -110,7 +66,7 @@ public function convertToTitleCase($input) public function toCamelCase($input) { if (is_array($input)) { - $result = array(); + $result = []; foreach ($input as $val) { $result[] = $this->convertToCamelCase($val); } @@ -122,29 +78,7 @@ public function toCamelCase($input) } /** - * Convert a string to camel case - * - * @param string $input Variable to convert - * - * @return string $input In Camel case format - */ - private function convertToCamelCase($input) - { - $input = lcfirst($input); - - // underscore - $input = preg_replace_callback('/_([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); - - // title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); - - $input = str_replace(' ', '', $input); - - return $input; - } - - /** - * Converts a string to pascal case format (e.g. FirstName) + * Converts a string to pascal case format (e.g. FirstName). * * @param string|array $input String or array argument * @@ -153,7 +87,7 @@ private function convertToCamelCase($input) public function toPascalCase($input) { if (is_array($input)) { - $result = array(); + $result = []; foreach ($input as $val) { $result[] = $this->convertToPascalCase($val); } @@ -165,29 +99,7 @@ public function toPascalCase($input) } /** - * Converts a string to pascal case format (e.g. FirstName) - * - * @param string|array $input String - * - * @return string $input translated into pascal case - */ - private function convertToPascalCase($input) - { - $input = ucfirst($input); - - // underscore - $input = preg_replace_callback('/_([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); - - // title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); - - $input = str_replace(' ', '', $input); - - return $input; - } - - /** - * Convert string + * Convert string. * * @param string|array $input Input argument as string or array * @param string|array $format Desired Case format @@ -196,7 +108,7 @@ private function convertToPascalCase($input) */ public function convert($input, $format = 'camel') { - switch($format) { + switch ($format) { case 'camel': return $this->toCamelCase($input); break; @@ -213,7 +125,7 @@ public function convert($input, $format = 'camel') } /** - * Get the format of a string + * Get the format of a string. * * @param string $input The input string * @@ -223,13 +135,147 @@ public function getFormat($input) { if (strpos($input, ' ')) { return 'title'; - } else if (strpos($input, '_')) { + } elseif (strpos($input, '_')) { return 'underscore'; - } else if ($input{0} === strtoupper($input{0})) { + } elseif ($input[0] === strtoupper($input[0])) { return 'pascal'; } else { return 'camel'; } } + /** + * Converts a string into title format (e.g. First Name). + * + * @param string $input String + * + * @return string $input Translated into title format + */ + private function convertToTitleCase($input) + { + $input = ucfirst($input); + + // camelCase + $input = preg_replace_callback( + '/([A-Z])/', + function ($c) { + return ' '.ucfirst($c[1]); + }, + $input + ); + + // lowercase title + $input = preg_replace_callback( + '/ ([a-z])/', + function ($c) { + return ' '.ucfirst($c[1]); + }, + $input + ); + + // underscore + $input = preg_replace_callback( + '/_([a-z])/', + function ($c) { + return ' '.strtoupper($c[1]); + }, + $input + ); + + return trim(preg_replace('/\s+/', ' ', $input)); + } + + /** + * Converts a string into underscore format (e.g. first_name). + * + * @param string|array $input String + * + * @return string $input In underscore format + */ + private function convertToUnderscoreCase($input) + { + $input = trim(lcfirst($input)); + + // camel case + $input = preg_replace_callback( + '/([A-Z])/', + function ($c) { + return '_'.strtolower($c[1]); + }, + $input + ); + + // title case + $input = str_replace(' ', '', $input); + + return $input; + } + + /** + * Convert a string to camel case. + * + * @param string $input Variable to convert + * + * @return string $input In Camel case format + */ + private function convertToCamelCase($input) + { + $input = lcfirst($input); + + // underscore + $input = preg_replace_callback( + '/_([a-z])/', + function ($c) { + return strtoupper($c[1]); + }, + $input + ); + + // title + $input = preg_replace_callback( + '/ ([a-z])/', + function ($c) { + return strtoupper($c[1]); + }, + $input + ); + + $input = str_replace(' ', '', $input); + + return $input; + } + + /** + * Converts a string to pascal case format (e.g. FirstName). + * + * @param string|array $input String + * + * @return string $input translated into pascal case + */ + private function convertToPascalCase($input) + { + $input = ucfirst($input); + + // underscore + $input = preg_replace_callback( + '/_([a-z])/', + function ($c) { + return strtoupper($c[1]); + }, + $input + ); + + // title + $input = preg_replace_callback( + '/ ([a-z])/', + function ($c) { + return strtoupper($c[1]); + }, + $input + ); + + $input = str_replace(' ', '', $input); + + return $input; + } } diff --git a/composer.json b/composer.json index b891acc..0de6859 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "avro/case-bundle", "type": "symfony-bundle", - "description": "Symfony2 Case Converter Bundle", + "description": "Symfony Case Converter Bundle", "keywords": ["pascal", "title", "camel", "converter"], "homepage": "http://github.com/jdewit/AvroCaseBundle", "license": "MIT", @@ -12,12 +12,22 @@ } ], "minimum-stability": "dev", + "extra": { + "branch-alias": { + "dev-master": "0.4-dev" + } + }, "require": { - "php": ">=5.3.2", - "twig/twig": "^1.12.0" + "php": "^7.1.3", + "twig/twig": "^1.12.0 || ^2.0" }, - "autoload": { - "psr-0": { "Avro\\CaseBundle": "" } + "require-dev": { + "phpunit/phpunit": "^5.7", + "symfony/config": "~4.0", + "symfony/dependency-injection": "~4.0", + "symfony/http-kernel": "~4.0" }, - "target-dir": "Avro/CaseBundle" + "autoload": { + "psr-4": { "Avro\\CaseBundle\\": "" } + } } From afe02cc912f76cb64f5909146128a3ad1b855504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ro=C3=9Fkamp?= Date: Wed, 17 Jul 2019 11:26:33 +0200 Subject: [PATCH 2/3] Enforce type checks, allow deep array conversion --- Tests/Util/CaseConverterTest.php | 73 +++++++++++++++------- Util/CaseConverter.php | 103 +++++++++++++++---------------- 2 files changed, 101 insertions(+), 75 deletions(-) diff --git a/Tests/Util/CaseConverterTest.php b/Tests/Util/CaseConverterTest.php index e6b3847..d20eb69 100644 --- a/Tests/Util/CaseConverterTest.php +++ b/Tests/Util/CaseConverterTest.php @@ -8,8 +8,9 @@ namespace Avro\CaseBundle\Tests\Util; use Avro\CaseBundle\Util\CaseConverter; +use PHPUnit_Framework_TestCase; -class CaseConverterTest extends \PHPUnit_Framework_TestCase +class CaseConverterTest extends PHPUnit_Framework_TestCase { /** * @var CaseConverter @@ -169,58 +170,86 @@ public function testConvert() ); } - public function testArrayArguments() + public function testArrayArguments(): void { $this->assertEquals( [ - 'underscore_case_format1', - 'underscore_case_format1', + 'foo' => 'Title Case Format1', + 'bar' => [ + 'Title Case Format2', + 'Title Case Format3', + ], + 'baz' => 'Title Case Format4', ], $this->converter->convert( [ - 'underscoreCaseFormat1', - 'underscoreCaseFormat1', + 'foo' => 'Title Case Format1', + 'bar' => [ + 'title_case_format2', + 'title case format3', + ], + 'baz' => 'titleCaseFormat4', ], - 'underscore' + 'title' ) ); $this->assertEquals( [ - 'camelCaseFormat1', - 'camelCaseFormat2', + 'foo' => 'underscore_case_format1', + 'bar' => [ + 'underscore_case_format2', + 'underscore_case_format3', + ], ], $this->converter->convert( [ - 'camel Case Format1', - 'camel Case Format2', + 'foo' => 'underscoreCaseFormat1', + 'bar' => [ + 'underscore_case_format2', + 'Underscore Case Format3', + ], ], - 'camel' + 'underscore' ) ); $this->assertEquals( [ - 'PascalCaseFormat1', - 'PascalCaseFormat2', + 'foo' => 'camelCaseFormat1', + 'bar' => [ + 'camelCaseFormat2', + 'camelCaseFormat3', + ], ], $this->converter->convert( [ - 'pascal_case_format1', - 'pascal_case_format2', + 'foo' => 'camelCaseFormat1', + 'bar' => [ + 'camel_case_format2', + 'camel Case Format3', + ], ], - 'pascal' + 'camel' ) ); $this->assertEquals( [ - 'Title Case Format1', - 'Title Case Format2', + 'foo' => 'PascalCaseFormat1', + 'bar' => [ + 'PascalCaseFormat2', + 'PascalCaseFormat3', + ], + 'baz' => 'PascalCaseFormat4', ], $this->converter->convert( [ - 'title case format1', - 'title case format2', + 'foo' => 'PascalCaseFormat1', + 'bar' => [ + 'pascal_case_format2', + 'pascal case format3', + ], + 'baz' => 'Pascal Case Format4', ], - 'title' + 'pascal' ) ); } diff --git a/Util/CaseConverter.php b/Util/CaseConverter.php index 0e4cfc6..e0c62c6 100644 --- a/Util/CaseConverter.php +++ b/Util/CaseConverter.php @@ -1,5 +1,7 @@ convertToUnderscoreCase($val); - } - } else { - $result = $this->convertToUnderscoreCase($input); + return array_map([$this, 'toUnderscoreCase'], $input); + } + if (is_string($input)) { + return $this->convertToUnderscoreCase($input); } - return $result; + return $input; } /** @@ -45,15 +45,13 @@ public function toUnderscoreCase($input) public function toTitleCase($input) { if (is_array($input)) { - $result = []; - foreach ($input as $val) { - $result[] = $this->convertToTitleCase($val); - } - } else { - $result = $this->convertToTitleCase($input); + return array_map([$this, 'toTitleCase'], $input); + } + if (is_string($input)) { + return $this->convertToTitleCase($input); } - return $result; + return $input; } /** @@ -61,20 +59,18 @@ public function toTitleCase($input) * * @param string|array $input String or array argument * - * @return string $result or array with values translated into camel case + * @return string|array $result In camel case format */ public function toCamelCase($input) { if (is_array($input)) { - $result = []; - foreach ($input as $val) { - $result[] = $this->convertToCamelCase($val); - } - } else { - $result = $this->convertToCamelCase($input); + return array_map([$this, 'toCamelCase'], $input); + } + if (is_string($input)) { + return $this->convertToCamelCase($input); } - return $result; + return $input; } /** @@ -82,20 +78,18 @@ public function toCamelCase($input) * * @param string|array $input String or array argument * - * @return string|array $result translated into pascal case + * @return string|array $result In pascal case format */ public function toPascalCase($input) { if (is_array($input)) { - $result = []; - foreach ($input as $val) { - $result[] = $this->convertToPascalCase($val); - } - } else { - $result = $this->convertToPascalCase($input); + return array_map([$this, 'toPascalCase'], $input); + } + if (is_string($input)) { + return $this->convertToPascalCase($input); } - return $result; + return $input; } /** @@ -104,14 +98,11 @@ public function toPascalCase($input) * @param string|array $input Input argument as string or array * @param string|array $format Desired Case format * - * @return string + * @return string|array */ public function convert($input, $format = 'camel') { switch ($format) { - case 'camel': - return $this->toCamelCase($input); - break; case 'pascal': return $this->toPascalCase($input); break; @@ -121,6 +112,10 @@ public function convert($input, $format = 'camel') case 'underscore': return $this->toUnderscoreCase($input); break; + case 'camel': + default: + return $this->toCamelCase($input); + break; } } @@ -131,17 +126,19 @@ public function convert($input, $format = 'camel') * * @return string The strings format */ - public function getFormat($input) + public function getFormat(string $input): string { if (strpos($input, ' ')) { return 'title'; - } elseif (strpos($input, '_')) { + } + if (strpos($input, '_')) { return 'underscore'; - } elseif ($input[0] === strtoupper($input[0])) { + } + if ($input[0] === strtoupper($input[0])) { return 'pascal'; - } else { - return 'camel'; } + + return 'camel'; } /** @@ -151,14 +148,14 @@ public function getFormat($input) * * @return string $input Translated into title format */ - private function convertToTitleCase($input) + private function convertToTitleCase(string $input): string { $input = ucfirst($input); // camelCase $input = preg_replace_callback( '/([A-Z])/', - function ($c) { + static function ($c) { return ' '.ucfirst($c[1]); }, $input @@ -167,7 +164,7 @@ function ($c) { // lowercase title $input = preg_replace_callback( '/ ([a-z])/', - function ($c) { + static function ($c) { return ' '.ucfirst($c[1]); }, $input @@ -176,7 +173,7 @@ function ($c) { // underscore $input = preg_replace_callback( '/_([a-z])/', - function ($c) { + static function ($c) { return ' '.strtoupper($c[1]); }, $input @@ -188,18 +185,18 @@ function ($c) { /** * Converts a string into underscore format (e.g. first_name). * - * @param string|array $input String + * @param string $input String * * @return string $input In underscore format */ - private function convertToUnderscoreCase($input) + private function convertToUnderscoreCase(string $input): string { - $input = trim(lcfirst($input)); + $input = lcfirst(trim($input)); // camel case $input = preg_replace_callback( '/([A-Z])/', - function ($c) { + static function ($c) { return '_'.strtolower($c[1]); }, $input @@ -218,14 +215,14 @@ function ($c) { * * @return string $input In Camel case format */ - private function convertToCamelCase($input) + private function convertToCamelCase(string $input): string { $input = lcfirst($input); // underscore $input = preg_replace_callback( '/_([a-z])/', - function ($c) { + static function ($c) { return strtoupper($c[1]); }, $input @@ -234,7 +231,7 @@ function ($c) { // title $input = preg_replace_callback( '/ ([a-z])/', - function ($c) { + static function ($c) { return strtoupper($c[1]); }, $input @@ -252,14 +249,14 @@ function ($c) { * * @return string $input translated into pascal case */ - private function convertToPascalCase($input) + private function convertToPascalCase(string $input): string { $input = ucfirst($input); // underscore $input = preg_replace_callback( '/_([a-z])/', - function ($c) { + static function ($c) { return strtoupper($c[1]); }, $input @@ -268,7 +265,7 @@ function ($c) { // title $input = preg_replace_callback( '/ ([a-z])/', - function ($c) { + static function ($c) { return strtoupper($c[1]); }, $input From 72868e033f393bc5625fec6ebe1a31bd541e426d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Ro=C3=9Fkamp?= Date: Wed, 17 Jul 2019 11:32:06 +0200 Subject: [PATCH 3/3] Rename package, update README --- README.md | 6 +++--- composer.json | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c67b453..a259d9a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -AvroCaseBundle [![Build Status](https://travis-ci.org/jdewit/AvroCaseBundle.png?branch=master)](https://travis-ci.org/jdewit/AvroCaseBundle) +AvroCaseBundle [![Build Status](https://travis-ci.org/MisatoTremor/AvroCaseBundle.png?branch=master)](https://travis-ci.org/MisatoTremor/AvroCaseBundle) -------------- -Convert strings or an array of strings to different case formats. +Convert strings or strings in arrays to different case formats. Supports: camelCase, PascalCase, Title Case, and underscore_case. @@ -11,7 +11,7 @@ This bundle is listed on packagist. Simply add it to your apps composer.json file ```json - "avro/case-bundle": "0.1.2" + "avro/case-bundle2": "^0.4.0" ``` Enable the bundle in config/bundles.php: diff --git a/composer.json b/composer.json index 0de6859..013a697 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "avro/case-bundle", + "name": "avro/case-bundle2", "type": "symfony-bundle", "description": "Symfony Case Converter Bundle", "keywords": ["pascal", "title", "camel", "converter"], @@ -9,6 +9,10 @@ { "name": "Joris de Wit", "email": "joris.w.dewit@gmail.com" + }, + { + "name": "Steffen Roßkamp", + "email": "sr@blackblizzard.org" } ], "minimum-stability": "dev",