PHP code example of needle-project / common

1. Go to this page and download the library: Download needle-project/common library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

needle-project / common example snippets



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


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"


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
}


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();


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
// )