From 07630d51a7d4ffe31e3d39e8fa3b106055290dad Mon Sep 17 00:00:00 2001 From: Adrian Tilita Date: Tue, 10 Jul 2018 12:28:15 +0300 Subject: [PATCH] Add word convertor --- README.md | 137 +++++++++++++++--- src/Convertor/CompoundWordConvertor.php | 61 ++++++++ tests/Convertor/CompoundWordConvertorTest.php | 82 +++++++++++ 3 files changed, 262 insertions(+), 18 deletions(-) create mode 100644 src/Convertor/CompoundWordConvertor.php create mode 100644 tests/Convertor/CompoundWordConvertorTest.php diff --git a/README.md b/README.md index 1376fa6..007250f 100644 --- a/README.md +++ b/README.md @@ -7,31 +7,132 @@ # README -## What is FileIo? -Common is a library packed with small re-usable utilities and helpers +## What is Common? +Common is a library packed with small re-usable utilities and helpers. ## Requirments For larger usage, minimum version is PHP 5.5 ## Instalation -Create/add to composer.json -``` -{ - "require": { - "needle-project/common": "dev-master" - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/needle-project/common" - } - ] +``` +composer require "needle-project/common" +``` +## Content +1. WordType convertor - Converts string to `camelCase`, `PascalCase` or `snake_case` +2. ArrayHelper - searches if an array has a key in depth and retrieves the value +3. Error to Exception - convert php errors to Exceptions +4. ClassFinder - Searches for classes of a certain type. + + +### 1. WordType convertor +Converts a set of strings of typographical conventions between them. +It handles `camelCase`, `PascalCase` and `snake_case`. +**Usage** +``` + [ + 'level2' => [ + 'level3' => 'A value' + ] + ] +]; + +$helper = new ArrayHelper(); +if ($helper->hasKeysInDepth($searchIn, $searchFor)) { + echo $helper->getValueFromDepth($searchIn, $searchFor); + // A value } ``` -Afterwards run `composer install` or `composer update` depending on the context. -## 1.2. Cloning repository using git +### 3. Error to Exception +Converts a PHP error to an Exception. +Error level constans are the PHP's default ones that can be found [here](http://php.net/manual/ro/function.error-reporting.php "here"). ``` -git clone https://github.com/needle-project/common.git +convertErrorsToExceptions(E_ALL, CustomException::class); + +try { + print(a); +} catch (\Exception $e) { + echo get_class($e) . "\n"; + echo $e->getMessage(); +} + +// restore the previous state of error handling +$convertor->restoreErrorHandler(); +``` +### 4. ClassFinder +Searches for a class of a certain sub-type. ``` -Run `composer install` in the library path. +findClasses(); + +print_r($foundClasses); +php test.php +// Array +// ( +// [0] => Fixture\Path\ClassList\BazClass +// [1] => Fixture\Path\ClassList\GodClass +// [2] => Fixture\Path\FooClass +// ) +``` +## Contribute +Feel free to contribute: +- State ideeas +- Open pull request with improvements/bug-fixes \ No newline at end of file diff --git a/src/Convertor/CompoundWordConvertor.php b/src/Convertor/CompoundWordConvertor.php new file mode 100644 index 0000000..6a54125 --- /dev/null +++ b/src/Convertor/CompoundWordConvertor.php @@ -0,0 +1,61 @@ + + * @copyright 2018 Adrian Tilita + * @license https://opensource.org/licenses/MIT MIT Licence + */ +class CompoundWordConvertor +{ + /** + * Convert any string to camelCase + * @param string $word + * @return string + */ + public static function convertToCamelCase($word) + { + return lcfirst(self::convertToPascalCase($word)); + } + + /** + * Convert any string to PascalCase + * @param string $word + * @return string + */ + public static function convertToPascalCase($word) + { + // separate into block for easier readability + $word = str_replace('_', ' ', $word); + $word = strtolower($word); + $word = ucwords($word); + return str_replace(' ', '', $word); + } + + /** + * Convert any string to underscore_string (snake_case) + * @param string $word + * @return string + */ + public static function convertToSnakeCase($word) + { + // append an _ to all capital letters. Ex: Abc will be converted to _Abc + $word = preg_replace('/(?assertEquals( + $expectedOutput, + CompoundWordConvertor::convertToCamelCase($input) + ); + } + + /** + * @dataProvider providePascalCaseScenarios + * @param $input + * @param $expectedOutput + */ + public function testConvertToPascalCase($input, $expectedOutput) + { + $this->assertEquals( + $expectedOutput, + CompoundWordConvertor::convertToPascalCase($input) + ); + } + + /** + * @dataProvider provideSnakeCaseScenarios + * @param $input + * @param $expectedOutput + */ + public function testConvertToSnakeCase($input, $expectedOutput) + { + $this->assertEquals( + $expectedOutput, + CompoundWordConvertor::convertToSnakeCase($input) + ); + } + + public function provideCamelCaseScenarios() + { + return [ + ['foo_bar', 'fooBar'], + ['foo_a', 'fooA'], + ['FOOO', 'fooo'], + ['fOoOoOo BaRrRr', 'fooooooBarrrr'], + ['Hello World', 'helloWorld'] + // @needs a fix: ['fooBar', 'fooBar'] + ]; + } + + public function providePascalCaseScenarios() + { + return [ + ['foo_bar', 'FooBar'], + ['foo_a', 'FooA'], + ['FOOO', 'Fooo'], + ['fOoOoOo BaRrRr', 'FooooooBarrrr'], + ['Hello World', 'HelloWorld'] + // @needs a fix: ['FooBar', 'FooBar'] + ]; + } + + public function provideSnakeCaseScenarios() + { + return [ + ['FooBar', 'foo_bar'], + ['fooBarBaz', 'foo_bar_baz'], + ['foo_bar', 'foo_bar'], + ['Hello World', 'hello_world'] + // @needs a fix: ['FOO BAR', 'foo_bar'], + // @needs a fix: ['SimpleXML', 'simple_xml'] + ]; + } +}