Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0e9d1ea

Browse filesBrowse files
committed
merged branch meandmymonkey/fluent-validatorbuilder (PR symfony#5328)
Commits ------- 1ff081d added tests for ValidatorBuilder fluent interface fec11ae updated docblocks for ValidatorBuilderInterface b5aaf53 added fluent interface to validatorbuilder Discussion ---------- [Validator] Added missing fluent interface to ValidatorBuilder The new ValidatorBuilder class seems to be intended to have a fluent interface, reasoning: - Static Validation::createValidatorBuilder() method exists - Consistency with other builders in the framework - Component README actually uses fluent interface for examples. This was not implemented though. This PR adds the fluent interface. BC Break: No Symfony2 Tests Pass: Yes --------------------------------------------------------------------------- by henrikbjorn at 2012-08-23T09:47:35Z Could you add a test for this? :) --------------------------------------------------------------------------- by bschussek at 2012-08-23T12:04:12Z Great, thanks! :+1: --------------------------------------------------------------------------- by meandmymonkey at 2012-08-23T12:30:40Z @henrikbjorn Yes, will do. --------------------------------------------------------------------------- by meandmymonkey at 2012-08-25T16:21:37Z @henrikbjorn done
2 parents a1e6cfb + 1ff081d commit 0e9d1ea
Copy full SHA for 0e9d1ea

File tree

Expand file treeCollapse file tree

3 files changed

+162
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+162
-0
lines changed
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Validator\Tests;
13+
14+
use Symfony\Component\Validator\ValidatorBuilder;
15+
use Symfony\Component\Validator\ValidatorBuilderInterface;
16+
17+
class ValidatorBuilderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @var ValidatorBuilderInterface
21+
*/
22+
protected $builder;
23+
24+
protected function setUp()
25+
{
26+
$this->builder = new ValidatorBuilder();
27+
}
28+
29+
protected function tearDown()
30+
{
31+
$this->builder = null;
32+
}
33+
34+
public function testAddObjectInitializer()
35+
{
36+
$this->assertSame($this->builder, $this->builder->addObjectInitializer(
37+
$this->getMock('Symfony\Component\Validator\ObjectInitializerInterface')
38+
));
39+
}
40+
41+
public function testAddObjectInitializers()
42+
{
43+
$this->assertSame($this->builder, $this->builder->addObjectInitializers(array()));
44+
}
45+
46+
public function testAddXmlMapping()
47+
{
48+
$this->assertSame($this->builder, $this->builder->addXmlMapping('mapping'));
49+
}
50+
51+
public function testAddXmlMappings()
52+
{
53+
$this->assertSame($this->builder, $this->builder->addXmlMappings(array()));
54+
}
55+
56+
public function testAddYamlMapping()
57+
{
58+
$this->assertSame($this->builder, $this->builder->addYamlMapping('mapping'));
59+
}
60+
61+
public function testAddYamlMappings()
62+
{
63+
$this->assertSame($this->builder, $this->builder->addYamlMappings(array()));
64+
}
65+
66+
public function testAddMethodMapping()
67+
{
68+
$this->assertSame($this->builder, $this->builder->addMethodMapping('mapping'));
69+
}
70+
71+
public function testAddMethodMappings()
72+
{
73+
$this->assertSame($this->builder, $this->builder->addMethodMappings(array()));
74+
}
75+
76+
public function testEnableAnnotationMapping()
77+
{
78+
if (!class_exists('Doctrine\Common\Annotations\AnnotationReader')) {
79+
$this->markTestSkipped('Annotations is required for this test');
80+
}
81+
82+
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
83+
}
84+
85+
public function testDisableAnnotationMapping()
86+
{
87+
$this->assertSame($this->builder, $this->builder->disableAnnotationMapping());
88+
}
89+
90+
public function testSetMetadataFactory()
91+
{
92+
$this->assertSame($this->builder, $this->builder->setMetadataFactory(
93+
$this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface'))
94+
);
95+
}
96+
97+
public function testSetMetadataCache()
98+
{
99+
$this->assertSame($this->builder, $this->builder->setMetadataCache($this->getMock(
100+
'Symfony\Component\Validator\Mapping\Cache\CacheInterface'))
101+
);
102+
}
103+
104+
public function testSetConstraintValidatorFactory()
105+
{
106+
$this->assertSame($this->builder, $this->builder->setConstraintValidatorFactory(
107+
$this->getMock('Symfony\Component\Validator\ConstraintValidatorFactoryInterface'))
108+
);
109+
}
110+
}

