|
| 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 | +use Translation\Extractor\Model\SourceLocation; |
| 17 | +use Translation\Extractor\Visitor\Php\BasePHPVisitor; |
| 18 | +use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
| 19 | +use Symfony\Component\Validator\MetadataFactoryInterface as LegacyMetadataFactoryInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author Tobias Nyholm <[email protected]> |
| 23 | + */ |
| 24 | +final class ValidationAnnotation extends BasePHPVisitor implements NodeVisitor |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var MetadataFactoryInterface|LegacyMetadataFactoryInterface |
| 28 | + */ |
| 29 | + private $metadataFactory; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + private $namespace; |
| 35 | + |
| 36 | + /** |
| 37 | + * ValidationExtractor constructor. |
| 38 | + * |
| 39 | + * @param MetadataFactoryInterface|LegacyMetadataFactoryInterface $metadataFactory |
| 40 | + */ |
| 41 | + public function __construct($metadataFactory) |
| 42 | + { |
| 43 | + if (!( |
| 44 | + $metadataFactory instanceof MetadataFactoryInterface |
| 45 | + || $metadataFactory instanceof LegacyMetadataFactoryInterface |
| 46 | + )) { |
| 47 | + throw new \InvalidArgumentException(sprintf('%s expects an instance of MetadataFactoryInterface', get_class($this))); |
| 48 | + } |
| 49 | + $this->metadataFactory = $metadataFactory; |
| 50 | + } |
| 51 | + |
| 52 | + public function beforeTraverse(array $nodes) |
| 53 | + { |
| 54 | + $this->namespace = ''; |
| 55 | + } |
| 56 | + |
| 57 | + public function enterNode(Node $node) |
| 58 | + { |
| 59 | + if ($node instanceof Node\Stmt\Namespace_) { |
| 60 | + if (isset($node->name)) { |
| 61 | + // save the namespace |
| 62 | + $this->namespace = implode('\\', $node->name->parts); |
| 63 | + } |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (!$node instanceof Node\Stmt\Class_) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $name = '' === $this->namespace ? $node->name : $this->namespace.'\\'.$node->name; |
| 73 | + |
| 74 | + if (!class_exists($name)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + $metadata = $this->metadataFactory->getMetadataFor($name); |
| 79 | + if (!$metadata->hasConstraints() && !count($metadata->getConstrainedProperties())) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $this->extractFromConstraints($metadata->constraints); |
| 84 | + foreach ($metadata->members as $members) { |
| 85 | + foreach ($members as $member) { |
| 86 | + $this->extractFromConstraints($member->constraints); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param array $constraints |
| 93 | + */ |
| 94 | + private function extractFromConstraints(array $constraints) |
| 95 | + { |
| 96 | + foreach ($constraints as $constraint) { |
| 97 | + $ref = new \ReflectionClass($constraint); |
| 98 | + $defaultValues = $ref->getDefaultProperties(); |
| 99 | + |
| 100 | + $properties = $ref->getProperties(); |
| 101 | + |
| 102 | + foreach ($properties as $property) { |
| 103 | + $propName = $property->getName(); |
| 104 | + |
| 105 | + // If the property ends with 'Message' |
| 106 | + if (strtolower(substr($propName, -1 * strlen('Message'))) === 'message') { |
| 107 | + // If it is different from the default value |
| 108 | + if ($defaultValues[$propName] !== $constraint->{$propName}) { |
| 109 | + $this->collection->addLocation(new SourceLocation($constraint->{$propName}, $this->getAbsoluteFilePath(), 0, ['domain' => 'validators'])); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public function leaveNode(Node $node) |
| 117 | + { |
| 118 | + } |
| 119 | + |
| 120 | + public function afterTraverse(array $nodes) |
| 121 | + { |
| 122 | + } |
| 123 | +} |
0 commit comments