first commit

This commit is contained in:
2024-07-15 12:33:27 +02:00
commit ce50ae282b
22084 changed files with 2623791 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait AccessorsPart {
/**
* Returns the element at the given index (or null if the index isn't present)
*
* @param int|string $index
*
* @return mixed
*/
public function get(int|string $index): mixed {
return $this->array[$index] ?? null;
}
}

28
vendor/phootwork/lang/parts/AddPart.php vendored Normal file
View File

@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait AddPart {
/**
* Adds one or more elements to that array
*
* @param mixed ...$elements
*
* @return $this
*/
public function add(mixed ...$elements): self {
/** @var mixed $element */
foreach ($elements as $element) {
$this->array[count($this->array)] = $element;
}
return $this;
}
}

View File

@@ -0,0 +1,103 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use InvalidArgumentException;
use phootwork\lang\ArrayObject;
use phootwork\lang\Text;
/**
* Text methods for array/ArrayObject conversions
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
trait ArrayConversionsPart {
abstract protected function getString(): string;
/**
* Splits the string by string
*
* @param string $delimiter The boundary string.
* @param int $limit
* If limit is set and positive, the returned array will contain a maximum of
* limit elements with the last element containing the rest of string.
*
* If the limit parameter is negative, all components except the last
* -limit are returned.
*
* If the limit parameter is zero, then this is treated as 1.
*
* @throws InvalidArgumentException If the delimiter is an empty string.
*
* @return ArrayObject
* Returns an array of strings created by splitting the string parameter on boundaries
* formed by the delimiter.
*
* If delimiter contains a value that is not contained in string and a negative limit is used,
* then an empty array will be returned, otherwise an array containing string will be returned.
*
*/
public function split(string $delimiter, int $limit = PHP_INT_MAX): ArrayObject {
if ('' === $delimiter) {
throw new InvalidArgumentException("The delimiter can't be an empty string");
}
return new ArrayObject(explode($delimiter, $this->getString(), $limit));
}
/**
* Join array elements with a string
*
* @param array $pieces The array of strings to join.
* @param string $glue Defaults to an empty string.
* @param string|null $encoding the desired encoding
*
* @return Text
* Returns a string containing a string representation of all the array elements in the
* same order, with the glue string between each element.
*
* @psalm-suppress MixedArgumentTypeCoercion
*/
public static function join(array $pieces, string $glue = '', ?string $encoding = null): Text {
array_map(
function (mixed $element): void {
if (!($element === null || is_scalar($element) || $element instanceof \Stringable)) {
throw new \TypeError('Can join elements only if scalar, null or \\Stringable');
}
},
$pieces
);
return new Text(implode($glue, $pieces), $encoding);
}
/**
* Convert the string to an array
*
* @param int $splitLength Maximum length of the chunk.
*
* @throws InvalidArgumentException If splitLength is less than 1.
*
* @return ArrayObject
* If the optional splitLength parameter is specified, the returned array will be
* broken down into chunks with each being splitLength in length, otherwise each chunk
* will be one character in length.
* If the split_length length exceeds the length of string, the entire string is returned
* as the first (and only) array element.
*/
public function chunk(int $splitLength = 1): ArrayObject {
if (false === $array = str_split($this->getString(), $splitLength)) {
throw new InvalidArgumentException('The chunk length has to be positive');
}
return new ArrayObject($array);
}
}

View File

