Skip to content

Commit bd6dff3

Browse files
Added constraints
1 parent 5b1cd5e commit bd6dff3

File tree

11 files changed

+385
-0
lines changed

11 files changed

+385
-0
lines changed

src/Bundle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation;
9+
10+
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
11+
12+
/**
13+
* @author Vuong Minh <vuongxuongminh@gmail.com>
14+
* @since 1.0.0
15+
*/
16+
class Bundle extends BaseBundle
17+
{
18+
19+
protected $name = 'PHPVietValidationBundle';
20+
21+
}

src/Constraints/IdVN.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use Symfony\Component\Validator\Constraint;
11+
12+
/**
13+
* @author Vuong Minh <vuongxuongminh@gmail.com>
14+
* @since 1.0.0
15+
*/
16+
class IdVN extends Constraint
17+
{
18+
const ID_VN_ERROR = '6d8d6065-0b7b-4e02-a65c-9500a511e628';
19+
20+
protected static $errorNames = [
21+
self::ID_VN_ERROR => 'ID_VN_ERROR',
22+
];
23+
24+
public $message = 'This is not a valid Vietnam id number.';
25+
}

src/Constraints/IdVNValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use PHPViet\Validation\Validator as ConcreteValidator;
11+
use Symfony\Component\Validator\Constraint;
12+
use Symfony\Component\Validator\ConstraintValidator;
13+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class IdVNValidator extends ConstraintValidator
20+
{
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function validate($value, Constraint $constraint): void
26+
{
27+
if (!$constraint instanceof IdVN) {
28+
throw new UnexpectedTypeException($constraint, IdVN::class);
29+
}
30+
31+
if (false === ConcreteValidator::idVN()->validate($value)) {
32+
$this->context->buildViolation($constraint->message)
33+
->setParameter('{{ value }}', $this->formatValue($value))
34+
->setCode(IdVN::ID_VN_ERROR)
35+
->addViolation();
36+
}
37+
}
38+
39+
}

src/Constraints/IpVN.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use Symfony\Component\Validator\Constraint;
11+
use PHPViet\Validation\Rules\IpVN as BaseIpVN;
12+
13+
/**
14+
* @author Vuong Minh <vuongxuongminh@gmail.com>
15+
* @since 1.0.0
16+
*/
17+
class IpVN extends Constraint
18+
{
19+
const IPV4 = BaseIpVN::IPV4;
20+
21+
const IPV6 = BaseIpVN::IPV6;
22+
23+
const IP_VN_ERROR = 'a21abd13-9fc6-4319-a07a-9dfdc0b33719';
24+
25+
const IPV4_VN_ERROR = '6b162d27-d99e-4c4c-aadd-179a8b37ebc8';
26+
27+
const IPV6_VN_ERROR = '1c417ab3-734c-4694-a52e-af4bcfc8dd4f';
28+
29+
protected static $errorNames = [
30+
self::IP_VN_ERROR => 'IP_VN_ERROR',
31+
self::IPV4_VN_ERROR => 'IPV4_VN_ERROR',
32+
self::IPV6_VN_ERROR => 'IPV6_VN_ERROR',
33+
];
34+
35+
public $message;
36+
37+
public $version;
38+
39+
public function __construct($options = null)
40+
{
41+
parent::__construct($options);
42+
43+
if (null === $this->message) {
44+
switch ($this->version) {
45+
case self::IPV4:
46+
$this->message = 'This is not a valid Vietnam ipv4.';
47+
break;
48+
case self::IPV6:
49+
$this->message = 'This is not a valid Vietnam ipv6.';
50+
break;
51+
default:
52+
$this->message = 'This is not a valid Vietnam ip.';
53+
break;
54+
}
55+
}
56+
}
57+
}

src/Constraints/IpVNValidator.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use PHPViet\Validation\Validator as ConcreteValidator;
11+
use Symfony\Component\Validator\Constraint;
12+
use Symfony\Component\Validator\ConstraintValidator;
13+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class IpVNValidator extends ConstraintValidator
20+
{
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function validate($value, Constraint $constraint): void
26+
{
27+
if (!$constraint instanceof IpVN) {
28+
throw new UnexpectedTypeException($constraint, IpVN::class);
29+
}
30+
31+
if (false === ConcreteValidator::ipVN($constraint->version)->validate($value)) {
32+
$this->context->buildViolation($constraint->message)
33+
->setParameter('{{ value }}', $this->formatValue($value))
34+
->setCode($this->getCode($constraint->version))
35+
->addViolation();
36+
}
37+
}
38+
39+
protected function getCode(int $ipVersion): string
40+
{
41+
switch ($ipVersion) {
42+
case IpVN::IPV4:
43+
return IpVN::IPV4_VN_ERROR;
44+
case IpVN::IPV6:
45+
return IpVN::IPV6_VN_ERROR;
46+
default:
47+
return IpVN::IP_VN_ERROR;
48+
}
49+
}
50+
51+
}

