Skip to content

Commit 0c00fd4

Browse files
author
Grzegorz Koziński
committed
refactoring
1 parent a279e55 commit 0c00fd4

File tree

4 files changed

+139
-99
lines changed

4 files changed

+139
-99
lines changed

src/Kiczort/PolishValidatorBundle/Validator/Constraints/NipValidator.php

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,36 @@
1111

1212
namespace Kiczort\PolishValidatorBundle\Validator\Constraints;
1313

14+
use Kiczort\PolishValidator\ValidatorInterface;
1415
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
17-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1816

1917
/**
2018
* @author Grzegorz Koziński <gkozinski@gmail.com>
2119
*/
22-
class NipValidator extends ConstraintValidator
20+
class NipValidator extends ValidatorAbstract
2321
{
2422
/**
25-
* Checks if the passed value is valid.
26-
*
27-
* @param mixed $value The value that should be validated
28-
* @param Constraint $constraint The constraint for the validation
23+
* @return ValidatorInterface
2924
*/
30-
public function validate($value, Constraint $constraint)
25+
public function getBaseValidator()
3126
{
32-
if (!$constraint instanceof Nip) {
33-
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Nip');
34-
}
35-
36-
if (null === $value || '' === $value) {
37-
return;
38-
}
39-
40-
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
41-
throw new UnexpectedTypeException($value, 'string');
42-
}
27+
return new \Kiczort\PolishValidator\NipValidator();
28+
}
4329

44-
$value = (string) $value;
30+
/**
31+
* @param Constraint $constraint
32+
* @return array
33+
*/
34+
public function getValidationOptions(Constraint $constraint)
35+
{
36+
return array();
37+
}
4538

46-
$validator = new \Kiczort\PolishValidator\NipValidator();
47-
if (!$validator->isValid($value)) {
48-
if ($this->context instanceof ExecutionContextInterface) {
49-
$this->context->buildViolation($constraint->message)
50-
->setParameter('{{ value }}', $this->formatValue($value))
51-
->addViolation();
52-
} else {
53-
$this->buildViolation($constraint->message)
54-
->setParameter('{{ value }}', $this->formatValue($value))
55-
->addViolation();
56-
}
57-
}
39+
/**
40+
* @return string
41+
*/
42+
public function getValidatorConstraintClass()
43+
{
44+
return __NAMESPACE__ . '\Nip';
5845
}
5946
}

src/Kiczort/PolishValidatorBundle/Validator/Constraints/PeselValidator.php

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,36 @@
1111

1212
namespace Kiczort\PolishValidatorBundle\Validator\Constraints;
1313

14+
use Kiczort\PolishValidator\ValidatorInterface;
1415
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
17-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1816

1917
/**
2018
* @author Grzegorz Koziński <gkozinski@gmail.com>
2119
*/
22-
class PeselValidator extends ConstraintValidator
20+
class PeselValidator extends ValidatorAbstract
2321
{
2422
/**
25-
* Checks if the passed value is valid.
26-
*
27-
* @param mixed $value The value that should be validated
28-
* @param Constraint $constraint The constraint for the validation
23+
* @return ValidatorInterface
2924
*/
30-
public function validate($value, Constraint $constraint)
25+
public function getBaseValidator()
3126
{
32-
if (!$constraint instanceof Pesel) {
33-
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Pesel');
34-
}
35-
36-
if (null === $value || '' === $value) {
37-
return;
38-
}
39-
40-
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
41-
throw new UnexpectedTypeException($value, 'string');
42-
}
27+
return new \Kiczort\PolishValidator\PeselValidator();
28+
}
4329

44-
$value = (string) $value;
30+
/**
31+
* @param Constraint $constraint
32+
* @return array
33+
*/
34+
public function getValidationOptions(Constraint $constraint)
35+
{
36+
return array('strict' => (bool)$constraint->strict);
37+
}
4538

46-
$validator = new \Kiczort\PolishValidator\PeselValidator();
47-
if (!$validator->isValid($value, array('strict' => (bool)$constraint->strict))) {
48-
if ($this->context instanceof ExecutionContextInterface) {
49-
$this->context->buildViolation($constraint->message)
50-
->setParameter('{{ value }}', $this->formatValue($value))
51-
->addViolation();
52-
} else {
53-
$this->buildViolation($constraint->message)
54-
->setParameter('{{ value }}', $this->formatValue($value))
55-
->addViolation();
56-
}
57-
}
39+
/**
40+
* @return string
41+
*/
42+
public function getValidatorConstraintClass()
43+
{
44+
return __NAMESPACE__ . '\Pesel';
5845
}
5946
}

