feature #23039 [Validator] Support for parsing PHP constants in yaml loader (mimol91)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] Support for parsing PHP constants in yaml loader

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        |  -

Yaml components supports PHP constants. It would be great if we could use it also in validator component.

Commits
-------

37afeeae7a Support for parsing PHP constants in yaml loader
This commit is contained in:
Fabien Potencier 2017-07-03 12:57:47 +03:00
commit ccfc4dac66
4 changed files with 23 additions and 1 deletions

View File

@ -19,6 +19,7 @@ CHANGELOG
-----
* deprecated `Tests\Constraints\AbstractContraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
* added support for PHP constants in YAML configuration files
3.1.0
-----

View File

@ -116,7 +116,7 @@ class YamlFileLoader extends FileLoader
private function parseFile($path)
{
try {
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS | Yaml::PARSE_CONSTANT);
} catch (ParseException $e) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
}

View File

@ -124,6 +124,19 @@ class YamlFileLoaderTest extends TestCase
$this->assertEquals($expected, $metadata);
}
public function testLoadClassMetadataWithConstants()
{
$loader = new YamlFileLoader(__DIR__.'/mapping-with-constants.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->addPropertyConstraint('firstName', new Range(array('max' => PHP_INT_MAX)));
$this->assertEquals($expected, $metadata);
}
public function testLoadGroupSequenceProvider()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');

View File

@ -0,0 +1,8 @@
namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\
Symfony\Component\Validator\Tests\Fixtures\Entity:
properties:
firstName:
- Range:
max: !php/const:PHP_INT_MAX