Skip to content

Commit f5918fe

Browse files
author
Adam Szaraniec
committed
Support for parsing PHP constants in yaml loader
1 parent 89c3f5c commit f5918fe

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* deprecated `Tests\Constraints\AbstractContraintValidatorTest` in favor of `Test\ConstraintValidatorTestCase`
8+
* added support for PHP constants in YAML configuration files
89

910
3.1.0
1011
-----

src/Symfony/Component/Validator/Mapping/Loader/YamlFileLoader.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Mapping\ClassMetadata;
1515
use Symfony\Component\Yaml\Exception\ParseException;
1616
use Symfony\Component\Yaml\Parser as YamlParser;
17+
use Symfony\Component\Yaml\Yaml;
1718

1819
/**
1920
* Loads validation metadata from a YAML file.
@@ -115,7 +116,7 @@ protected function parseNodes(array $nodes)
115116
private function parseFile($path)
116117
{
117118
try {
118-
$classes = $this->yamlParser->parse(file_get_contents($path));
119+
$classes = $this->yamlParser->parse(file_get_contents($path), Yaml::PARSE_CONSTANT);
119120
} catch (ParseException $e) {
120121
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
121122
}

src/Symfony/Component/Validator/Tests/Mapping/Loader/YamlFileLoaderTest.php

+13
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');
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

src/Symfony/Component/Validator/composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"require-dev": {
2424
"symfony/http-foundation": "~2.8|~3.0",
2525
"symfony/intl": "^2.8.18|^3.2.5",
26-
"symfony/yaml": "~2.8|~3.0",
26+
"symfony/yaml": "~3.2",
2727
"symfony/config": "~2.8|~3.0",
2828
"symfony/expression-language": "~2.8|~3.0",
2929
"symfony/cache": "~3.1",
@@ -32,7 +32,8 @@
3232
"egulias/email-validator": "^1.2.8|~2.0"
3333
},
3434
"conflict": {
35-
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0"
35+
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
36+
"symfony/yaml": "<3.2"
3637
},
3738
"suggest": {
3839
"psr/cache-implementation": "For using the metadata cache.",

0 commit comments

Comments
 (0)