src/Kiczort/PolishValidatorBundle/Validator/Constraints/RegonValidator.php

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,36 @@
1111

1212
namespace Kiczort\PolishValidatorBundle\Validator\Constraints;
1313

14+
use Kiczort\PolishValidator\ValidatorInterface;
1415
use Symfony\Component\Validator\Constraint;
15-
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Context\ExecutionContextInterface;
17-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1816

1917
/**
2018
* @author Grzegorz Koziński <gkozinski@gmail.com>
2119
*/
22-
class RegonValidator extends ConstraintValidator
20+
class RegonValidator extends ValidatorAbstract
2321
{
2422
/**
25-
* Checks if the passed value is valid.
26-
*
27-
* @param mixed $value The value that should be validated
28-
* @param Constraint $constraint The constraint for the validation
23+
* @return ValidatorInterface
2924
*/
30-
public function validate($value, Constraint $constraint)
25+
public function getBaseValidator()
3126
{
32-
if (!$constraint instanceof Regon) {
33-
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Regon');
34-
}
35-
36-
if (null === $value || '' === $value) {
37-
return;
38-
}
39-
40-
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
41-
throw new UnexpectedTypeException($value, 'string');
42-
}
27+
return new \Kiczort\PolishValidator\RegonValidator();
28+
}
4329

44-
$value = (string) $value;
30+
/**
31+
* @param Constraint $constraint
32+
* @return array
33+
*/
34+
public function getValidationOptions(Constraint $constraint)
35+
{
36+
return array();
37+
}
4538

46-
$validator = new \Kiczort\PolishValidator\RegonValidator();
47-
if (!$validator->isValid($value)) {
48-
if ($this->context instanceof ExecutionContextInterface) {
49-
$this->context->buildViolation($constraint->message)
50-
->setParameter('{{ value }}', $this->formatValue($value))
51-
->addViolation();
52-
} else {
53-
$this->buildViolation($constraint->message)
54-
->setParameter('{{ value }}', $this->formatValue($value))
55-
->addViolation();
56-
}
57-
}
39+
/**
40+
* @return string
41+
*/
42+
public function getValidatorConstraintClass()
43+
{
44+
return __NAMESPACE__ . '\Regon';
5845
}
5946
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Polish Validator Bundle package.
5+
*
6+
* (c) Grzegorz Koziński
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 Kiczort\PolishValidatorBundle\Validator\Constraints;
13+
14+
use Kiczort\PolishValidator\ValidatorInterface;
15+
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\ConstraintValidator;
17+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
18+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
20+
21+
/**
22+
* @author Grzegorz Koziński <gkozinski@gmail.com>
23+
*/
24+
abstract class ValidatorAbstract extends ConstraintValidator
25+
{
26+
/**
27+
* @return ValidatorInterface
28+
*/
29+
abstract public function getBaseValidator();
30+
31+
/**
32+
* @param Constraint $constraint
33+
* @return array
34+
*/
35+
abstract public function getValidationOptions(Constraint $constraint);
36+
37+
/**
38+
* @return string
39+
*/
40+
abstract public function getValidatorConstraintClass();
41+
42+
/**
43+
* Checks if the passed value is valid.
44+
*
45+
* @param mixed $value The value that should be validated
46+
* @param Constraint $constraint The constraint for the validation
47+
*/
48+
public function validate($value, Constraint $constraint)
49+
{
50+
$constraintClass = $this->getValidatorConstraintClass();
51+
52+
if (get_class($constraint) !== $constraintClass) {
53+
throw new UnexpectedTypeException($constraint, $constraintClass);
54+
}
55+
56+
if (null === $value || '' === $value) {
57+
return;
58+
}
59+
60+
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
61+
throw new UnexpectedTypeException($value, 'string');
62+
}
63+
64+
$value = (string) $value;
65+
66+
$validator = $this->getBaseValidator();
67+
if (!$validator->isValid($value, $this->getValidationOptions($constraint))) {
68+
if ($this->context instanceof ExecutionContextInterface) {
69+
$this->context->buildViolation($constraint->message)
70+
->setParameter('{{ value }}', $this->formatValue($value))
71+
->addViolation();
72+
} else {
73+
$this->buildViolation($constraint->message)
74+
->setParameter('{{ value }}', $this->formatValue($value))
75+
->addViolation();
76+
}
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)