Giter Site home page Giter Site logo

case-converter's Introduction

CaseConverter

Package for advanced case conversion

It allows to convert arbitrary strings to specific cases.

Supported cases:

  • camelCase
  • PascalCase
  • kebab-case
  • snake_case
  • human case

Installation:

composer require maxon755/case-converter

Usage:

String conversion:

    CaseConverter::string('some String')->toCamel();    //'someString'
    
    CaseConverter::string('some String')->toPascal();   //'SomeString'
    
    CaseConverter::string('some String')->toKebab();    //'some-string'
    
    CaseConverter::string('some String')->toSnake();    //'some_string'
    
    CaseConverter::string('some String')->toHuman();    //'some string'

Array conversion:

Also arrays can be converted:

    CaseConverter::array([
        'not_converted_key' => 'converted_value'
    ])->toCamel(); 


    // return: [
    //    'not_converted_key' => 'convertedValue'
    // ]

By default only array values will be converted. You can specify it explicitly, using values() method:

    CaseConverter::array($someArray)->values()->toCamel();

Or you can convert only keys of array:

    CaseConverter::array($someArray)->keys()->toCamel();

Or both keys and values:

    CaseConverter::array($someArray)->both()->toCamel();

Nested array conversion:

If given array is nested CaseConverter will recursively convert it's items.

You can limit depth of recursion using depth(int $depth) method:

note: recursion level count starts from 0.

    $someArray = [
        'zero level array',
        [
            'first level array'
        ]
    ];
        

    CaseConverter::array($someArray)->values()->depth(0)->toCamel();

    // return:
    // [
    //     'zeroLevelArray', // converted
    //     [
    //         'first level array' // not converted
    //     ]
    // ]

Custom converters:

If supported functionality is not enough for your need you can define your own converter and pass it to withConverter() method.

  1. Callable

You can pass you converter as callable, that takes string as argument and return modified string:

    CaseConverter::string('1-2-3-4')->withConverter(function ($string) {
        return str_replace('-', '*', $string);
    }); //'1*2*3*4'
  1. Converter class

You can create your own class, that will implement CaseConverter\Interfaces\Converter interface and pass instance of this object to withConverter() method.

    class AsteriskConverter implements Converter
    {
        public function convert($string)
        {
            return str_replace('-', '*', $string);
        }
    }
    
    CaseConverter::string('1-2-3-4')
        ->withConverter(new AsteriskConverter()); //'1*2*3*4'

case-converter's People

Contributors

maxon755 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.