src/Constraints/LandLineVN.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use Symfony\Component\Validator\Constraint;
11+
12+
/**
13+
* @author Vuong Minh <vuongxuongminh@gmail.com>
14+
* @since 1.0.0
15+
*/
16+
class LandLineVN extends Constraint
17+
{
18+
const LAND_LINE_VN_ERROR = '61b40d48-c1e1-41d7-afea-7f6e007db510';
19+
20+
protected static $errorNames = [
21+
self::LAND_LINE_VN_ERROR => 'LAND_LINE_VN_ERROR',
22+
];
23+
24+
public $message = 'This is not a valid Vietnam land line phone number.';
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use PHPViet\Validation\Validator as ConcreteValidator;
11+
use Symfony\Component\Validator\Constraint;
12+
use Symfony\Component\Validator\ConstraintValidator;
13+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class LandLineVNValidator extends ConstraintValidator
20+
{
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function validate($value, Constraint $constraint): void
26+
{
27+
if (!$constraint instanceof LandLineVN) {
28+
throw new UnexpectedTypeException($constraint, LandLineVN::class);
29+
}
30+
31+
if (false === ConcreteValidator::landLineVN()->validate($value)) {
32+
$this->context->buildViolation($constraint->message)
33+
->setParameter('{{ value }}', $this->formatValue($value))
34+
->setCode(LandLineVN::LAND_LINE_VN_ERROR)
35+
->addViolation();
36+
}
37+
}
38+
39+
}

src/Constraints/MobileVN.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use Symfony\Component\Validator\Constraint;
11+
12+
/**
13+
* @Annotation
14+
*
15+
* @author Vuong Minh <vuongxuongminh@gmail.com>
16+
* @since 1.0.0
17+
*/
18+
class MobileVN extends Constraint
19+
{
20+
const MOBILE_VN_ERROR = '07b98258-11ff-4e2c-963c-4c075f83688f';
21+
22+
protected static $errorNames = [
23+
self::MOBILE_VN_ERROR => 'MOBILE_VN_ERROR',
24+
];
25+
26+
public $message = 'This is not a valid Vietnam mobile phone number.';
27+
}

src/Constraints/MobileVNValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @link https://github.com/vuongxuongminh/symfony-validation
4+
* @copyright Copyright (c) 2019 Vuong Xuong Minh
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace PHPViet\Symfony\Validation\Constraints;
9+
10+
use PHPViet\Validation\Validator as ConcreteValidator;
11+
use Symfony\Component\Validator\Constraint;
12+
use Symfony\Component\Validator\ConstraintValidator;
13+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14+
15+
/**
16+
* @author Vuong Minh <vuongxuongminh@gmail.com>
17+
* @since 1.0.0
18+
*/
19+
class MobileVNValidator extends ConstraintValidator
20+
{
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function validate($value, Constraint $constraint): void
26+
{
27+
if (!$constraint instanceof MobileVN) {
28+
throw new UnexpectedTypeException($constraint, MobileVN::class);
29+
}
30+
31+
if (false === ConcreteValidator::mobileVN()->validate($value)) {
32+
$this->context->buildViolation($constraint->message)
33+
->setParameter('{{ value }}', $this->formatValue($value))
34+
->setCode(MobileVN::MOBILE_VN_ERROR)
35+
->addViolation();
36+
}
37+
}
38+
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0"?>
2+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3+
<file source-language="en" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1">
6+
<source>This is not a valid Vietnam id number.</source>
7+
<target>This is not a valid Vietnam id number.</target>
8+
</trans-unit>
9+
<trans-unit id="2">
10+
<source>This is not a valid Vietnam ipv4.</source>
11+
<target>This is not a valid Vietnam ipv4.</target>
12+
</trans-unit>
13+
<trans-unit id="3">
14+
<source>This is not a valid Vietnam ipv6.</source>
15+
<target>This is not a valid Vietnam ipv6.</target>
16+
</trans-unit>
17+
<trans-unit id="4">
18+
<source>This is not a valid Vietnam ip.</source>
19+
<target>This is not a valid Vietnam ip.</target>
20+
</trans-unit>
21+
<trans-unit id="5">
22+
<source>This is not a valid Vietnam land line phone number.</source>
23+
<target>This is not a valid Vietnam land line phone number.</target>
24+
</trans-unit>
25+
<trans-unit id="6">
26+
<source>This is not a valid Vietnam mobile phone number.</source>
27+
<target>This is not a valid Vietnam mobile phone number.</target>
28+
</trans-unit>
29+
</body>
30+
</file>
31+
</xliff>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0"?>
2+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3+
<file source-language="en" datatype="plaintext" original="file.ext">
4+
<body>
5+
<trans-unit id="1">
6+
<source>This is not a valid Vietnam id number.</source>
7+
<target>Giá trị không phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.</target>
8+
</trans-unit>
9+
<trans-unit id="2">
10+
<source>This is not a valid Vietnam ipv4.</source>
11+
<target>Giá trị không phải là địa chỉ ipv4 của Việt Nam.</target>
12+
</trans-unit>
13+
<trans-unit id="3">
14+
<source>This is not a valid Vietnam ipv6.</source>
15+
<target>Giá trị không phải là địa chỉ ipv6 của Việt Nam.</target>
16+
</trans-unit>
17+
<trans-unit id="4">
18+
<source>This is not a valid Vietnam ip.</source>
19+
<target>Giá trị không phải là địa chỉ ip của Việt Nam.</target>
20+
</trans-unit>
21+
<trans-unit id="5">
22+
<source>This is not a valid Vietnam land line phone number.</source>
23+
<target>Giá trị không phải là số điện thoại bàn tại Việt Nam.</target>
24+
</trans-unit>
25+
<trans-unit id="6">
26+
<source>This is not a valid Vietnam mobile phone number.</source>
27+
<target>Giá trị không phải là số di động tại Việt Nam.</target>
28+
</trans-unit>
29+
</body>
30+
</file>
31+
</xliff>

0 commit comments

Comments
 (0)