@@ -0,0 +1,133 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use phootwork\lang\inflector\Inflector;
use phootwork\lang\inflector\InflectorInterface;
trait CheckerPart {
abstract protected function getString(): string;
/**
* Checks if the string is empty
*
* @return bool
*/
public function isEmpty(): bool {
return empty($this->getString());
}
/**
* Check if the string contains only alphanumeric characters.
*
* @return bool
*/
public function isAlphanumeric(): bool {
return ctype_alnum($this->getString());
}
/**
* Check if the string contains only alphanumeric characters.
*
* @return bool
*/
public function isAlphabetic(): bool {
return ctype_alpha($this->getString());
}
/**
* Check if the string contains only numeric characters.
*
* @return bool
*/
public function isNumeric(): bool {
return ctype_digit($this->getString());
}
/**
* Check if the string contains only characters which are not whitespace or an alphanumeric.
*
* @return bool
*/
public function isPunctuation(): bool {
return ctype_punct($this->getString());
}
/**
* Check if the string contains only space characters.
*
* @return bool
*/
public function isSpace(): bool {
return ctype_space($this->getString());
}
/**
* Check if the string contains only lower case characters.
*
* Spaces are considered non-lowercase characters, so lowercase strings with multiple words, separated by spaces,
* return false. E.g.:
*
* <code>
* $text = new Text('lowercase multi words string');<br>
* var_dump($text->isLowercase()); // false
* </code>
*
* @return bool
*/
public function isLowerCase(): bool {
return ctype_lower($this->getString());
}
/**
* Check if the string contains only upper case characters.
*
* Spaces are considered non-uppercase characters, so uppercase strings with multiple words, separated by spaces,
* return false. E.g.:
*
* <code>
* $text = new Text('UPPERCASE MULTI WORDS STRING'); <br>
* var_dump($text->isUppercase()); // false
* </code>
*
* @return bool
*/
public function isUpperCase(): bool {
return ctype_upper($this->getString());
}
/**
* Check if a string is singular form.
*
* @param InflectorInterface|null $pluralizer
* A custom pluralizer. Default is the Inflector
*
* @return bool
*/
public function isSingular(?InflectorInterface $pluralizer = null): bool {
$pluralizer = $pluralizer ?? new Inflector();
return $pluralizer->isSingular($this->getString());
}
/**
* Check if a string is plural form.
*
* @param InflectorInterface|null $pluralizer
* A custom pluralizer. Default is the Inflector
*
* @return bool
*/
public function isPlural(?InflectorInterface $pluralizer = null): bool {
$pluralizer = $pluralizer ?? new Inflector();
return $pluralizer->isPlural($this->getString());
}
}

View File

@@ -0,0 +1,93 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use phootwork\lang\Text;
/**
* Text methods to perform string and Text object comparison
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
trait ComparisonPart {
abstract protected function getString(): string;
/**
* Compares this string to another
*
* @param mixed $comparison
*
* @return int
*
* @see \phootwork\lang\Comparable::compareTo()
*/
public function compareTo(mixed $comparison): int {
if (is_string($comparison) || $comparison instanceof Text) {
return $this->compare($comparison);
}
throw new \InvalidArgumentException('`compareTo` method can accept only strings or Text objects.');
}
/**
* Compares this string to another string, ignoring the case
*
* @param string|Text $compare
*
* @return int Return Values:<br>
* &lt; 0 if the object is less than comparison<br>
* &gt; 0 if the object is greater than comparison<br>
* 0 if they are equal.
*/
public function compareCaseInsensitive(string|Text $compare): int {
return $this->compare($compare, 'strcasecmp');
}
/**
* Compares this string to another
*
* @param string|Text $compare string to compare to
* @param callable|null $callback
*
* @return int
*
* @psalm-suppress MixedInferredReturnType
*/
public function compare(string|Text $compare, callable $callback = null): int {
if ($callback === null) {
$callback = 'strcmp';
}
return $callback($this->getString(), (string) $compare);
}
/**
* Checks whether the string and the given object are equal
*
* @param string|Text $string
*
* @return bool
*/
public function equals(string|Text $string): bool {
return $this->compareTo($string) === 0;
}
/**
* Checks whether the string and the given object are equal ignoring the case
*
* @param string|Text $string
*
* @return bool
*/
public function equalsIgnoreCase(string|Text $string): bool {
return $this->compareCaseInsensitive($string) === 0;
}
}

View File

@@ -0,0 +1,21 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait EachPart {
/**
* Iterates the array and calls the callback function with the current item as parameter
*
* @param callable $callback
*/
public function each(callable $callback): void {
array_map($callback, $this->array);
}
}

View File

