PHP code example of monospice / spicy-identifier-tools

1. Go to this page and download the library: Download monospice/spicy-identifier-tools 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/ */

    

monospice / spicy-identifier-tools example snippets


$identifierParts = Parser::parseFromUnderscore('an_identifier_name');
// returns array('an', 'identifier', 'name');

echo Formatter::formatCamelCase($identifierParts);
// 'anIdentifierName'

echo Converter::convert(
    'an_identifier_name',
    CaseFormat::UNDERSCORE,
    CaseFormat::CAMEL_CASE
);
// 'anIdentifierName'

use Monospice\SpicyIdentifiers\Tools\CaseFormat;
use Monospice\SpicyIdentifiers\Tools\Parser;
use Monospice\SpicyIdentifiers\Tools\Formatter;
use Monospice\SpicyIdentifiers\Tools\Converter;

Parser::parseFromCamelCase('anIdentifier');     // array('an', 'Identifier');
Parser::parseFromUnderscore('an_identifier');   // array('an', 'identifier');
Parser::parseFromHyphen('an-identifier');       // array('an', 'identifier');

// or use the generic method:
Parser::parse('anIdentifier', CaseFormat::CAMEL_CASE);
// array('an', 'Identifier');

$parts = array('an', 'identifier');

Formatter::formatUppercase($parts);         // 'ANIDENTIFIER'
Formatter::formatLowercase($parts);         // 'anidentifier'
Formatter::formatCamelCase($parts);         // 'anIdentifier'
Formatter::formatUpperCamelCase($parts);    // 'AnIdentifier'
Formatter::formatUnderscore($parts);        // 'an_identifier'
Formatter::formatUpperUnderscore($parts);   // 'An_Identifier'
Formatter::formatCapsUnderscore($parts);    // 'AN_IDENTIFIER'
Formatter::formatHyphen($parts);            // 'an-identifier'
Formatter::formatUpperHyphen($parts);       // 'An-Identifier'
Formatter::formatCapsHyphen($parts);        // 'AN-IDENTIFIER'

// or use the generic method:
Formatter::format($parts, CaseFormat::CAPS_UNDERSCORE); // AN_IDENTIFIER

// Converter::convert($partsArray, $inputFormat, $outputFormat);

Converter::convert(
    'anIdentifierName',
    CaseFormat::CAMEL_CASE,
    CaseFormat::UPPER_CAMEL_CASE
);
// 'AnIdentifierName'

$parts = Parser::parseFromCamelCase('XMLHttpRequest');
// array('XML', 'Http', 'Request');

Formatter::formatCamelCase($parts);             // 'xmlHttpRequest'
Formatter::formatCamelCaseWithAcronyms($parts); // 'XMLHttpRequest'

// Parser::parseFromMixedCase($identiferString, $arrayOfCases);

Parser::parseFromMixedCase('aMixed_case-identifier', [
    CaseFormat::CAMEL_CASE,
    CaseFormat::UNDERSCORE,
    CaseFormat::HYPHEN,
]);
// array('a', 'Mixed', 'case', 'identifier');

// From the php.net manual:
$täyte = 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.

Parser::parseFromCamelCaseExtended('änÏdentifierNáme');
// array('än', 'Ïdentifier', 'Náme');