‎src/Symfony/Component/Validator/ValidatorBuilder.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ValidatorBuilder.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class ValidatorBuilder implements ValidatorBuilderInterface
8181
public function addObjectInitializer(ObjectInitializerInterface $initializer)
8282
{
8383
$this->initializers[] = $initializer;
84+
85+
return $this;
8486
}
8587

8688
/**
@@ -89,6 +91,8 @@ public function addObjectInitializer(ObjectInitializerInterface $initializer)
8991
public function addObjectInitializers(array $initializers)
9092
{
9193
$this->initializers = array_merge($this->initializers, $initializers);
94+
95+
return $this;
9296
}
9397

9498
/**
@@ -101,6 +105,8 @@ public function addXmlMapping($path)
101105
}
102106

103107
$this->xmlMappings[] = $path;
108+
109+
return $this;
104110
}
105111

106112
/**
@@ -113,6 +119,8 @@ public function addXmlMappings(array $paths)
113119
}
114120

115121
$this->xmlMappings = array_merge($this->xmlMappings, $paths);
122+
123+
return $this;
116124
}
117125

118126
/**
@@ -125,6 +133,8 @@ public function addYamlMapping($path)
125133
}
126134

127135
$this->yamlMappings[] = $path;
136+
137+
return $this;
128138
}
129139

130140
/**
@@ -137,6 +147,8 @@ public function addYamlMappings(array $paths)
137147
}
138148

139149
$this->yamlMappings = array_merge($this->yamlMappings, $paths);
150+
151+
return $this;
140152
}
141153

142154
/**
@@ -149,6 +161,8 @@ public function addMethodMapping($methodName)
149161
}
150162

151163
$this->methodMappings[] = $methodName;
164+
165+
return $this;
152166
}
153167

154168
/**
@@ -161,6 +175,8 @@ public function addMethodMappings(array $methodNames)
161175
}
162176

163177
$this->methodMappings = array_merge($this->methodMappings, $methodNames);
178+
179+
return $this;
164180
}
165181

166182
/**
@@ -181,6 +197,8 @@ public function enableAnnotationMapping(Reader $annotationReader = null)
181197
}
182198

183199
$this->annotationReader = $annotationReader;
200+
201+
return $this;
184202
}
185203

186204
/**
@@ -189,6 +207,8 @@ public function enableAnnotationMapping(Reader $annotationReader = null)
189207
public function disableAnnotationMapping()
190208
{
191209
$this->annotationReader = null;
210+
211+
return $this;
192212
}
193213

194214
/**
@@ -201,6 +221,8 @@ public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactor
201221
}
202222

203223
$this->metadataFactory = $metadataFactory;
224+
225+
return $this;
204226
}
205227

206228
/**
@@ -213,6 +235,8 @@ public function setMetadataCache(CacheInterface $cache)
213235
}
214236

215237
$this->metadataCache = $cache;
238+
239+
return $this;
216240
}
217241

218242
/**
@@ -221,6 +245,8 @@ public function setMetadataCache(CacheInterface $cache)
221245
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory)
222246
{
223247
$this->validatorFactory = $validatorFactory;
248+
249+
return $this;
224250
}
225251

226252
/**

‎src/Symfony/Component/Validator/ValidatorBuilderInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Validator/ValidatorBuilderInterface.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,114 @@ interface ValidatorBuilderInterface
2626
* Adds an object initializer to the validator.
2727
*
2828
* @param ObjectInitializerInterface $initializer The initializer.
29+
*
30+
* @return ValidatorBuilderInterface The builder object.
2931
*/
3032
public function addObjectInitializer(ObjectInitializerInterface $initializer);
3133

