diff --git a/docs/book/v3/word.md b/docs/book/v3/word.md index cc95b4ad..ec917fdf 100644 --- a/docs/book/v3/word.md +++ b/docs/book/v3/word.md @@ -323,3 +323,22 @@ print $filter->filter('this_is_my_content'); ``` The above example returns `this-is-my-content`. + +## UnderscoreToStudlyCase + +This filter modifies a given string such that `words_with_underscores` are +converted to `wordsWithUnderscores`. + +### Supported Options + +There are no additional options for `Laminas\Filter\Word\UnderscoreToStudlyCase`: + +### Basic Usage + +```php +$filter = new Laminas\Filter\Word\UnderscoreToStudlyCase(); + +print $filter->filter('this_is_my_content'); +``` + +The above example returns `thisIsMyContent`. diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7b00704d..103558e3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -635,22 +635,6 @@ - - - - - - - - - - - - - - - - @@ -1008,14 +992,4 @@ - - - - - - - - - - diff --git a/src/Word/UnderscoreToStudlyCase.php b/src/Word/UnderscoreToStudlyCase.php index 8dd81653..8da1b489 100755 --- a/src/Word/UnderscoreToStudlyCase.php +++ b/src/Word/UnderscoreToStudlyCase.php @@ -4,30 +4,18 @@ namespace Laminas\Filter\Word; -use Laminas\Stdlib\StringUtils; +use Laminas\Filter\FilterInterface; +use Laminas\Filter\ScalarOrArrayFilterCallback; -use function array_map; use function is_array; use function is_scalar; use function mb_strlen; use function mb_strtolower; use function mb_substr; -/** - * @psalm-type Options = array{ - * separator?: string, - * ... - * } - * @template TOptions of Options - * @template-extends UnderscoreToCamelCase - */ -final class UnderscoreToStudlyCase extends UnderscoreToCamelCase +/** @implements FilterInterface> */ +final class UnderscoreToStudlyCase implements FilterInterface { - /** - * Defined by Laminas\Filter\Filter - * - * @psalm-return ($value is scalar ? string : $value is array ? array : mixed) - */ public function filter(mixed $value): mixed { if (! is_scalar($value) && ! is_array($value)) { @@ -35,19 +23,22 @@ public function filter(mixed $value): mixed } /** @var string|array $value */ - $value = parent::filter($value); - $lowerCaseFirst = 'lcfirst'; + $value = (new SeparatorToCamelCase('_'))->filter($value); - if (StringUtils::hasPcreUnicodeSupport()) { - $lowerCaseFirst = static function ($value) { - if (0 === mb_strlen($value)) { - return $value; + return ScalarOrArrayFilterCallback::applyRecursively( + $value, + function (string $input): string { + if (0 === mb_strlen($input)) { + return $input; } - return mb_strtolower(mb_substr($value, 0, 1)) . mb_substr($value, 1); - }; - } + return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1); + } + ); + } - return is_array($value) ? array_map($lowerCaseFirst, $value) : $lowerCaseFirst($value); + public function __invoke(mixed $value): mixed + { + return $this->filter($value); } }