simklee/string-buffer

A string buffer to manipulate strings object orientated with fluent interface and method chaining.

1.0.8 2022-11-25 13:54 UTC

This package is auto-updated.

Last update: 2024-09-25 18:22:23 UTC


README

A small library to manipulate strings object orientated with fluent interface and method chaining.

Installation

composer require simklee/string-buffer

Usage

use Simklee\LaravelStringBuffer\StringBuffer;

// Sample with append methods
$buffer = new StringBuffer();
$buffer->append('some string')
    ->appendIf($condition, 'true', 'false')
    ->appendIfNot($condition, 'false', 'true')
    ->appendFormatted('%s, %s', 'LastName', 'FirstName')
    ->appendIfNull($isNull, 'is null', 'is NOT null')
    ->appendIfNotNull($isNotNull, 'is NOT null', 'is null')
    ->appendImplode(['a', 'b', 'c'], ',');
echo $buffer->toString();

// Sample with prepend methods
echo StringBuffer::create('some string')
    ->prepend('some string')
    ->prependIf($condition, 'true', 'false')
    ->prependIfNot($condition, 'false', 'true')
    ->prependFormatted('%s, %s', 'LastName', 'FirstName')
    ->prependIfNull($isNull, 'is null', 'is NOT null')
    ->prependIfNotNull($isNotNull, 'is NOT null', 'is null')
    ->prependImplode(['a', 'b', 'c'], ',')
    ->toString();

Methods

MethodDescription
__construct(string $string = null)Constructor of StringBuffer
StringBuffer static create(string $string = null)Creates an instance of StringBuffer. Good for method chaining
string toString()Get the string value of StringBuffer
string __toString()Get the string value of StringBuffer
StringBuffer append(string $string)Appends a string to the buffer.
StringBuffer appendFormatted(string $format, string ...$values)Appends a formatted string (with sprintf()) to the buffer.
StringBuffer appendIf(bool $condition, string $string, string $else = null)Appends a string to the buffer if $condition is true, otherwise $else (if not null)
StringBuffer appendIfNot(bool $condition, string $string, string $else = null)Appends a string to the buffer if $condition is false, otherwise $else (if not null)
StringBuffer appendIfNull(mixed $value, string $string, string $else = null)Appends a string to the buffer if $value is null, otherwise $else (if not null)
StringBuffer appendIfNotNull(mixed $value, string $string, string $else = null)Appends a string to the buffer if $value is NOT null, otherwise $else (if not null)
StringBuffer appendImplode(array $values, string $separator = ' ')Appends an imploded array to the buffer.
StringBuffer prepend(string $string)Prepends a string to the buffer.
StringBuffer prependFormatted(string $format, string ...$values)Prepends a formatted string (with sprintf()) to the buffer.
StringBuffer prependIf(bool $condition, string $string, string $else = null)Prepends a string to the buffer if $condition is true, otherwise $else (if not null)
StringBuffer prependIfNot(bool $condition, string $string, string $else = null)Prepends a string to the buffer if $condition is false, otherwise $else (if not null)
StringBuffer prependIfNull(mixed $value, string $string, string $else = null)Prepends a string to the buffer if $value is null, otherwise $else (if not null)
StringBuffer prependIfNotNull(mixed $value, string $string, string $else = null)Prepends a string to the buffer if $value is NOT null, otherwise $else (if not null)
StringBuffer prependImplode(array $values, string $separator = ' ')Prepends an imploded array to the buffer.