Skip to content

Commit 96bb84a

Browse files
authored
Adding support for "empty_value" (#50)
* Added test resource * Added extractor * Typo * Applied changes from StyleCI
1 parent 9daec1b commit 96bb84a

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\NodeVisitor;
17+
use Translation\Extractor\Model\SourceLocation;
18+
use Translation\Extractor\Visitor\Php\BasePHPVisitor;
19+
20+
/**
21+
* @author Tobias Nyholm <[email protected]>
22+
*/
23+
final class FormTypeEmptyValue extends BasePHPVisitor implements NodeVisitor
24+
{
25+
public function enterNode(Node $node)
26+
{
27+
// only Traverse *Type
28+
if ($node instanceof Stmt\Class_) {
29+
if (substr($node->name, -4) !== 'Type') {
30+
return;
31+
}
32+
}
33+
34+
if (!$node instanceof Node\Expr\Array_) {
35+
return;
36+
}
37+
38+
foreach ($node->items as $item) {
39+
if (!$item->key instanceof Node\Scalar\String_) {
40+
continue;
41+
}
42+
43+
if ($item->key->value !== 'empty_value') {
44+
continue;
45+
}
46+
47+
if ($item->value instanceof Node\Scalar\String_) {
48+
$this->storeValue($node, $item);
49+
} elseif ($item->value instanceof Node\Expr\Array_) {
50+
foreach ($item->value->items as $arrayItem) {
51+
$this->storeValue($node, $arrayItem);
52+
}
53+
}
54+
}
55+
}
56+
57+
/**
58+
* @param Node $node
59+
* @param $item
60+
*/
61+
private function storeValue(Node $node, $item)
62+
{
63+
if (!$item->value instanceof Node\Scalar\String_) {
64+
$this->addError($item, 'Form label is not a scalar string');
65+
66+
return;
67+
}
68+
69+
$label = $item->value->value;
70+
if (empty($label)) {
71+
return;
72+
}
73+
74+
$sl = new SourceLocation($label, $this->getAbsoluteFilePath(), $node->getAttribute('startLine'));
75+
$this->collection->addLocation($sl);
76+
}
77+
78+
public function leaveNode(Node $node)
79+
{
80+
}
81+
82+
public function beforeTraverse(array $nodes)
83+
{
84+
}
85+
86+
public function afterTraverse(array $nodes)
87+
{
88+
}
89+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Functional\Visitor\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\FormTypeEmptyValue;
17+
18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
21+
class FormEmptyValueTest extends BasePHPVisitorTest
22+
{
23+
public function testExtract()
24+
{
25+
$collection = $this->getSourceLocations(new FormTypeEmptyValue(), Resources\Php\Symfony\EmptyValueType::class);
26+
27+
$this->assertCount(3, $collection);
28+
29+
$this->assertEquals('gender.empty_value', $collection->get(0)->getMessage());
30+
$this->assertEquals('birthday.form.year', $collection->get(1)->getMessage());
31+
$this->assertEquals('birthday.form.month', $collection->get(2)->getMessage());
32+
}
33+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Translation\Extractor\Tests\Resources\Php\Symfony;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class EmptyValueType extends AbstractType
11+
{
12+
public function buildForm(FormBuilderInterface $builder, array $options)
13+
{
14+
$builder
15+
->add('gender', ChoiceType::class, array(
16+
'choices_as_values' => true,
17+
'empty_value' => 'gender.empty_value', // <-- here
18+
'choices' => array(
19+
'gender.female' => 0,
20+
'gender.male' => 1,
21+
),
22+
));
23+
}
24+
25+
public function configureOptions(OptionsResolver $resolver)
26+
{
27+
$resolver->setDefaults(array(
28+
'empty_value' => array(
29+
'year' => 'birthday.form.year',
30+
'month' => 'birthday.form.month',
31+
),
32+
'error_bubbling' => false,
33+
));
34+
}
35+
36+
37+
}

0 commit comments

Comments
 (0)