forked from php-soap/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpPropertyNameNormalizer.php
More file actions
39 lines (33 loc) · 935 Bytes
/
PhpPropertyNameNormalizer.php
File metadata and controls
39 lines (33 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
declare(strict_types=1);
namespace Soap\Encoding\Normalizer;
use function array_filter;
use function array_map;
use function array_shift;
use function array_unshift;
use function count;
use function preg_split;
use function Psl\Type\non_empty_string;
final class PhpPropertyNameNormalizer
{
public static function normalize(string $name): string
{
return self::camelCase($name, '{[^a-z0-9_]+}i');
}
/**
* @param non-empty-string $regexp
*/
private static function camelCase(string $word, string $regexp):string
{
$parts = array_filter(preg_split($regexp, $word));
if (count($parts) === 1) {
return $word;
}
$keepUnchanged = array_shift($parts);
$parts = array_map(\ucfirst(...), $parts);
array_unshift($parts, $keepUnchanged);
return non_empty_string()->assert(
implode('', $parts)
);
}
}