3234
/**
3335
* Adds a list of object initializers to the validator.
3436
*
3537
* @param array $initializers The initializer.
38+
*
39+
* @return ValidatorBuilderInterface The builder object.
3640
*/
3741
public function addObjectInitializers(array $initializers);
3842

3943
/**
4044
* Adds an XML constraint mapping file to the validator.
4145
*
4246
* @param string $path The path to the mapping file.
47+
*
48+
* @return ValidatorBuilderInterface The builder object.
4349
*/
4450
public function addXmlMapping($path);
4551

4652
/**
4753
* Adds a list of XML constraint mapping files to the validator.
4854
*
4955
* @param array $paths The paths to the mapping files.
56+
*
57+
* @return ValidatorBuilderInterface The builder object.
5058
*/
5159
public function addXmlMappings(array $paths);
5260

5361
/**
5462
* Adds a YAML constraint mapping file to the validator.
5563
*
5664
* @param string $path The path to the mapping file.
65+
*
66+
* @return ValidatorBuilderInterface The builder object.
5767
*/
5868
public function addYamlMapping($path);
5969

6070
/**
6171
* Adds a list of YAML constraint mappings file to the validator.
6272
*
6373
* @param array $paths The paths to the mapping files.
74+
*
75+
* @return ValidatorBuilderInterface The builder object.
6476
*/
6577
public function addYamlMappings(array $paths);
6678

6779
/**
6880
* Enables constraint mapping using the given static method.
6981
*
7082
* @param string $methodName The name of the method.
83+
*
84+
* @return ValidatorBuilderInterface The builder object.
7185
*/
7286
public function addMethodMapping($methodName);
7387

7488
/**
7589
* Enables constraint mapping using the given static methods.
7690
*
7791
* @param array $methodNames The names of the methods.
92+
*
93+
* @return ValidatorBuilderInterface The builder object.
7894
*/
7995
public function addMethodMappings(array $methodNames);
8096

8197
/**
8298
* Enables annotation based constraint mapping.
8399
*
84100
* @param Reader $annotationReader The annotation reader to be used.
101+
*
102+
* @return ValidatorBuilderInterface The builder object.
85103
*/
86104
public function enableAnnotationMapping(Reader $annotationReader = null);
87105

88106
/**
89107
* Disables annotation based constraint mapping.
108+
*
109+
* @return ValidatorBuilderInterface The builder object.
90110
*/
91111
public function disableAnnotationMapping();
92112

93113
/**
94114
* Sets the class metadata factory used by the validator.
95115
*
96116
* @param ClassMetadataFactoryInterface $metadataFactory The metadata factory.
117+
*
118+
* @return ValidatorBuilderInterface The builder object.
97119
*/
98120
public function setMetadataFactory(ClassMetadataFactoryInterface $metadataFactory);
99121

100122
/**
101123
* Sets the cache for caching class metadata.
102124
*
103125
* @param CacheInterface $cache The cache instance.
126+
*
127+
* @return ValidatorBuilderInterface The builder object.
104128
*/
105129
public function setMetadataCache(CacheInterface $cache);
106130

107131
/**
108132
* Sets the constraint validator factory used by the validator.
109133
*
110134
* @param ConstraintValidatorFactoryInterface $validatorFactory The validator factory.
135+
*
136+
* @return ValidatorBuilderInterface The builder object.
111137
*/
112138
public function setConstraintValidatorFactory(ConstraintValidatorFactoryInterface $validatorFactory);
113139

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.