Skip to content

Commit

Permalink
[Validator] Added abstract method Constraint::targets() to define whe…
Browse files Browse the repository at this point in the history
…ther constraints can be put onto properties, classes or both
  • Loading branch information
Bernhard Schussek committed Jan 19, 2011
1 parent 4b490bb commit 1a32664
Show file tree
Hide file tree
Showing 29 changed files with 246 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,24 @@
*/
abstract class Constraint
{
/**
* The name of the group given to all constraints with no explicit group
* @var string
*/
const DEFAULT_GROUP = 'Default';

/**
* Marks a constraint that can be put onto classes
* @var string
*/
const CLASS_CONSTRAINT = 'class';

/**
* Marks a constraint that can be put onto properties
* @var string
*/
const PROPERTY_CONSTRAINT = 'property';

/**
* @var array
*/
Expand Down Expand Up @@ -173,4 +189,15 @@ public function validatedBy()
{
return get_class($this) . 'Validator';
}

/**
* Returns whether the constraint can be put onto classes, properties or
* both
*
* This method should return one or more of the constants
* Constraint::CLASS_CONSTRAINT and Constraint::PROPERTY_CONSTRAINT.
*
* @return string|array One or more constant values
*/
abstract public function targets();
}
8 changes: 8 additions & 0 deletions Constraints/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function requiredOptions()
{
return array('constraints');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/AssertFalse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class AssertFalse extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should be false';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/AssertTrue.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class AssertTrue extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should be true';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/AssertType.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public function requiredOptions()
{
return array('type');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Blank.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Blank extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should be blank';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ public function defaultOption()
{
return 'choices';
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public function requiredOptions()
{
return array('fields');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Country extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid country';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Date extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid date';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class DateTime extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid datetime';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ class Email extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid email address';
public $checkMX = false;

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ class File extends \Symfony\Component\Validator\Constraint
public $notReadableMessage = 'The file is not readable';
public $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}';
public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ public function __construct($options = null)
throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
}
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Language extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid language';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Locale extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid locale';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public function requiredOptions()
{
return array('limit');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/MaxLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function requiredOptions()
{
return array('limit');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public function requiredOptions()
{
return array('limit');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/MinLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function requiredOptions()
{
return array('limit');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/NotBlank.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class NotBlank extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should not be blank';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/NotNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class NotNull extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should not be null';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Null.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Null extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value should be null';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function requiredOptions()
{
return array('pattern');
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@
class Time extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid time';

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ class Url extends \Symfony\Component\Validator\Constraint
{
public $message = 'This value is not a valid URL';
public $protocols = array('http', 'https');

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
8 changes: 8 additions & 0 deletions Constraints/Valid.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ public function __construct($options = null)
throw new ConstraintDefinitionException('The constraint Valid does not accept any options');
}
}

/**
* {@inheritDoc}
*/
public function targets()
{
return self::PROPERTY_CONSTRAINT;
}
}
Loading

0 comments on commit 1a32664

Please sign in to comment.