Strings
True OOP library for strings manipulation, (*1)
, (*2)
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., (*3)
Setup
Just run, (*4)
composer require vantoozz/strings
<?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, (*5)
PHP
php: hYPERTEXT pREPROCESSOR
rossecorperP txetrepyH :PHP
p_h_p:_hypertext_preprocessor
- 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, (*6)
aabbcccddaa
aabbccddaa
ccddaabbc
Available joins
- Joined
- StartingWith
- EndingWith
<?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, (*7)
user@example.com
Invalid format
- 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, (*8)
uSER@eXAMPLE.cOM
Testing
./vendor/bin/phpunit