PHP code example of omaressaouaf / text-morph

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

    

omaressaouaf / text-morph example snippets


use Omaressaouaf\TextMorph\Contracts\TextMorpher;

interface TextMorpher
{
    public function morph(string $text): string;

    public function unmorph(string $text): string;
}

use Omaressaouaf\TextMorph\Morphers\RotationalTextMorpher;

$morpher = new RotationalTextMorpher(3);

$morphed = $morpher->morph('Meet at 9pm!');
// Phhw dw 9sp!

$original = $morpher->unmorph($morphed);
// Meet at 9pm!

$morpher = new RotationalTextMorpher(1);

$morpher->morph('a'); // b
$morpher->morph('z'); // B
$morpher->morph('Z'); // a

use Omaressaouaf\TextMorph\Morphers\SubstitutionTextMorpher;

$morpher = new SubstitutionTextMorpher(['ab', 'cd']);

$morpher->morph('aabbcc'); // bbaacc
$morpher->morph('adam');   // bcbm

$morpher->unmorph('bbaacc'); // aabbcc

use Omaressaouaf\TextMorph\Morphers\TranspositionTextMorpher;

$morpher = new TranspositionTextMorpher(3);

$morpher->morph('HELLO');       // HOELL
$morpher->morph('Meet at 9pm!'); // M 9eta p!etm

use Omaressaouaf\TextMorph\Morphers\CompositeTextMorpher;
use Omaressaouaf\TextMorph\Morphers\RotationalTextMorpher;
use Omaressaouaf\TextMorph\Morphers\SubstitutionTextMorpher;
use Omaressaouaf\TextMorph\Morphers\TranspositionTextMorpher;

$pipeline = new CompositeTextMorpher();

$pipeline->add(new SubstitutionTextMorpher(['ae', 'io']));
$pipeline->add(new RotationalTextMorpher(5));
$pipeline->add(new TranspositionTextMorpher(3));

$original = 'Meet at 9pm!';

$morphed = $pipeline->morph($original);
$restored = $pipeline->unmorph($morphed);

// $restored === $original

namespace App\Morphers;

use Omaressaouaf\TextMorph\Contracts\TextMorpher;

class ReverseTextMorpher implements TextMorpher
{
    public function morph(string $text): string
    {
        return strrev($text);
    }

    public function unmorph(string $text): string
    {
        return strrev($text);
    }
}

use App\Morphers\ReverseTextMorpher;
use Omaressaouaf\TextMorph\Morphers\CompositeTextMorpher;
use Omaressaouaf\TextMorph\Morphers\RotationalTextMorpher;

$pipeline = new CompositeTextMorpher();
$pipeline->add(new ReverseTextMorpher());
$pipeline->add(new RotationalTextMorpher(13));

$morphed = $pipeline->morph('Hello');
$original = $pipeline->unmorph($morphed);