@@ -0,0 +1,78 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait IndexFindersPart {
abstract public function find(mixed ...$arguments);
abstract public function findLast(mixed ...$arguments);
/**
* Returns the index of the given element or null if the element can't be found
*
* @param mixed $element
*
* @return int|string|null the index for the given element
*/
public function indexOf(mixed $element): int|string|null {
$out = array_search($element, $this->array, true);
return $out === false ? null : $out;
}
/**
* Searches the array with a given callback and returns the index for the last element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return int|string|null the index or null if it hasn't been found
*/
public function findLastIndex(mixed ...$arguments): int|string|null {
/** @var mixed $index */
$index = count($arguments) === 1 ?
$this->findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]);
return $this->indexOf($index);
}
/**
* Searches the array with a given callback and returns the index for the first element if found.
*
* The callback function takes one or two parameters:
*
* function ($element [, $query]) {}
*
* The callback must return a boolean
* When it's passed, $query must be the first argument:
*
* - find($query, callback)
* - find(callback)
*
* @param array $arguments
*
* @return int|string|null the index or null if it hasn't been found
*/
public function findIndex(mixed ...$arguments): int|string|null {
/** @var mixed $index */
$index = count($arguments) === 1 ? $this->find($arguments[0]) : $this->find($arguments[0], $arguments[1]);
return $this->indexOf($index);
}
}

View File

@@ -0,0 +1,33 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait InsertPart {
abstract public function add(mixed ...$elements);
/**
* Insert one element at the given index
*
* @param mixed $element
* @param int|null|string $index
*
* @return $this
*/
public function insert(mixed $element, int|string|null $index): self {
if (null === $index) {
return $this->add($element);
}
is_int($index) ? array_splice($this->array, $index, 0, [$element])
: $this->array[$index] = $element;
return $this;
}
}

View File

@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use InvalidArgumentException;
/**
* Internal Text methods
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
trait InternalPart {
abstract public function length(): int;
/**
* @internal
*
* @param int $offset
*
* @return int
*/
protected function prepareOffset(int $offset): int {
$len = $this->length();
if ($offset < -$len || $offset > $len) {
throw new InvalidArgumentException("Offset must be in range [-$len, $len]");
}
if ($offset < 0) {
$offset += $len;
}
return $offset;
}
/**
* @param int $offset
* @param int|null $length
*
* @return int
*
* @internal
*
*/
protected function prepareLength(int $offset, ?int $length): int {
$length = (null === $length) ? ($this->length() - $offset) : (
($length < 0) ? ($length + $this->length() - $offset) : $length
);
if ($length < 0) {
throw new InvalidArgumentException('Length too small');
}
if ($offset + $length > $this->length()) {
throw new InvalidArgumentException('Length too large');
}
return $length;
}
}

21
vendor/phootwork/lang/parts/PopPart.php vendored Normal file
View File

@@ -0,0 +1,21 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait PopPart {
/**
* Pop the element off the end of array
*
* @return mixed the popped element
*/
public function pop(): mixed {
return array_pop($this->array);
}
}

View File

@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait ReducePart {
/**
* Iterative reduction of this array or collection with the help of a callback function. The callback
* function takes two parameters, the first is the carry, the second the current item, with this
* signature: mixed callback(mixed $carry, mixed $item)
*
* @param callable $callback the callback function
* @param mixed $fallback the default value, that will be returned when the list is empty
*
* @return mixed
*
* @psalm-suppress MixedArgumentTypeCoercion $callback is a callback
*/
public function reduce(callable $callback, mixed $fallback = null): mixed {
return array_reduce($this->array, $callback, $fallback);
}
}

View File

@@ -0,0 +1,43 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait RemovePart {
/**
* Removes one or more elements from the array
*
* @param mixed ...$elements
*
* @return $this
*/
public function remove(mixed ...$elements): self {
/** @var mixed $element */
foreach ($elements as $element) {
$index = array_search($element, $this->array, true);
if ($index !== false) {
unset($this->array[$index]);
$this->reorderList();
}
}
return $this;
}
private function reorderList(): void {
if (count(array_filter(array_keys($this->array), 'is_string')) > 0) {
//it's an associative array: do nothing
return;
}
if (!array_is_list($this->array)) {
$this->array = array_values($this->array);
}
}
}

