chevere/chevere

View on GitHub
src/Str/Str.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <rodolfo@chevere.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Chevere\Str;

use Chevere\Str\Interfaces\StrInterface;

final class Str implements StrInterface
{
    public function __construct(
        private string $string
    ) {
    }

    public function __toString(): string
    {
        return $this->string;
    }

    public function withLowercase(): StrInterface
    {
        $new = clone $this;
        $new->string = mb_strtolower($new->string, 'UTF-8');

        return $new;
    }

    public function withUppercase(): StrInterface
    {
        $new = clone $this;
        $new->string = mb_strtoupper($new->string, 'UTF-8');

        return $new;
    }

    public function withStripWhitespace(): StrInterface
    {
        $new = clone $this;
        $new->string = preg_replace('/\s+/', '', $new->string) ?? '';

        return $new;
    }

    public function withStripExtraWhitespace(): StrInterface
    {
        $new = clone $this;
        $new->string = preg_replace('/\s+/', ' ', $new->string) ?? '';

        return $new;
    }

    public function withStripNonAlphanumerics(): StrInterface
    {
        $new = clone $this;
        $new->string = preg_replace('/[^[:alnum:]]/u', '', $new->string) ?? '';

        return $new;
    }

    public function withForwardSlashes(): StrInterface
    {
        $new = clone $this;
        $new->string = str_replace('\\', '/', $new->string);

        return $new;
    }

    public function withLeftTail(string $tail): StrInterface
    {
        $new = clone $this;
        $new->string = $tail . ltrim($new->string, $tail);

        return $new;
    }

    public function withRightTail(string $tail): StrInterface
    {
        $new = clone $this;
        $new->string = rtrim($new->string, $tail) . $tail;

        return $new;
    }

    public function withReplaceFirst(string $search, string $replace): StrInterface
    {
        $new = clone $this;
        $pos = strpos($new->string, $search);
        if ($pos !== false) {
            $subject = substr_replace($new->string, $replace, $pos, strlen($search));
            $new->string = $subject;
        }

        return $new;
    }

    public function withReplaceLast(string $search, string $replace): StrInterface
    {
        $new = clone $this;
        $pos = strrpos($new->string, $search);
        if ($pos !== false) {
            $subject = substr_replace($new->string, $replace, $pos, strlen($search));
            $new->string = $subject;
        }

        return $new;
    }

    public function withReplaceAll(string $search, string $replace): StrInterface
    {
        $new = clone $this;
        $new->string = str_replace($search, $replace, $new->string);

        return $new;
    }

    public function withStripANSIColors(): StrInterface
    {
        $new = clone $this;
        $new->string = preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $new->string);

        return $new;
    }
}