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.
*
*
* $var = new Text('my_own_variable');
* $var->toCamelCase(); // myOwnVariable
*
* $var = new Text('my_test3_variable');
* $var->toCamelCase(); // myTest3Variable
*
*
* @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.
*
*
* $var = new Text('myOwnVariable');
* $var->toSnakeCase(); // my_own_variable
*
* $var = new Text('myTest3Variable');
* $var->toSnakeCase(); // my_test3_variable
*
*
* @return Text
*/
public function toSnakeCase(): Text {
return $this->toKebabCase()->replace('-', '_');
}
/**
* Converts this string into StudlyCase. Numbers are considered as part of its previous piece.
*
*
* $var = new Text('my_own_variable');
* $var->toStudlyCase(); // MyOwnVariable
*
* $var = new Text('my_test3_variable');
* $var->toStudlyCase(); // MyTest3Variable
*
*
* @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.
*
*
* $var = new Text('myOwnVariable');
* $var->toKebabCase(); // my-own-variable
*
* $var = new Text('myTest3Variable');
* $var->toKebabCase(); // my-test3-variable
*
*
* @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");
}
}