Skip to content

Commit 7d0aa0d

Browse files
committed
Add LowercaseFormatter with proper UTF-8 support
The new LowercaseFormatter provides reliable UTF-8 aware lowercase conversion for international text, ensuring accented characters and Turkish special cases (İ/i) are handled correctly using mb_strtolower(). This formatter complements UppercaseFormatter and is essential for applications requiring proper internationalization support when manipulating text in various languages including those with special character mapping rules. Includes comprehensive tests covering ASCII, Latin accents, Turkish characters, non-Latin scripts, emoji, combining diacritics, right-to-left text, multi-byte characters, and mixed content. Assisted-by: OpenCode (GLM-4.7)
1 parent e5f5bef commit 7d0aa0d

File tree

6 files changed

+404
-0
lines changed

6 files changed

+404
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ See the [PlaceholderFormatter documentation](docs/PlaceholderFormatter.md) and [
6464
| [ImperialAreaFormatter](docs/ImperialAreaFormatter.md) | Imperial area promotion (in², ft², yd², ac, mi²) |
6565
| [ImperialLengthFormatter](docs/ImperialLengthFormatter.md) | Imperial length promotion (in, ft, yd, mi) |
6666
| [ImperialMassFormatter](docs/ImperialMassFormatter.md) | Imperial mass promotion (oz, lb, st, ton) |
67+
| [LowercaseFormatter](docs/LowercaseFormatter.md) | Convert string to lowercase |
6768
| [MaskFormatter](docs/MaskFormatter.md) | Range-based string masking with Unicode support |
6869
| [MassFormatter](docs/MassFormatter.md) | Metric mass promotion (mg, g, kg, t) |
6970
| [MetricFormatter](docs/MetricFormatter.md) | Metric length promotion (mm, cm, m, km) |

docs/LowercaseFormatter.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<!--
2+
SPDX-FileCopyrightText: (c) Respect Project Contributors
3+
SPDX-License-Identifier: ISC
4+
SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
5+
-->
6+
7+
# LowercaseFormatter
8+
9+
The `LowercaseFormatter` converts strings to lowercase with proper UTF-8 character support for international text.
10+
11+
## Usage
12+
13+
### Basic Usage
14+
15+
```php
16+
use Respect\StringFormatter\LowercaseFormatter;
17+
18+
$formatter = new LowercaseFormatter();
19+
20+
echo $formatter->format('HELLO WORLD');
21+
// Outputs: "hello world"
22+
```
23+
24+
### Unicode Characters
25+
26+
```php
27+
use Respect\StringFormatter\LowercaseFormatter;
28+
29+
$formatter = new LowercaseFormatter();
30+
31+
echo $formatter->format('CAFÉ FRANÇAIS');
32+
// Outputs: "café français"
33+
34+
echo $formatter->format('コンニチハ');
35+
// Outputs: "コンニチハ"
36+
```
37+
38+
### Mixed Content
39+
40+
```php
41+
use Respect\StringFormatter\LowercaseFormatter;
42+
43+
$formatter = new LowercaseFormatter();
44+
45+
echo $formatter->format('HELLO WORLD 😊');
46+
// Outputs: "hello world 😊"
47+
```
48+
49+
## API
50+
51+
### `LowercaseFormatter::__construct`
52+
53+
- `__construct()`
54+
55+
Creates a new lowercase formatter instance.
56+
57+
### `format`
58+
59+
- `format(string $input): string`
60+
61+
Converts the input string to lowercase using UTF-8 aware conversion.
62+
63+
**Parameters:**
64+
65+
- `$input`: The string to convert to lowercase
66+
67+
**Returns:** The lowercase string
68+
69+
## Examples
70+
71+
| Input | Output | Description |
72+
| ------------ | ------------ | ----------------------------- |
73+
| `HELLO` | `hello` | Simple ASCII text |
74+
| `CAFÉ` | `café` | Latin characters with accents |
75+
| `ПРИВЕТ` | `привет` | Cyrillic text |
76+
| `コンニチハ` | `コンニチハ` | Japanese text |
77+
| `HELLO 😊` | `hello 😊` | Text with emoji |
78+
| `ÉÎÔÛ` | `éîôû` | Multiple accented characters |
79+
80+
## Notes
81+
82+
- Uses `mb_strtolower()` for proper Unicode handling
83+
- Preserves accent marks and diacritical marks
84+
- Works with all Unicode scripts (Latin, Cyrillic, Greek, CJK, etc.)
85+
- Emoji and special symbols are preserved unchanged
86+
- Combining diacritics are properly handled
87+
- Numbers and special characters remain unchanged
88+
- Empty strings return empty strings

src/LowercaseFormatter.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* SPDX-FileCopyrightText: (c) Respect Project Contributors
5+
* SPDX-License-Identifier: ISC
6+
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Respect\StringFormatter;
12+
13+
use function mb_strtolower;
14+
15+
final readonly class LowercaseFormatter implements Formatter
16+
{
17+
public function format(string $input): string
18+
{
19+
return mb_strtolower($input);
20+
}
21+
}

src/Mixin/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public static function imperialMass(string $unit): FormatterBuilder;
3030

3131
public static function date(string $format = 'Y-m-d H:i:s'): FormatterBuilder;
3232

33+
public static function lowercase(): FormatterBuilder;
34+
3335
public static function mask(string $range, string $replacement = '*'): FormatterBuilder;
3436

3537
public static function metric(string $unit): FormatterBuilder;

src/Mixin/Chain.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function imperialMass(string $unit): FormatterBuilder;
3030

3131
public function date(string $format = 'Y-m-d H:i:s'): FormatterBuilder;
3232

33+
public function lowercase(): FormatterBuilder;
34+
3335
public function mask(string $range, string $replacement = '*'): FormatterBuilder;
3436

3537
public function metric(string $unit): FormatterBuilder;

0 commit comments

Comments
 (0)