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..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. @@ -10,39 +10,60 @@ This bundle is listed on packagist. Simply add it to your apps composer.json file -``` js - "avro/case-bundle": "0.1.2" +```json + "avro/case-bundle2": "^0.4.0" ``` -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..d20eb69 100644 --- a/Tests/Util/CaseConverterTest.php +++ b/Tests/Util/CaseConverterTest.php @@ -1,6 +1,6 @@ assertEquals( @@ -123,8 +126,6 @@ public function testWordsToPascalCase() ); } - - public function testTitleToUnderscoreCase() { $this->assertEquals( @@ -169,50 +170,90 @@ public function testConvert() ); } - public function testArrayArguments() + public function testArrayArguments(): void { $this->assertEquals( - array( - 'underscore_case_format1', - 'underscore_case_format1', - ), - $this->converter->convert(array( - 'underscoreCaseFormat1', - 'underscoreCaseFormat1', - ), 'underscore') + [ + 'foo' => 'Title Case Format1', + 'bar' => [ + 'Title Case Format2', + 'Title Case Format3', + ], + 'baz' => 'Title Case Format4', + ], + $this->converter->convert( + [ + 'foo' => 'Title Case Format1', + 'bar' => [ + 'title_case_format2', + 'title case format3', + ], + 'baz' => 'titleCaseFormat4', + ], + 'title' + ) ); $this->assertEquals( - array( - 'camelCaseFormat1', - 'camelCaseFormat2', - ), - $this->converter->convert(array( - 'camel Case Format1', - 'camel Case Format2', - ), 'camel') + [ + 'foo' => 'underscore_case_format1', + 'bar' => [ + 'underscore_case_format2', + 'underscore_case_format3', + ], + ], + $this->converter->convert( + [ + 'foo' => 'underscoreCaseFormat1', + 'bar' => [ + 'underscore_case_format2', + 'Underscore Case Format3', + ], + ], + 'underscore' + ) ); $this->assertEquals( - array( - 'PascalCaseFormat1', - 'PascalCaseFormat2', - ), - $this->converter->convert(array( - 'pascal_case_format1', - 'pascal_case_format2', - ), 'pascal') + [ + 'foo' => 'camelCaseFormat1', + 'bar' => [ + 'camelCaseFormat2', + 'camelCaseFormat3', + ], + ], + $this->converter->convert( + [ + 'foo' => 'camelCaseFormat1', + 'bar' => [ + 'camel_case_format2', + 'camel Case Format3', + ], + ], + 'camel' + ) ); - $this->assertEquals(array( - 'Title Case Format1', - 'Title Case Format2', - ), - $this->converter->convert(array( - 'title case format1', - 'title case format2', - ), 'title') + $this->assertEquals( + [ + 'foo' => 'PascalCaseFormat1', + 'bar' => [ + 'PascalCaseFormat2', + 'PascalCaseFormat3', + ], + 'baz' => 'PascalCaseFormat4', + ], + $this->converter->convert( + [ + 'foo' => 'PascalCaseFormat1', + 'bar' => [ + 'pascal_case_format2', + 'pascal case format3', + ], + 'baz' => 'Pascal Case Format4', + ], + 'pascal' + ) ); } - public function testGetFormat() { $this->assertEquals( @@ -235,7 +276,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..e0c62c6 100644 --- a/Util/CaseConverter.php +++ b/Util/CaseConverter.php @@ -1,6 +1,8 @@ - * */ 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,118 +26,216 @@ class CaseConverter public function toUnderscoreCase($input) { if (is_array($input)) { - $result = array(); - foreach ($input as $val) { - $result[] = $this->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; } /** - * Converts a string into underscore format (e.g. first_name) + * Converts a string or array into title format (e.g. First Name). * * @param string|array $input String * - * @return string $input In underscore format + * @return string|array $result In title case format */ - private function convertToUnderscoreCase($input) + public function toTitleCase($input) { - $input = trim(lcfirst($input)); + if (is_array($input)) { + return array_map([$this, 'toTitleCase'], $input); + } + if (is_string($input)) { + return $this->convertToTitleCase($input); + } - // camel case - $input = preg_replace_callback('/([A-Z])/', create_function('$c', 'return "_" . strtolower($c[1]);'), $input); + return $input; + } - // title case - $input = str_replace(' ', '', $input); + /** + * Converts a string to camel case format (e.g. firstName). + * + * @param string|array $input String or array argument + * + * @return string|array $result In camel case format + */ + public function toCamelCase($input) + { + if (is_array($input)) { + return array_map([$this, 'toCamelCase'], $input); + } + if (is_string($input)) { + return $this->convertToCamelCase($input); + } return $input; } /** - * Converts a string or array into title format (e.g. First Name) + * Converts a string to pascal case format (e.g. FirstName). * - * @param string|array $input String + * @param string|array $input String or array argument * - * @return string|array $result In title case format + * @return string|array $result In pascal case format */ - public function toTitleCase($input) + public function toPascalCase($input) { if (is_array($input)) { - $result = array(); - foreach ($input as $val) { - $result[] = $this->convertToTitleCase($val); - } - } else { - $result = $this->convertToTitleCase($input); + return array_map([$this, 'toPascalCase'], $input); + } + if (is_string($input)) { + return $this->convertToPascalCase($input); } - return $result; + return $input; + } + + /** + * Convert string. + * + * @param string|array $input Input argument as string or array + * @param string|array $format Desired Case format + * + * @return string|array + */ + public function convert($input, $format = 'camel') + { + switch ($format) { + case 'pascal': + return $this->toPascalCase($input); + break; + case 'title': + return $this->toTitleCase($input); + break; + case 'underscore': + return $this->toUnderscoreCase($input); + break; + case 'camel': + default: + return $this->toCamelCase($input); + break; + } } /** - * Converts a string into title format (e.g. First Name) + * Get the format of a string. + * + * @param string $input The input string + * + * @return string The strings format + */ + public function getFormat(string $input): string + { + if (strpos($input, ' ')) { + return 'title'; + } + if (strpos($input, '_')) { + return 'underscore'; + } + if ($input[0] === strtoupper($input[0])) { + return 'pascal'; + } + + return 'camel'; + } + + /** + * 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) + private function convertToTitleCase(string $input): string { $input = ucfirst($input); // camelCase - $input = preg_replace_callback('/([A-Z])/', create_function('$c', 'return " " . ucfirst($c[1]);'), $input); + $input = preg_replace_callback( + '/([A-Z])/', + static function ($c) { + return ' '.ucfirst($c[1]); + }, + $input + ); // lowercase title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return " " . ucfirst($c[1]);'), $input); + $input = preg_replace_callback( + '/ ([a-z])/', + static function ($c) { + return ' '.ucfirst($c[1]); + }, + $input + ); // underscore - $input = preg_replace_callback('/_([a-z])/', create_function('$c', 'return " " . strtoupper($c[1]);'), $input); + $input = preg_replace_callback( + '/_([a-z])/', + static 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 into underscore format (e.g. first_name). * - * @param string|array $input String or array argument + * @param string $input String * - * @return string $result or array with values translated into camel case + * @return string $input In underscore format */ - public function toCamelCase($input) + private function convertToUnderscoreCase(string $input): string { - if (is_array($input)) { - $result = array(); - foreach ($input as $val) { - $result[] = $this->convertToCamelCase($val); - } - } else { - $result = $this->convertToCamelCase($input); - } + $input = lcfirst(trim($input)); + + // camel case + $input = preg_replace_callback( + '/([A-Z])/', + static function ($c) { + return '_'.strtolower($c[1]); + }, + $input + ); - return $result; + // title case + $input = str_replace(' ', '', $input); + + return $input; } /** - * Convert a string to camel case + * Convert a string to camel case. * * @param string $input Variable to convert * * @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])/', create_function('$c', 'return strtoupper($c[1]);'), $input); + $input = preg_replace_callback( + '/_([a-z])/', + static function ($c) { + return strtoupper($c[1]); + }, + $input + ); // title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); + $input = preg_replace_callback( + '/ ([a-z])/', + static function ($c) { + return strtoupper($c[1]); + }, + $input + ); $input = str_replace(' ', '', $input); @@ -144,92 +243,36 @@ private function convertToCamelCase($input) } /** - * Converts a string to pascal case format (e.g. FirstName) - * - * @param string|array $input String or array argument - * - * @return string|array $result translated into pascal case - */ - public function toPascalCase($input) - { - if (is_array($input)) { - $result = array(); - foreach ($input as $val) { - $result[] = $this->convertToPascalCase($val); - } - } else { - $result = $this->convertToPascalCase($input); - } - - return $result; - } - - /** - * 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 * * @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])/', create_function('$c', 'return strtoupper($c[1]);'), $input); + $input = preg_replace_callback( + '/_([a-z])/', + static function ($c) { + return strtoupper($c[1]); + }, + $input + ); // title - $input = preg_replace_callback('/ ([a-z])/', create_function('$c', 'return strtoupper($c[1]);'), $input); + $input = preg_replace_callback( + '/ ([a-z])/', + static function ($c) { + return strtoupper($c[1]); + }, + $input + ); $input = str_replace(' ', '', $input); return $input; } - - /** - * Convert string - * - * @param string|array $input Input argument as string or array - * @param string|array $format Desired Case format - * - * @return string - */ - public function convert($input, $format = 'camel') - { - switch($format) { - case 'camel': - return $this->toCamelCase($input); - break; - case 'pascal': - return $this->toPascalCase($input); - break; - case 'title': - return $this->toTitleCase($input); - break; - case 'underscore': - return $this->toUnderscoreCase($input); - break; - } - } - - /** - * Get the format of a string - * - * @param string $input The input string - * - * @return string The strings format - */ - public function getFormat($input) - { - if (strpos($input, ' ')) { - return 'title'; - } else if (strpos($input, '_')) { - return 'underscore'; - } else if ($input{0} === strtoupper($input{0})) { - return 'pascal'; - } else { - return 'camel'; - } - } - } diff --git a/composer.json b/composer.json index b891acc..013a697 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "avro/case-bundle", + "name": "avro/case-bundle2", "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", @@ -9,15 +9,29 @@ { "name": "Joris de Wit", "email": "joris.w.dewit@gmail.com" + }, + { + "name": "Steffen Roßkamp", + "email": "sr@blackblizzard.org" } ], "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\\": "" } + } }