Skip to content

Commit

Permalink
[Validator] The default option name can now be omitted when defining …
Browse files Browse the repository at this point in the history
…constraints as annotations
  • Loading branch information
webmozart committed Sep 10, 2013
1 parent 599c865 commit 5499a29
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Component/Validator/Constraint.php
Expand Up @@ -87,8 +87,9 @@ public function __construct($options = null)
$invalidOptions = array();
$missingOptions = array_flip((array) $this->getRequiredOptions());

if (is_array($options) && count($options) == 1 && isset($options['value'])) {
$options = $options['value'];
if (is_array($options) && count($options) >= 1 && isset($options['value']) && !property_exists($this, 'value')) {
$options[$this->getDefaultOption()] = $options['value'];
unset($options['value']);
}

if (is_array($options) && count($options) > 0 && is_string(key($options))) {
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintTest.php
Expand Up @@ -15,6 +15,8 @@
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintC;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValue;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintWithValueAsDefault;

class ConstraintTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -71,6 +73,30 @@ public function testSetDefaultPropertyDoctrineStyle()
$this->assertEquals('foo', $constraint->property2);
}

public function testSetDefaultPropertyDoctrineStylePlusOtherProperty()
{
$constraint = new ConstraintA(array('value' => 'foo', 'property1' => 'bar'));

$this->assertEquals('foo', $constraint->property2);
$this->assertEquals('bar', $constraint->property1);
}

public function testSetDefaultPropertyDoctrineStyleWhenDefaultPropertyIsNamedValue()
{
$constraint = new ConstraintWithValueAsDefault(array('value' => 'foo'));

$this->assertEquals('foo', $constraint->value);
$this->assertNull($constraint->property);
}

public function testDontSetDefaultPropertyIfValuePropertyExists()
{
$constraint = new ConstraintWithValue(array('value' => 'foo'));

$this->assertEquals('foo', $constraint->value);
$this->assertNull($constraint->property);
}

public function testSetUndefinedDefaultProperty()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
Expand Down
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Constraint;

/** @Annotation */
class ConstraintWithValue extends Constraint
{
public $property;
public $value;

public function getDefaultOption()
{
return 'property';
}

public function getTargets()
{
return array(self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT);
}
}
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Fixtures;

use Symfony\Component\Validator\Constraint;

/** @Annotation */
class ConstraintWithValueAsDefault extends Constraint
{
public $property;
public $value;

public function getDefaultOption()
{
return 'value';
}

public function getTargets()
{
return array(self::PROPERTY_CONSTRAINT, self::CLASS_CONSTRAINT);
}
}

0 comments on commit 5499a29

Please sign in to comment.