Skip to content

Commit

Permalink
Merge pull request #1 from Kreyu/dev
Browse files Browse the repository at this point in the history
Prefix requirement fixes and class aliases
  • Loading branch information
Kreyu authored Jul 25, 2019
2 parents 42be232 + beeebbe commit 94b4e53
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class Company
}
```

##### Note:

Constraint and validator classes are aliased to the `Tin` and `TinValidator` classes.

- `Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Tin`
- `Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\TinValidator`


### Pattern validation

By default, the only accepted format is a string of characters without the prefix nor dashes.
Expand Down Expand Up @@ -138,6 +146,13 @@ class Company
}
```

You can use the following parameters in this message:

| Parameter | Description |
| --- | --- |
| `{{ value }}` | The current (invalid value) |
| `{{ pattern }}` | The regular expression pattern used in the validation |

#### Usage of a custom regular expression

If the default functionality does not meet your needs, consider using the `pattern` option:
Expand Down Expand Up @@ -206,6 +221,12 @@ class Company
}
```

You can use the following parameters in this message:

| Parameter | Description |
| --- | --- |
| `{{ value }}` | The current (invalid value) |

## License

The MIT License (MIT). Please see [license file](LICENSE.md) for more information.
2 changes: 2 additions & 0 deletions src/Validator/Constraints/Nip.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ class Nip extends Constraint
public $requirePrefix = false;
public $prefixLength = 2;
}

class_alias(Nip::class, 'Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Tin');
13 changes: 9 additions & 4 deletions src/Validator/Constraints/NipValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class NipValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!preg_match($this->getPattern($constraint), $value)) {
if (!preg_match($pattern = $this->getPattern($constraint), $value)) {
$this->context->buildViolation($constraint->patternMessage)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ pattern }}', $pattern)
->setInvalidValue($value)
->setCode(Nip::INVALID_PATTERN_ERROR)
->addViolation();

Expand All @@ -36,6 +38,7 @@ public function validate($value, Constraint $constraint)
if ($constraint->checksum && !$this->validateChecksum($value, $constraint)) {
$this->context->buildViolation($constraint->checksumMessage)
->setParameter('{{ value }}', $this->formatValue($value))
->setInvalidValue($value)
->setCode(Nip::INVALID_CHECKSUM_ERROR)
->addViolation();

Expand All @@ -50,7 +53,7 @@ public function validate($value, Constraint $constraint)
* @param Constraint|Nip $constraint
* @return string
*/
protected function validateChecksum($value, Constraint $constraint)
public function validateChecksum($value, Constraint $constraint)
{
preg_match_all('!\d+!', $value, $matches);

Expand All @@ -69,7 +72,7 @@ protected function validateChecksum($value, Constraint $constraint)
* @param Constraint|Nip $constraint
* @return string
*/
protected function getPattern(Constraint $constraint)
public function getPattern(Constraint $constraint)
{
if (null !== $constraint->pattern) {
return $constraint->pattern;
Expand All @@ -81,7 +84,7 @@ protected function getPattern(Constraint $constraint)
if ($constraint->prefixLength > 0 && ($constraint->requirePrefix || $constraint->allowPrefix)) {
$pattern .= '([A-z]{' . $constraint->prefixLength . '})';

if ($constraint->allowPrefix) {
if (!$constraint->requirePrefix) {
$pattern .= '?';
}
}
Expand All @@ -105,3 +108,5 @@ protected function getPattern(Constraint $constraint)
return $pattern;
}
}

class_alias(NipValidator::class, 'Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\TinValidator');
41 changes: 34 additions & 7 deletions tests/Validator/Constraints/NipValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@

use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Nip;
use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\NipValidator;
use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\Tin;
use Kreyu\Bundle\NipValidatorBundle\Validator\Constraints\TinValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @property-read NipValidator $validator
*
* @author Sebastian Wróblewski <kontakt@swroblewski.pl>
*/
class NipValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
Expand Down Expand Up @@ -73,6 +80,7 @@ public function testCustomPatternWithInvalidChecksum($value)

$this->buildViolation($constraint->checksumMessage)
->setParameter('{{ value }}', '"'.$value.'"')
->setInvalidValue($value)
->setCode(Nip::INVALID_CHECKSUM_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -127,6 +135,7 @@ public function testCustomChecksumMessage($value)

$this->buildViolation('My custom checksum message')
->setParameter('{{ value }}', '"'.$value.'"')
->setInvalidValue($value)
->setCode(Nip::INVALID_CHECKSUM_ERROR)
->assertRaised();
}
Expand All @@ -145,6 +154,8 @@ public function testCustomPatternMessage($value)

$this->buildViolation('My custom pattern message')
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ pattern }}', $this->validator->getPattern($constraint))
->setInvalidValue($value)
->setCode(Nip::INVALID_PATTERN_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -200,6 +211,8 @@ public function testRequiringDashesShouldNotAcceptWithout($value)

$this->buildViolation($constraint->patternMessage)
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ pattern }}', $this->validator->getPattern($constraint))
->setInvalidValue($value)
->setCode(Nip::INVALID_PATTERN_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -256,19 +269,19 @@ public function testAllowingPrefixShouldAcceptBothWithAndWithout($value)
$this->assertNoViolation();
}

public function getWithPrefix()
public function getWithoutPrefix()
{
return [
['PL 3774988224'],
['EN 3784078972'],
['DE 7745293711'],
['AA 1239664059'],
['BB 1589583841'],
['3774988224'],
['3784078972'],
['7745293711'],
['1239664059'],
['1589583841'],
];
}

/**
* @dataProvider getWithPrefix
* @dataProvider getWithoutPrefix
*/
public function testRequiringPrefixShouldNotAcceptWithout($value)
{
Expand All @@ -281,6 +294,8 @@ public function testRequiringPrefixShouldNotAcceptWithout($value)

$this->buildViolation($constraint->patternMessage)
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ pattern }}', $this->validator->getPattern($constraint))
->setInvalidValue($value)
->setCode(Nip::INVALID_PATTERN_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -327,7 +342,19 @@ public function testInvalidCustomPrefixLength($value)

$this->buildViolation($constraint->patternMessage)
->setParameter('{{ value }}', '"'.$value.'"')
->setParameter('{{ pattern }}', $this->validator->getPattern($constraint))
->setInvalidValue($value)
->setCode(Nip::INVALID_PATTERN_ERROR)
->assertRaised();
}

public function testNipConstraintClassAlias()
{
$this->assertInstanceOf(Nip::class, new Tin());
}

public function testNipValidatorClassAlias()
{
$this->assertInstanceOf(NipValidator::class, new TinValidator());
}
}

0 comments on commit 94b4e53

Please sign in to comment.