needle-project / common
A common set of tools used in NeedleProject libraries
Installs: 1 358
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: >=5.5
- psr/log: 1.*
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: 2.*
README
README
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
composer require "needle-project/common"
Content
- WordType convertor - Converts string to
camelCase
,PascalCase
orsnake_case
- ArrayHelper - searches if an array has a key in depth and retrieves the value
- Error to Exception - convert php errors to Exceptions
- 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
<?php
require_once 'vendor/autoload.php';
use NeedleProject\Common\Convertor\CompoundWordConvertor;
echo CompoundWordConvertor::convertToPascalCase("Hello World") . "\n";
// HelloWorld
echo CompoundWordConvertor::convertToCamelCase("Hello World") . "\n";
// helloWorld
echo CompoundWordConvertor::convertToSnakeCase("Hello World") . "\n";
// hello_world
Known issues Converting an already converted type will fail. Example:
<?php
require_once 'vendor/autoload.php';
use NeedleProject\Common\Convertor\CompoundWordConvertor;
echo CompoundWordConvertor::convertToCamelCase("fooBar");
// will output "foobar", not fooBar
echo CompoundWordConvertor::convertToPascalCase("FooBar");
// will output "Foobar", not FooBar
echo CompoundWordConvertor::convertToSnakeCase("FOO BAR");
// will output "f_o_o_b_a_r", not "foo_bar"
2. ArrayHelper
Searches for a key in depth and retrieves it or state it's presense. Usage
<?php
require_once 'vendor/autoload.php';
use NeedleProject\Common\Helper\ArrayHelper;
$searchFor = ['level1', 'level2', 'level3'];
$searchIn = [
'level1' => [
'level2' => [
'level3' => 'A value'
]
]
];
$helper = new ArrayHelper();
if ($helper->hasKeysInDepth($searchIn, $searchFor)) {
echo $helper->getValueFromDepth($searchIn, $searchFor);
// A value
}
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.
<?php
require_once 'vendor/autoload.php';
use NeedleProject\Common\Util\ErrorToExceptionConverter;
class CustomException extends \Exception {
}
$convertor = new ErrorToExceptionConverter();
$convertor->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.
<?php
require_once 'vendor/autoload.php';
use NeedleProject\Common\ClassFinder;
$classFinder = new ClassFinder(
__DIR__ . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'fixtures',
\Fixture\BaseInterface::class
);
$foundClasses = $classFinder->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