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\NodeVisitor; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Tobias Nyholm <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class FormTypeTitle extends AbstractFormType implements NodeVisitor |
21
|
|
|
{ |
22
|
|
|
use FormTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
5 |
|
public function enterNode(Node $node): ?Node |
28
|
|
|
{ |
29
|
5 |
|
if (!$this->isFormType($node)) { |
30
|
5 |
|
return null; |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
parent::enterNode($node); |
34
|
|
|
|
35
|
4 |
|
if (!$node instanceof Node\Expr\Array_) { |
36
|
4 |
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
$titleNode = null; |
40
|
4 |
|
$domain = null; |
41
|
4 |
|
foreach ($node->items as $item) { |
42
|
4 |
|
if (!$item->key instanceof Node\Scalar\String_) { |
43
|
1 |
|
continue; |
44
|
|
|
} |
45
|
4 |
|
if ('translation_domain' === $item->key->value) { |
46
|
|
|
// Try to find translation domain |
47
|
2 |
|
if ($item->value instanceof Node\Scalar\String_) { |
48
|
2 |
|
$domain = $item->value->value; |
49
|
2 |
|
} elseif ($item->value instanceof Node\Expr\ConstFetch && 'false' === $item->value->name->toString()) { |
50
|
2 |
|
$domain = false; |
51
|
|
|
} |
52
|
4 |
|
} elseif ('attr' === $item->key->value && $item->value instanceof Node\Expr\Array_) { |
53
|
3 |
|
foreach ($item->value->items as $attrValue) { |
54
|
3 |
|
if (!$attrValue->key instanceof Node\Scalar\String_) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
3 |
|
if ('title' === $attrValue->key->value) { |
58
|
3 |
|
$titleNode = $attrValue; |
59
|
|
|
|
60
|
3 |
|
break; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
if (null === $titleNode) { |
67
|
4 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
3 |
|
if ($titleNode->value instanceof Node\Scalar\String_) { |
71
|
2 |
|
$line = $titleNode->value->getAttribute('startLine'); |
72
|
2 |
|
if (null !== $location = $this->getLocation($titleNode->value->value, $line, $titleNode, ['domain' => $domain])) { |
73
|
2 |
|
$this->lateCollect($location); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
2 |
|
$this->addError($titleNode, 'Form field title is not a scalar string'); |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|