vantoozz/strings

OOP Strings Library

v1.0.0-rc1 2020-06-21 19:06 UTC

This package is auto-updated.

Last update: 2024-03-15 23:52:42 UTC


README

True OOP library for strings manipulation

Build Status Coverage Status Codacy Badge Packagist

The goal of the library is providing an OOP way for strings manipulations. It works with any object implementing Stringable interface. https://wiki.php.net/rfc/stringable.

Setup

Just run

composer require vantoozz/strings

Transformations

<?php declare(strict_types=1);

use Vantoozz\Strings\Transforms\Acronym;
use Vantoozz\Strings\Transforms\CaseToggled;
use Vantoozz\Strings\Transforms\Reversed;
use Vantoozz\Strings\Transforms\SnakeCased;

use function Vantoozz\Strings\str;

require_once __DIR__ . '/vendor/autoload.php';

$string = str('PHP: Hypertext Preprocessor');

echo new Acronym($string) . PHP_EOL;
echo new CaseToggled($string) . PHP_EOL;
echo new Reversed($string) . PHP_EOL;
echo new SnakeCased($string) . PHP_EOL;

Will output

PHP
php: hYPERTEXT pREPROCESSOR
rossecorperP txetrepyH :PHP
p_h_p:_hypertext_preprocessor

Available transformations

  • Acronym
  • CamelCased
  • CaseToggled
  • KebabCased
  • LowerCased
  • PascalCased
  • Reversed
  • SentenceCased
  • SnakeCased
  • TitleCased
  • UpperCased

Joins

<?php declare(strict_types=1);

use Vantoozz\Strings\Joins\EndingWith;
use Vantoozz\Strings\Joins\Joined;
use Vantoozz\Strings\Joins\StartingWith;

use function Vantoozz\Strings\str;

require_once __DIR__ . '/vendor/autoload.php';

$one = str('aabbc');
$two = str('ccddaa');

echo new Joined($one, $two) . PHP_EOL;
echo new EndingWith($one, $two) . PHP_EOL;
echo new StartingWith($one, $two) . PHP_EOL;

Will output

aabbcccddaa
aabbccddaa
ccddaabbc

Available joins

  • Joined
  • StartingWith
  • EndingWith

Formats

<?php declare(strict_types=1);

use Vantoozz\Strings\Exceptions\InvalidFormatException;
use Vantoozz\Strings\Formats\Email;

use function Vantoozz\Strings\str;

require_once __DIR__ . '/vendor/autoload.php';


try {
    echo new Email(str('user@example.com')) . PHP_EOL;
} catch (InvalidFormatException $e) {
    echo $e->getMessage() . PHP_EOL;
}

try {
    echo new Email(str('user%example.com')) . PHP_EOL;
} catch (InvalidFormatException $e) {
    echo $e->getMessage() . PHP_EOL;
}

Will output

user@example.com
Invalid format

Available formats

  • Email
  • Hostname
  • Ipv4
  • Ipv6
  • Mac
  • Sha1
  • Sha256
  • Url

Composition

<?php declare(strict_types=1);

use Vantoozz\Strings\Formats\Email;
use Vantoozz\Strings\Joins\EndingWith;
use Vantoozz\Strings\Transforms\CaseToggled;

use function Vantoozz\Strings\str;


require_once __DIR__ . '/vendor/autoload.php';

$username = str('User@');
$domain = str('@Example.Com');

echo new CaseToggled(new Email(new EndingWith($username, $domain))) . PHP_EOL;

Will output

uSER@eXAMPLE.cOM

Testing

./vendor/bin/phpunit