forked from doctrine/annotations
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving validation to metadata objects
- Loading branch information
1 parent
5a4a16e
commit 08d35db
Showing
32 changed files
with
600 additions
and
263 deletions.
There are no files selected for viewing
24 changes: 0 additions & 24 deletions
24
lib/Doctrine/Annotations/Assembler/Validator/Exception/InvalidValue.php
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
lib/Doctrine/Annotations/Assembler/Validator/TargetValidator.php
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
lib/Doctrine/Annotations/Assembler/Validator/ValueValidator.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
lib/Doctrine/Annotations/Metadata/Constraint/CompositeConstraint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
final class CompositeConstraint implements Constraint | ||
{ | ||
/** @var Constraint[] */ | ||
private $constraints; | ||
|
||
public function __construct(Constraint ...$constraints) | ||
{ | ||
$this->constraints = $constraints; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function validate($value) : bool | ||
{ | ||
foreach ($this->constraints as $constraint) { | ||
$constraint->validate($value); | ||
} | ||
|
||
return true; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
lib/Doctrine/Annotations/Metadata/Constraint/Constraint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
interface Constraint | ||
{ | ||
/** | ||
* @param mixed $value | ||
* | ||
* @return true | ||
* | ||
* @throws ConstraintNotFulfilled | ||
*/ | ||
public function validate($value) : bool; | ||
} |
17 changes: 17 additions & 0 deletions
17
lib/Doctrine/Annotations/Metadata/Constraint/ConstraintNotFulfilled.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
use Doctrine\Annotations\Metadata\ValidationException; | ||
use Exception; | ||
use Throwable; | ||
|
||
abstract class ConstraintNotFulfilled extends Exception implements ValidationException | ||
{ | ||
protected function __construct(string $message, ?Throwable $previous = null) | ||
{ | ||
parent::__construct($message, 0, $previous); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
lib/Doctrine/Annotations/Metadata/Constraint/EnumConstraint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
use function in_array; | ||
|
||
final class EnumConstraint implements Constraint | ||
{ | ||
/** @var mixed[] */ | ||
private $allowedValues; | ||
|
||
/** | ||
* @param mixed[] $allowedValues | ||
*/ | ||
public function __construct(array $allowedValues) | ||
{ | ||
$this->allowedValues = $allowedValues; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function validate($value) : bool | ||
{ | ||
if (! in_array($value, $this->allowedValues)) { | ||
throw InvalidValue::new($this->allowedValues, $value); | ||
} | ||
|
||
return true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/Doctrine/Annotations/Metadata/Constraint/InvalidType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
use function sprintf; | ||
|
||
final class InvalidType extends ConstraintNotFulfilled | ||
{ | ||
/** | ||
* @param mixed $value | ||
*/ | ||
public static function new(string $typeDescription, $value) : self | ||
{ | ||
// TODO: Describe value | ||
return new self( | ||
sprintf( | ||
'Invalid value "%s" fo type %s.', | ||
$value, | ||
$typeDescription | ||
) | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/Doctrine/Annotations/Metadata/Constraint/InvalidValue.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
use function gettype; | ||
use function implode; | ||
use function sprintf; | ||
|
||
final class InvalidValue extends ConstraintNotFulfilled | ||
{ | ||
/** | ||
* @param mixed[] $allowedValues | ||
* @param mixed $value | ||
*/ | ||
public static function new(array $allowedValues, $value) : self | ||
{ | ||
// TODO: Describe values | ||
return new self( | ||
sprintf( | ||
'Invalid value "%s" for allowed values: "%s".', | ||
gettype($value), | ||
implode(',', $allowedValues) | ||
) | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
lib/Doctrine/Annotations/Metadata/Constraint/MissingRequiredValue.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Annotations\Metadata\Constraint; | ||
|
||
final class MissingRequiredValue extends ConstraintNotFulfilled | ||
{ | ||
public static function new() : self | ||
{ | ||
return new self('Required value is null'); | ||
} | ||
} |
Oops, something went wrong.