-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from veewee/normalize-object-names
Normalize property names to supported php names in the object encoder
- Loading branch information
Showing
4 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\Encoding\Normalizer; | ||
|
||
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 literal-string $regexp | ||
*/ | ||
private static function camelCase(string $word, string $regexp):string | ||
{ | ||
$parts = array_filter(preg_split($regexp, $word)); | ||
$keepUnchanged = array_shift($parts); | ||
$parts = array_map('ucfirst', $parts); | ||
array_unshift($parts, $keepUnchanged); | ||
|
||
return non_empty_string()->assert( | ||
implode('', $parts) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Soap\Encoding\Test\Unit\Normalizer; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Soap\Encoding\Normalizer\PhpPropertyNameNormalizer; | ||
|
||
#[CoversClass(PhpPropertyNameNormalizer::class)] | ||
final class PhpPropertyNameNormalizerTest extends TestCase | ||
{ | ||
|
||
/** | ||
* | ||
* @dataProvider provideCases | ||
*/ | ||
public function test_it_can_normalize(string $in, string $expected): void | ||
{ | ||
static::assertEquals($expected, PhpPropertyNameNormalizer::normalize($in)); | ||
} | ||
|
||
public static function provideCases() | ||
{ | ||
yield ['prop1', 'prop1']; | ||
yield ['final', 'final']; | ||
yield ['Final', 'Final']; | ||
yield ['UpperCased', 'UpperCased']; | ||
yield ['my-./*prop_123', 'myProp_123']; | ||
yield ['My-./*prop_123', 'MyProp_123']; | ||
yield ['My-./final*prop_123', 'MyFinalProp_123']; | ||
} | ||
} |