Skip to content

Commit e5f5bef

Browse files
committed
Add UppercaseFormatter with proper UTF-8 support
The new UppercaseFormatter provides reliable UTF-8 aware uppercase conversion for international text, ensuring accented characters and non-Latin scripts are handled correctly using mb_strtoupper(). This formatter is essential for applications requiring proper internationalization support when manipulating text in various languages like French, German, Turkish, Greek, Cyrillic, and CJK languages. Includes comprehensive tests covering ASCII, Latin accents, non-Latin scripts, emoji, combining diacritics, right-to-left text, multi-byte characters, and mixed content scenarios. Assisted-by: OpenCode (GLM-4.7)
1 parent 315169d commit e5f5bef

File tree

6 files changed

+383
-0
lines changed

6 files changed

+383
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ See the [PlaceholderFormatter documentation](docs/PlaceholderFormatter.md) and [
7272
| [PlaceholderFormatter](docs/PlaceholderFormatter.md) | Template interpolation with placeholder replacement |
7373
| [SecureCreditCardFormatter](docs/SecureCreditCardFormatter.md) | Masked credit card formatting for secure display |
7474
| [TimeFormatter](docs/TimeFormatter.md) | Time promotion (mil, c, dec, y, mo, w, d, h, min, s, ms, us, ns) |
75+
| [UppercaseFormatter](docs/UppercaseFormatter.md) | Convert string to uppercase |
7576

7677
## Contributing
7778

docs/UppercaseFormatter.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+
# UppercaseFormatter
8+
9+
The `UppercaseFormatter` converts strings to uppercase with proper UTF-8 character support for international text.
10+
11+
## Usage
12+
13+
### Basic Usage
14+
15+
```php
16+
use Respect\StringFormatter\UppercaseFormatter;
17+
18+
$formatter = new UppercaseFormatter();
19+
20+
echo $formatter->format('hello world');
21+
// Outputs: "HELLO WORLD"
22+
```
23+
24+
### Unicode Characters
25+
26+
```php
27+
use Respect\StringFormatter\UppercaseFormatter;
28+
29+
$formatter = new UppercaseFormatter();
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\UppercaseFormatter;
42+
43+
$formatter = new UppercaseFormatter();
44+
45+
echo $formatter->format('Hello World 😊');
46+
// Outputs: "HELLO WORLD 😊"
47+
```
48+
49+
## API
50+
51+
### `UppercaseFormatter::__construct`
52+
53+
- `__construct()`
54+
55+
Creates a new uppercase formatter instance.
56+
57+
### `format`
58+
59+
- `format(string $input): string`
60+
61+
Converts the input string to uppercase using UTF-8 aware conversion.
62+
63+
**Parameters:**
64+
65+
- `$input`: The string to convert to uppercase
66+
67+
**Returns:** The uppercase 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 hiragana converted to katakana |
77+
| `Hello 😊` | `HELLO 😊` | Text with emoji |
78+
| `éîôû` | `ÉÎÔÛ` | Multiple accented characters |
79+
80+
## Notes
81+
82+
- Uses `mb_strtoupper()` 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/Mixin/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ public static function pattern(string $pattern): FormatterBuilder;
4848
public static function placeholder(array $parameters): FormatterBuilder;
4949

5050
public static function time(string $unit): FormatterBuilder;
51+
52+
public static function uppercase(): FormatterBuilder;
5153
}

src/Mixin/Chain.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ public function pattern(string $pattern): FormatterBuilder;
4848
public function placeholder(array $parameters): FormatterBuilder;
4949

5050
public function time(string $unit): FormatterBuilder;
51+
52+
public function uppercase(): FormatterBuilder;
5153
}

src/UppercaseFormatter.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_strtoupper;
14+
15+
final readonly class UppercaseFormatter implements Formatter
16+
{
17+
public function format(string $input): string
18+
{
19+
return mb_strtoupper($input);
20+
}
21+
}

0 commit comments

Comments
 (0)