Skip to content

Commit 675f11a

Browse files
authored
Added placeholder extractor (#16)
* Added placeholder extractor * cs
1 parent 631b868 commit 675f11a

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Extractor\Visitor\Php\Symfony;
13+
14+
use PhpParser\Node;
15+
use PhpParser\Node\Stmt;
16+
use PhpParser\NodeTraverser;
17+
use PhpParser\NodeVisitor;
18+
use Translation\Extractor\Model\SourceLocation;
19+
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
20+
21+
class FormTypePlaceholder extends BasePHPVisitor implements NodeVisitor
22+
{
23+
public function enterNode(Node $node)
24+
{
25+
// only Traverse *Type
26+
if ($node instanceof Stmt\Class_) {
27+
if (substr($node->name, -4) !== 'Type') {
28+
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
29+
}
30+
}
31+
32+
if (!$node instanceof Node\Expr\Array_) {
33+
return;
34+
}
35+
36+
foreach ($node->items as $item) {
37+
if (!$item->key instanceof Node\Scalar\String_) {
38+
continue;
39+
}
40+
41+
if ($item->key->value === 'placeholder' && $item->value instanceof Node\Scalar\String_) {
42+
$this->collection->addLocation(new SourceLocation($item->value->value, $this->getAbsoluteFilePath(), $item->value->getAttribute('startLine')));
43+
}
44+
}
45+
}
46+
47+
public function leaveNode(Node $node)
48+
{
49+
}
50+
51+
public function beforeTraverse(array $nodes)
52+
{
53+
}
54+
55+
public function afterTraverse(array $nodes)
56+
{
57+
}
58+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
13+
14+
use Translation\Extractor\Tests\Functional\Visitor\Php\BasePHPVisitorTest;
15+
use Translation\Extractor\Tests\Resources;
16+
use Translation\Extractor\Visitor\Php\Symfony\FormTypePlaceholder;
17+
18+
class FormTypePlaceholderTest extends BasePHPVisitorTest
19+
{
20+
public function testExtract()
21+
{
22+
$collection = $this->getSourceLocations(new FormTypePlaceholder(), Resources\Php\Symfony\PlaceholderFormType::class);
23+
24+
$this->assertCount(3, $collection);
25+
$this->assertEquals('form.placeholder.text', $collection->get(0)->getMessage());
26+
$this->assertEquals('form.placeholder.text.but.no.label', $collection->get(1)->getMessage());
27+
$this->assertEquals('form.choice_placeholder', $collection->get(2)->getMessage());
28+
}
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
8+
class PlaceholderFormType extends AbstractType
9+
{
10+
public function buildForm(FormBuilderInterface $builder, array $options)
11+
{
12+
$builder
13+
->add('field_with_attr_placeholder', 'text', array(
14+
'label' => 'field.with.placeholder',
15+
'attr' => array('placeholder' => 'form.placeholder.text')
16+
))
17+
->add('field_without_label_with_attr_placeholder', 'text', array(
18+
'label' => false,
19+
'attr' => array('placeholder' => 'form.placeholder.text.but.no.label')
20+
))
21+
->add('field_placeholder', 'choice', array(
22+
'placeholder' => 'form.choice_placeholder'
23+
))
24+
;
25+
}
26+
}

0 commit comments

Comments
 (0)