|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Respect/Validation. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Respect\Validation\Rules; |
|
13
|
|
|
|
|
14
|
|
|
use ReflectionClass; |
|
15
|
|
|
use ReflectionException; |
|
16
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
|
17
|
|
|
use Symfony\Component\Validator\Constraint; |
|
18
|
|
|
use Symfony\Component\Validator\Validation; |
|
19
|
|
|
|
|
20
|
|
|
class Sf extends AbstractRule |
|
21
|
|
|
{ |
|
22
|
|
|
const SYMFONY_CONSTRAINT_NAMESPACE = 'Symfony\Component\Validator\Constraints\%s'; |
|
23
|
|
|
public $name; |
|
24
|
|
|
private $constraint; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct($name, array $params = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->name = ucfirst($name); |
|
29
|
|
|
$this->constraint = $this->createSymfonyConstraint($this->name, $params); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function createSymfonyConstraint($constraintName, array $constraintConstructorParameters = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$fullClassName = sprintf(self::SYMFONY_CONSTRAINT_NAMESPACE, $constraintName); |
|
35
|
|
|
try { |
|
36
|
|
|
$constraintReflection = new ReflectionClass($fullClassName); |
|
37
|
|
|
} catch (ReflectionException $previousException) { |
|
38
|
|
|
$baseExceptionMessage = 'Symfony/Validator constraint "%s" does not exist.'; |
|
39
|
|
|
$exceptionMessage = sprintf($baseExceptionMessage, $constraintName); |
|
40
|
|
|
throw new ComponentException($exceptionMessage, 0, $previousException); |
|
41
|
|
|
} |
|
42
|
|
|
if ($constraintReflection->hasMethod('__construct')) { |
|
43
|
|
|
return $constraintReflection->newInstanceArgs($constraintConstructorParameters); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $constraintReflection->newInstance(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function returnViolationsForConstraint($valueToValidate, Constraint $symfonyConstraint) |
|
50
|
|
|
{ |
|
51
|
|
|
$validator = Validation::createValidator(); // You gotta love those Symfony namings |
|
52
|
|
|
|
|
53
|
|
|
return $validator->validate($valueToValidate, $symfonyConstraint); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function assert($input) |
|
57
|
|
|
{ |
|
58
|
|
|
$violations = $this->returnViolationsForConstraint($input, $this->constraint); |
|
59
|
|
|
if (count($violations) == 0) { |
|
60
|
|
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw $this->reportError((string) $violations); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function validate($input) |
|
67
|
|
|
{ |
|
68
|
|
|
$violations = $this->returnViolationsForConstraint($input, $this->constraint); |
|
69
|
|
|
if (count($violations)) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|