View File

@@ -0,0 +1,23 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
trait ReversePart {
/**
* Reverses the order of all elements
*
* @return $this
*/
public function reverse(): self {
$this->array = array_reverse($this->array);
return $this;
}
}

View File

@@ -0,0 +1,166 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use phootwork\lang\ArrayObject;
use phootwork\lang\Text;
use Stringable;
/**
* Text searching methods
*
* @author ThomasGossmann
* @author Cristiano Cinotti
*/
trait SearchPart {
abstract protected function getString(): string;
abstract public function length(): int;
/**
* Returns the character at the given zero-related index
*
* <code>
* $str = new Text('Hello World!');<br>
* $str->at(6); // W
*
* $str = new Text('いちりんしゃ');<br>
* $str->at(4) // し
* </code>
*
* @param int $index zero-related index
*
* @return string the found character
*/
public function at(int $index): string {
return mb_substr($this->getString(), $index, 1, $this->encoding);
}
/**
* Returns an ArrayObject consisting of the characters in the string.
*
* @return ArrayObject An ArrayObject of all chars
*/
public function chars(): ArrayObject {
return new ArrayObject(mb_str_split($this->getString(), 1, $this->encoding));
}
/**
* Returns the index of a given string, starting at the optional zero-related offset
*
* @param string|Stringable $string
* @param int $offset zero-related offset
*
* @return int|null int for the index or null if the given string doesn't occur
*/
public function indexOf(string|Stringable $string, int $offset = 0): ?int {
$output = mb_strpos($this->getString(), (string) $string, $offset, $this->encoding);
return false === $output ? null : $output;
}
/**
* Returns the last index of a given string, starting at the optional offset
*
* @param string|Stringable $string $string
* @param int|null $offset
*
* @return int|null int for the index or null if the given string doesn't occur
*/
public function lastIndexOf(string|Stringable $string, ?int $offset = null): ?int {
if (null === $offset) {
$offset = $this->length();
}
// Converts $offset to a negative offset as strrpos has a different
// behavior for positive offsets.
$output = mb_strrpos($this->getString(), (string) $string, $offset - $this->length(), $this->encoding);
return false === $output ? null : $output;
}
/**
* Checks whether the string starts with the given string. Case sensitive!
*
* @param string|Stringable $substring The substring to look for
*
* @return bool
*
* @see Text::startsWithIgnoreCase()
*
*/
public function startsWith(string|Stringable $substring): bool {
return str_starts_with($this->getString(), (string) $substring);
}
/**
* Checks whether the string starts with the given string. Ignores case.
*
* @param string|Stringable $substring The substring to look for
*
* @return bool
*
* @see Text::startsWith()
*
*/
public function startsWithIgnoreCase(string|Stringable $substring): bool {
return str_starts_with($this->toUpperCase()->getString(), mb_strtoupper((string) $substring, $this->encoding));
}
/**
* Checks whether the string ends with the given string. Case sensitive!
*
* @param string|Stringable $substring The substring to look for
*
* @return bool
*
* @see Text::endsWithIgnoreCase()
*
*/
public function endsWith(string|Stringable $substring): bool {
return str_ends_with($this->getString(), (string) $substring);
}
/**
* Checks whether the string ends with the given string. Ingores case.
*
* @param string|Stringable $substring The substring to look for
*
* @return bool
*
* @see Text::endsWith()
*
*/
public function endsWithIgnoreCase(string|Stringable $substring): bool {
return str_ends_with($this->toUpperCase()->getString(), mb_strtoupper((string) $substring, $this->encoding));
}
/**
* Checks whether the given string occurs
*
* @param string|Stringable $text
*
* @return bool
*/
public function contains(Stringable|string $text): bool {
return str_contains($this->getString(), (string) $text);
}
/**
* Performs a regular expression matching with the given regexp
*
* @param string $regexp
*
* @return bool
*/
public function match(string $regexp): bool {
return (bool) preg_match($regexp, $this->getString());
}
}

