Skip to content

Commit b3c9f6c

Browse files
committed
Method name filter and upper letters issue - Close #10
1 parent 0a1f3a2 commit b3c9f6c

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/Filter/UpperToLower.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-filter for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-filter/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-filter/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\Filter\Filter;
12+
13+
final class UpperToLower extends AbstractFilter
14+
{
15+
public function __invoke(string $value): string
16+
{
17+
$value = ($this->filter)($value);
18+
19+
if (1 === \preg_match('/^[A-Z]+$/', $value)) {
20+
return \strtolower($value);
21+
}
22+
23+
return $value;
24+
}
25+
}

src/FilterFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use OpenCodeModeling\Filter\Filter\LowerCaseFirst;
1717
use OpenCodeModeling\Filter\Filter\NormalizeLabel;
1818
use OpenCodeModeling\Filter\Filter\UpperCaseFirst;
19+
use OpenCodeModeling\Filter\Filter\UpperToLower;
1920

2021
final class FilterFactory
2122
{
@@ -93,7 +94,7 @@ public static function propertyNameFilter(): callable
9394
*/
9495
public static function methodNameFilter(): callable
9596
{
96-
return new LowerCaseFirst(self::normalizeFilter());
97+
return new LowerCaseFirst(self::normalizeFilter(new UpperToLower()));
9798
}
9899

99100
/**

tests/FilterFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public function it_filters_method_name(string $label): void
3636
$this->assertSame('addBuilding', ($filter)($label));
3737
}
3838

39+
/**
40+
* @test
41+
*/
42+
public function it_filters_method_name_with_only_upper_letters(): void
43+
{
44+
$filter = FilterFactory::methodNameFilter();
45+
$this->assertSame('ny', ($filter)('NY'));
46+
}
47+
3948
/**
4049
* @test
4150
* @dataProvider providerForLabel

0 commit comments

Comments
 (0)