Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
return PhpCsFixer\Config::create()
->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');
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions AvroCaseBundle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CaseBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand Down
16 changes: 7 additions & 9 deletions DependencyInjection/AvroCaseExtension.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CaseBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* Process bundle configuration
* Process bundle configuration.
*
* @author Joris de Wit <joris.w.dewit@gmail.com>
*/
class AvroCaseExtension extends Extension
{
/**
* Load bundle config
* Load bundle config.
*
* @param array $configs Config array
* @param ContainerBuilder $container The container builder
Expand All @@ -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'));

Expand All @@ -43,4 +42,3 @@ public function load(array $configs, ContainerBuilder $container)
}
}
}

24 changes: 13 additions & 11 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Avro\CaseBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
* This is the class that validates and merges configuration from your app/config files.
*/
class Configuration
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
* Generates the configuration tree builder.
*
* @return NodeInterface
* @return TreeBuilder The tree builder
*/
public function getConfigTree()
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->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;
}
}
55 changes: 38 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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 }}
Expand Down
7 changes: 5 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
services:
avro_case.converter:
class: Avro\CaseBundle\Util\CaseConverter
_defaults:
autowire: true
autoconfigure: true
public: false

Avro\CaseBundle\Util\CaseConverter: ~
11 changes: 7 additions & 4 deletions Resources/config/twig.yml
Original file line number Diff line number Diff line change
@@ -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 }
27 changes: 13 additions & 14 deletions Tests/Twig/Extension/CaseExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

/**
/*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand All @@ -10,7 +10,7 @@
use Avro\CaseBundle\Twig\Extension\CaseExtension;

/**
* Test CaseExtension Class
* Test CaseExtension Class.
*
* @author Joris de Wit <joris.w.dewit@gmail.com>
*/
Expand All @@ -27,20 +27,20 @@ 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()
{
$this->assertSame('CaseExtension', $this->extension->getName());
}

/**
* @covers Avro\CaseBundle\Twig\Extension\CaseExtension::getFilters
* @covers \Avro\CaseBundle\Twig\Extension\CaseExtension::getFilters
*/
public function testGetFilters()
{
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
);
}

}
Loading