A clean, elegant, unicode-safe, fluent, immutable, localisable, dependency-free string manipulation library for PHP 7.1+

v2.0.3 2020-05-25 08:24 UTC

This package is auto-updated.

Last update: 2024-11-25 18:41:58 UTC


README

Editor

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads License

A clean, elegant, unicode-safe, fluent, immutable, localisable, dependency-free string manipulation library for PHP 7.1+

Installation

You can install the package via composer:

composer require placemat/editor

Usage

Create an instance

To use Editor, first you've got to instantiate it on your string:

echo Editor::create('hello world')->upperCase();

// HELLO WORLD

That's a little verbose though, so Editor comes with a global helper function too:

echo s('hello world')->upperCase();

// HELLO WORLD

Do some stuff!

Editor is fluent and chainable, so you can just keep adding operations to each other:

echo s('    EDITOR is pretty neat I guess 💩      ')
    ->trim()
    ->titleCase();
    
// Editor is Pretty Neat I Guess 💩

Editor is also immutable, so you won't mess up your original instance:

$str = s('Apple');

echo $str->plural(); // Apples

echo $str; // Apple

Editor implements PHP's __toString() magic method, so in most cases you can just use it like a string and it'll work just fine:

$str = s('worlds');

echo 'Hello ' . $str->singular()->upperCaseFirst();

// Hello World

If you do need to get a regular string back for whatever reason, you can either cast an instance of Editor, or call str():

$hello = s('Hello World');

(string) $hello; // Will be a string

$hello->str(); // Will also be a string

Title Casing

Editor implements proper title casing, based on John Gruber's crazy title casing script:

s('this is a headline that will be properly title-cased')->titleCase();

// This Is a Headline That Will Be Properly Title-Cased

If you're after what other libraries so boldly claim is title case, you want upperCaseFirst():

s('this is not actually title casing')->upperCaseFirst();

// This Is Not Actually Title-casing

Inflection

Editor supports making a string singular or plural:

s('apples')->singular(); // 'apple'

s('apple')->plural(); // 'apples'

// plural also has a $count parameter, in case you need to pluralize dynamically
s('apple')->plural($count); // 'apple' if $count is 1, 'apples' otherwise

Inflections are localizable in Editor. Right now, Editor supports 6 languages:

  • English ('en')
  • Spanish ('es')
  • French ('fr')
  • Portuguese ('pt')
  • Norwegian Bokmal ('nb')
  • Turkish ('tr')
s('bijou')->plural($count, 'fr'); // 'bijoux'

If you'd like to add an inflector, simply

  • Extend Hipsterjazzbo\Editor\Inflectors\Inflector
  • Register with Editor::registerInflector($inflector)
  • That's it!

If you do add inflectors, feel free to open a Pull Request!

Function Reference

Full function reference documentation is probably something I should write. But for now, you can peruse the main Editor class. Every method is doc-blocked and tested 👍