Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2e3e635

Browse files
authoredFeb 14, 2022
Merge pull request #154 from rmbl/feature/humanize-implicit-label
Humanize the implicit label of symfony form types
2 parents fb24f91 + 0715e98 commit 2e3e635

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed
 

‎src/Visitor/Php/Symfony/FormTypeLabelImplicit.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public function enterNode(Node $node): ?Node
8484

8585
$label = $node->args[0]->value->value;
8686
if (!empty($label)) {
87+
$label = $this->humanize($label);
8788
if (null !== $location = $this->getLocation($label, $node->getAttribute('startLine'), $node, ['domain' => $domain])) {
8889
$this->lateCollect($location);
8990
}
@@ -93,4 +94,12 @@ public function enterNode(Node $node): ?Node
9394

9495
return null;
9596
}
97+
98+
/**
99+
* @see Symfony\Component\Form\FormRenderer::humanize()
100+
*/
101+
private function humanize(string $text): string
102+
{
103+
return ucfirst(strtolower(trim(preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $text))));
104+
}
96105
}

‎tests/Functional/Visitor/Php/Symfony/FormTypeLabelImplicitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function testExtract()
2525
$collection = $this->getSourceLocations(new FormTypeLabelImplicit(), Resources\Php\Symfony\ImplicitLabelType::class);
2626

2727
$this->assertCount(5, $collection, print_r($collection, true));
28-
$this->assertEquals('find1', $collection->get(0)->getMessage());
29-
$this->assertEquals('bigger_find2', $collection->get(1)->getMessage());
30-
$this->assertEquals('camelFind3', $collection->get(2)->getMessage());
28+
$this->assertEquals('Find1', $collection->get(0)->getMessage());
29+
$this->assertEquals('Bigger find2', $collection->get(1)->getMessage());
30+
$this->assertEquals('Camel find3', $collection->get(2)->getMessage());
3131

3232
//issue87: support for Ignore annotation
33-
$this->assertEquals('issue87-willBeAdded', $collection->get(3)->getMessage());
33+
$this->assertEquals('Issue87-will be added', $collection->get(3)->getMessage());
3434
}
3535
}

‎tests/Functional/Visitor/Php/Symfony/FormTypeLabelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function testTranslationDomain()
5555
$this->assertEquals('admin0', $messageA->getContext()['domain']);
5656

5757
$messageB = $collection->get(1);
58-
$this->assertEquals('test_b', $messageB->getMessage());
58+
$this->assertEquals('Test b', $messageB->getMessage());
5959
$this->assertEquals('admin1', $messageB->getContext()['domain']);
6060

6161
$messageC = $collection->get(2);
62-
$this->assertEquals('test_c', $messageC->getMessage());
62+
$this->assertEquals('Test c', $messageC->getMessage());
6363
$this->assertNull($messageC->getContext()['domain']);
6464
}
6565
}

0 commit comments

Comments
 (0)
Please sign in to comment.