Skip to content

Commit

Permalink
Merge pull request #7 from kynx/class-constant-normalizer
Browse files Browse the repository at this point in the history
Added ClassConstantNameNormalizer
  • Loading branch information
kynx authored Mar 29, 2023
2 parents cb374e5 + 0ea7fc2 commit ffbab38
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ protected function sanitizeReserved(string $string): string
{
assert($this->suffix !== null);

if (in_array(strtolower($string), self::RESERVED, true)) {
if (in_array(strtolower($string), static::RESERVED, true)) {
return $string . $this->suffix;
}
return $string;
Expand Down
36 changes: 36 additions & 0 deletions src/ClassConstantNameNormalizer.php
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);
}
}
45 changes: 45 additions & 0 deletions test/ClassConstantNameNormalizerTest.php
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);
}
}

0 comments on commit ffbab38

Please sign in to comment.