-
Notifications
You must be signed in to change notification settings - Fork 0
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 #7 from kynx/class-constant-normalizer
Added ClassConstantNameNormalizer
- Loading branch information
Showing
3 changed files
with
82 additions
and
1 deletion.
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kynx\Code\Normalizer; | ||
|
||
/** | ||
* @see \KynxTest\Code\Normalizer\ClassConstantNameNormalizerTest | ||
*/ | ||
final class ClassConstantNameNormalizer extends AbstractNormalizer | ||
{ | ||
protected const RESERVED = [ | ||
'class' | ||
]; | ||
|
||
public function __construct( | ||
string $suffix, | ||
WordCase $case = WordCase::UpperSnake, | ||
string $separators = self::DEFAULT_SEPARATORS | ||
) { | ||
parent::__construct($suffix, $case, $separators); | ||
} | ||
|
||
/** | ||
* Returns a valid PHP constant name from a UTF-8 string | ||
*/ | ||
public function normalize(string $label): string | ||
{ | ||
$ascii = $this->toAscii($label); | ||
$spaced = $this->separatorsToSpace($ascii); | ||
$speltOut = $this->spellOutAscii($spaced); | ||
$cased = $this->toCase($speltOut); | ||
|
||
return $this->sanitizeReserved($cased); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KynxTest\Code\Normalizer; | ||
|
||
use Kynx\Code\Normalizer\ClassConstantNameNormalizer; | ||
use Kynx\Code\Normalizer\WordCase; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Kynx\Code\Normalizer\ClassConstantNameNormalizer | ||
*/ | ||
final class ClassConstantNameNormalizerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider constantNameProvider | ||
*/ | ||
public function testNormalize(string $constantName, WordCase $case, string $expected): void | ||
{ | ||
$normalizer = new ClassConstantNameNormalizer('RESERVED', $case); | ||
$actual = $normalizer->normalize($constantName); | ||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public function constantNameProvider(): array | ||
{ | ||
return [ | ||
'unicode_spellout' => ['€ sign', WordCase::UpperSnake, 'EURO_SIGN'], | ||
'ascii_spellout' => ['$', WordCase::UpperSnake, 'DOLLAR'], | ||
'const_reserved' => ['exit', WordCase::UpperSnake, 'EXIT'], | ||
'class' => ['class', WordCase::UpperSnake, 'CLASS_RESERVED'], | ||
'lead_digits' => ['12 foo', WordCase::UpperSnake, 'ONE_TWO_FOO'], | ||
'PascalCase' => ['foo bar', WordCase::Pascal, 'FooBar'], | ||
]; | ||
} | ||
|
||
public function testNormalizeUsesSeparators(): void | ||
{ | ||
$expected = 'FOO_BAR_BAZ'; | ||
$normalizer = new ClassConstantNameNormalizer('Foo', WordCase::UpperSnake, '|/'); | ||
$actual = $normalizer->normalize('Foo|Bar/ Baz'); | ||
self::assertSame($expected, $actual); | ||
} | ||
} |