View File

@@ -0,0 +1,40 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use phootwork\lang\Comparator;
trait SortAssocPart {
/**
* Sorts the array and persisting key-value pairs
*
* @param Comparator|callable|null $cmp
*
* @return $this
*/
public function sortAssoc(Comparator|callable $cmp = null): self {
$this->doSort($this->array, 'uasort', 'asort', $cmp);
return $this;
}
/**
* Sorts the array by keys
*
* @param Comparator|callable|null $cmp
*
* @return $this
*/
public function sortKeys(Comparator|callable $cmp = null): self {
$this->doSort($this->array, 'uksort', 'ksort', $cmp);
return $this;
}
}

View File

@@ -0,0 +1,281 @@
<?php declare(strict_types=1);
/**
* This file is part of the Phootwork package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
* @copyright Thomas Gossmann
*/
namespace phootwork\lang\parts;
use phootwork\lang\Arrayable;
use phootwork\lang\ArrayObject;
use phootwork\lang\inflector\Inflector;
use phootwork\lang\inflector\InflectorInterface;
use phootwork\lang\Text;
use Stringable;
/**
* Text transformation methods
*
* @author Thomas Gossmann
* @author Cristiano Cinotti
*/
trait TransformationsPart {
abstract protected function getString(): string;
/**
* Slices a piece of the string from a given start to an end.
* If no length is given, the String is sliced to its maximum length.
*
* @param int $start
* @param int $end
*
* @return Text
*/
abstract public function substring(int $start, ?int $end = null): Text;
/**
* Splits the string by string
*
* @param string $delimiter
* @param int $limit
*
* @return ArrayObject
*/
abstract public function split(string $delimiter, int $limit = PHP_INT_MAX): ArrayObject;
/**
* Strip whitespace (or other characters) from the beginning and end of the string
*
* @param string $characters
*
* @return Text
*/
abstract public function trim(string $characters): Text;
/**
* @param string|Stringable $text
*
* @return bool
*/
abstract public function contains(Stringable|string $text): bool;
/**
* Replace all occurrences of the search string with the replacement string
*
* @param Arrayable|Stringable|array|string $search
* The value being searched for, otherwise known as the needle. An array may be used
* to designate multiple needles.
* @param Arrayable|Stringable|array|string $replace
* The replacement value that replaces found search values. An array may be used to
* designate multiple replacements.
*
* @return Text
*/
abstract public function replace(Arrayable|Stringable|array|string $search, Arrayable|Stringable|array|string $replace): Text;
/**
* Transforms the string to lowercase
*
* @return Text
*/
public function toLowerCase(): Text {
return new Text(mb_strtolower($this->getString(), $this->encoding), $this->encoding);
}
/**
* Transforms the string to first character lowercased
*
* @return Text
*/
public function toLowerCaseFirst(): Text {
$first = $this->substring(0, 1);
$rest = $this->substring(1);
return new Text(mb_strtolower((string) $first, $this->encoding) . $rest, $this->encoding);
}
/**
* Transforms the string to uppercase
*
* @return Text
*/
public function toUpperCase(): Text {
return new Text(mb_strtoupper($this->getString(), $this->encoding), $this->encoding);
}
/**
* Transforms the string to first character uppercased
*
* @return Text
*/
public function toUpperCaseFirst(): Text {
$first = $this->substring(0, 1);
$rest = $this->substring(1);
return new Text(mb_strtoupper((string) $first, $this->encoding) . $rest, $this->encoding);
}
/**
* Transforms the string to only its first character capitalized.
*
* @return Text
*/
public function toCapitalCase(): Text {
return $this->toLowerCase()->toUpperCaseFirst();
}
/**
* Transforms the string with the words capitalized.
*
* @return Text
*/
public function toCapitalCaseWords(): Text {
$encoding = $this->encoding;
return $this->split(' ')->map(function (string $str) use ($encoding) {
return Text::create($str, $encoding)->toCapitalCase();
})->join(' ');
}
/**
* Converts this string into camelCase. Numbers are considered as part of its previous piece.
*
* <code>
* $var = new Text('my_own_variable');<br>
* $var->toCamelCase(); // myOwnVariable
*
* $var = new Text('my_test3_variable');<br>
* $var->toCamelCase(); // myTest3Variable
* </code>
*
* @return Text
*/
public function toCamelCase(): Text {
return $this->toStudlyCase()->toLowerCaseFirst();
}
/**
* Converts this string into snake_case. Numbers are considered as part of its previous piece.
*
* <code>
* $var = new Text('myOwnVariable');<br>
* $var->toSnakeCase(); // my_own_variable
*
* $var = new Text('myTest3Variable');<br>
* $var->toSnakeCase(); // my_test3_variable
* </code>
*
* @return Text
*/
public function toSnakeCase(): Text {
return $this->toKebabCase()->replace('-', '_');
}
/**
* Converts this string into StudlyCase. Numbers are considered as part of its previous piece.
*
* <code>
* $var = new Text('my_own_variable');<br>
* $var->toStudlyCase(); // MyOwnVariable
*
* $var = new Text('my_test3_variable');<br>
* $var->toStudlyCase(); // MyTest3Variable
* </code>
*
* @return Text
*
* @psalm-suppress MixedArgument $matches[0] is a string
*/
public function toStudlyCase(): Text {
$input = $this->trim('-_');
if ($input->isEmpty()) {
return $input;
}
$normString = preg_replace('/\s+/', ' ', $input->toString());
$encoding = $this->encoding;
return Text::create(preg_replace_callback(
'/([A-Z-_\s][a-z0-9]+)/',
fn (array $matches): string => ucfirst(str_replace(['-', '_', ' '], '', $matches[0])),
$normString
), $encoding)
->toUpperCaseFirst();
}
/**
* Convert this string into kebab-case. Numbers are considered as part of its previous piece.
*
* <code>
* $var = new Text('myOwnVariable');<br>
* $var->toKebabCase(); // my-own-variable
*
* $var = new Text('myTest3Variable');<br>
* $var->toKebabCase(); // my-test3-variable
* </code>
*
* @return Text
*/
public function toKebabCase(): Text {
$input = $this->trim('-_');
$normString = str_replace([' ', '_'], '-', preg_replace('/\s+/', ' ', $input->toString()));
return new Text(mb_strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $normString)), $this->encoding);
}
/**
* Get the plural form of the Text object.
*
* @param InflectorInterface|null $pluralizer
*
* @return Text
*/
public function toPlural(?InflectorInterface $pluralizer = null): Text {
$pluralizer = $pluralizer ?: new Inflector();
return new Text($pluralizer->getPluralForm($this->getString()), $this->encoding);
}
/**
* Get the singular form of the Text object.
*
* @param InflectorInterface|null $pluralizer
*
* @return Text
*/
public function toSingular(?InflectorInterface $pluralizer = null): Text {
$pluralizer = $pluralizer ?: new Inflector();
return new Text($pluralizer->getSingularForm($this->getString()), $this->encoding);
}
/**
* Converts each tab in the string to some number of spaces, as defined by
* $tabLength. By default, each tab is converted to 4 consecutive spaces.
*
* @param int $tabLength Number of spaces to replace each tab with
*
* @return Text text with tabs converted to spaces
*/
public function toSpaces(int $tabLength = 4): Text {
$spaces = str_repeat(' ', $tabLength);
return $this->replace("\t", $spaces);
}
/**
* Converts each occurrence of some consecutive number of spaces, as
* defined by $tabLength, to a tab. By default, each 4 consecutive spaces
* are converted to a tab.
*
* @param int $tabLength Number of spaces to replace with a tab
*
* @return Text text with spaces converted to tabs
*/
public function toTabs(int $tabLength = 4): Text {
$spaces = str_repeat(' ', $tabLength);
return $this->replace($spaces, "\t");
}
}