Skip to content

Commit 6406c11

Browse files
committed
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
2 parents ec7b549 + fdfeeec commit 6406c11

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CHANGELOG
1919
-----
2020

2121
* deprecated `Tests\Constraints\AbstractContraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
22+
* added support for PHP constants in YAML configuration files
2223

2324
3.1.0
2425
-----

Mapping/Loader/YamlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function parseNodes(array $nodes)
116116
private function parseFile($path)
117117
{
118118
try {
119-
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS);
119+
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_KEYS_AS_STRINGS | Yaml::PARSE_CONSTANT);
120120
} catch (ParseException $e) {
121121
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
122122
}

Tests/Mapping/Loader/YamlFileLoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ public function testLoadClassMetadata()
124124
$this->assertEquals($expected, $metadata);
125125
}
126126

127+
public function testLoadClassMetadataWithConstants()
128+
{
129+
$loader = new YamlFileLoader(__DIR__.'/mapping-with-constants.yml');
130+
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
131+
132+
$loader->loadClassMetadata($metadata);
133+
134+
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
135+
$expected->addPropertyConstraint('firstName', new Range(array('max' => PHP_INT_MAX)));
136+
137+
$this->assertEquals($expected, $metadata);
138+
}
139+
127140
public function testLoadGroupSequenceProvider()
128141
{
129142
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespaces:
2+
custom: Symfony\Component\Validator\Tests\Fixtures\
3+
4+
Symfony\Component\Validator\Tests\Fixtures\Entity:
5+
properties:
6+
firstName:
7+
- Range:
8+
max: !php/const:PHP_INT_MAX

0 commit comments

Comments
 (0)