PHP code example of ykw / cruet

1. Go to this page and download the library: Download ykw/cruet 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/ */

    

ykw / cruet example snippets


use Ykw\Cruet\Inflector;

// Case conversions
echo Inflector::toCamelCase('user_account');         // userAccount
echo Inflector::toPascalCase('user_account');        // UserAccount  
echo Inflector::toSnakeCase('UserAccount');          // user_account
echo Inflector::toScreamingSnakeCase('UserAccount'); // USER_ACCOUNT
echo Inflector::toKebabCase('UserAccount');          // user-account

// Format detection
var_dump(Inflector::isCamelCase('userAccount'));     // true
var_dump(Inflector::isPascalCase('UserAccount'));    // true
var_dump(Inflector::isSnakeCase('user_account'));    // true

// Fluent interface
$result = Inflector::convert('user_account')
    ->pascalCase()      // UserAccount
    ->snakeCase()       // user_account
    ->screamingSnakeCase() // USER_ACCOUNT
    ->get();
echo $result; // USER_ACCOUNT

use Ykw\Cruet\Inflector;

$input = 'user_profile_settings';

// All available case conversions
echo Inflector::toCamelCase($input);           // userProfileSettings
echo Inflector::toPascalCase($input);          // UserProfileSettings  
echo Inflector::toSnakeCase($input);           // user_profile_settings
echo Inflector::toScreamingSnakeCase($input);  // USER_PROFILE_SETTINGS
echo Inflector::toKebabCase($input);           // user-profile-settings

// Check string formats
var_dump(Inflector::isCamelCase('userAccount'));        // true
var_dump(Inflector::isPascalCase('UserAccount'));       // true
var_dump(Inflector::isSnakeCase('user_account'));       // true
var_dump(Inflector::isScreamingSnakeCase('USER_ACCOUNT')); // true
var_dump(Inflector::isKebabCase('user-account'));       // true

use Ykw\Cruet\Inflector;

// Complex transformation chains
$result = Inflector::convert('user_account')
    ->pascalCase()       // UserAccount
    ->snakeCase()        // user_account  
    ->screamingSnakeCase() // USER_ACCOUNT
    ->get();

echo $result; // USER_ACCOUNT

// Another example
$result = Inflector::convert('AdminUser')
    ->snakeCase()        // admin_user
    ->kebabCase()        // admin-user
    ->get();
    
echo $result; // admin-user