| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 Symfony\Component\Translation\Extractor\Visitor;
|
|
|
13 |
|
|
|
14 |
use PhpParser\Node;
|
|
|
15 |
use PhpParser\NodeVisitor;
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* @author Mathieu Santostefano <msantostefano@protonmail.com>
|
|
|
19 |
*
|
|
|
20 |
* Code mostly comes from https://github.com/php-translation/extractor/blob/master/src/Visitor/Php/Symfony/Constraint.php
|
|
|
21 |
*/
|
|
|
22 |
final class ConstraintVisitor extends AbstractVisitor implements NodeVisitor
|
|
|
23 |
{
|
|
|
24 |
private const CONSTRAINT_VALIDATION_MESSAGE_PATTERN = '/[a-zA-Z]*message/i';
|
|
|
25 |
|
|
|
26 |
public function __construct(
|
|
|
27 |
private readonly array $constraintClassNames = []
|
|
|
28 |
) {
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function beforeTraverse(array $nodes): ?Node
|
|
|
32 |
{
|
|
|
33 |
return null;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function enterNode(Node $node): ?Node
|
|
|
37 |
{
|
|
|
38 |
if (!$node instanceof Node\Expr\New_ && !$node instanceof Node\Attribute) {
|
|
|
39 |
return null;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
$className = $node instanceof Node\Attribute ? $node->name : $node->class;
|
|
|
43 |
if (!$className instanceof Node\Name) {
|
|
|
44 |
return null;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$parts = $className->parts;
|
|
|
48 |
$isConstraintClass = false;
|
|
|
49 |
|
|
|
50 |
foreach ($parts as $part) {
|
|
|
51 |
if (\in_array($part, $this->constraintClassNames, true)) {
|
|
|
52 |
$isConstraintClass = true;
|
|
|
53 |
|
|
|
54 |
break;
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
if (!$isConstraintClass) {
|
|
|
59 |
return null;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
$arg = $node->args[0] ?? null;
|
|
|
63 |
if (!$arg instanceof Node\Arg) {
|
|
|
64 |
return null;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if ($this->hasNodeNamedArguments($node)) {
|
|
|
68 |
$messages = $this->getStringArguments($node, self::CONSTRAINT_VALIDATION_MESSAGE_PATTERN, true);
|
|
|
69 |
} else {
|
|
|
70 |
if (!$arg->value instanceof Node\Expr\Array_) {
|
|
|
71 |
// There is no way to guess which argument is a message to be translated.
|
|
|
72 |
return null;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$messages = [];
|
|
|
76 |
$options = $arg->value;
|
|
|
77 |
|
|
|
78 |
/** @var Node\Expr\ArrayItem $item */
|
|
|
79 |
foreach ($options->items as $item) {
|
|
|
80 |
if (!$item->key instanceof Node\Scalar\String_) {
|
|
|
81 |
continue;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (!preg_match(self::CONSTRAINT_VALIDATION_MESSAGE_PATTERN, $item->key->value ?? '')) {
|
|
|
85 |
continue;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if (!$item->value instanceof Node\Scalar\String_) {
|
|
|
89 |
continue;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
$messages[] = $item->value->value;
|
|
|
93 |
|
|
|
94 |
break;
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
foreach ($messages as $message) {
|
|
|
99 |
$this->addMessageToCatalogue($message, 'validators', $node->getStartLine());
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return null;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public function leaveNode(Node $node): ?Node
|
|
|
106 |
{
|
|
|
107 |
return null;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public function afterTraverse(array $nodes): ?Node
|
|
|
111 |
{
|
|
|
112 |
return null;
|
|
|
113 |
}
|
|
|
114 |
}
|