Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 98 lines (78 sloc) 2.702 kb
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
1 <?php
2
3 /*
64e1cf2 @blue-eyes normalized license messages in PHP files
blue-eyes authored
4 * This file is part of the Symfony package.
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
5 *
fec10a4 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
7 *
64e1cf2 @blue-eyes normalized license messages in PHP files
blue-eyes authored
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
10 */
11
64e1cf2 @blue-eyes normalized license messages in PHP files
blue-eyes authored
12 namespace Symfony\Component\Validator\Constraints;
13
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
14 use Symfony\Component\Validator\Constraint;
15 use Symfony\Component\Validator\ConstraintValidator;
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17
18 /**
19 * Validates whether a value is a valid IP address
20 *
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::isVa...
webmozart authored
21 * @author Bernhard Schussek <bschussek@gmail.com>
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
22 * @author Joseph Bielawski <stloyd@gmail.com>
be4f2a0 @fabpot [Validator] tagged the public @api
fabpot authored
23 *
24 * @api
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
25 */
26 class IpValidator extends ConstraintValidator
27 {
28 /**
9bde527 @danielholmes [Validator] Added inheritDoc phpdoc for validate methods
danielholmes authored
29 * {@inheritDoc}
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
30 */
734609c @webmozart [Validator] Renamed ConstraintValidatorInterface::isValid() to validate(...
webmozart authored
31 public function validate($value, Constraint $constraint)
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
32 {
33 if (null === $value || '' === $value) {
529a58d @webmozart [Validator] Removed return value from ConstraintValidatorInterface::isVa...
webmozart authored
34 return;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
35 }
36
8c43fee @pborreli [Validator] Fixing method_exists
pborreli authored
37 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
38 throw new UnexpectedTypeException($value, 'string');
39 }
40
47cb866 @hason [Validator] fixed CS
hason authored
41 $value = (string) $value;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
42
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
43 switch ($constraint->version) {
44 case Ip::V4:
45 $flag = FILTER_FLAG_IPV4;
46 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
47
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
48 case Ip::V6:
49 $flag = FILTER_FLAG_IPV6;
50 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
51
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
52 case Ip::V4_NO_PRIV:
53 $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE;
54 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
55
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
56 case Ip::V6_NO_PRIV:
57 $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE;
58 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
59
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
60 case Ip::ALL_NO_PRIV:
61 $flag = FILTER_FLAG_NO_PRIV_RANGE;
62 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
63
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
64 case Ip::V4_NO_RES:
65 $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_RES_RANGE;
66 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
67
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
68 case Ip::V6_NO_RES:
69 $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_RES_RANGE;
70 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
71
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
72 case Ip::ALL_NO_RES:
73 $flag = FILTER_FLAG_NO_RES_RANGE;
74 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
75
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
76 case Ip::V4_ONLY_PUBLIC:
77 $flag = FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
78 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
79
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
80 case Ip::V6_ONLY_PUBLIC:
81 $flag = FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
82 break;
83
84 case Ip::ALL_ONLY_PUBLIC:
85 $flag = FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE;
86 break;
87
88 default:
89 $flag = null;
90 break;
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
91 }
92
edbf281 @stloyd Refactoring of IpValidator to use native php filter extension, also addi...
stloyd authored
93 if (!filter_var($value, FILTER_VALIDATE_IP, $flag)) {
d7cb249 @webmozart [Validator] Deprecated ConstraintValidator methods setMessage(), getMess...
webmozart authored
94 $this->context->addViolation($constraint->message, array('{{ value }}' => $value));
9370e1c @webmozart [Validator] Implemented @Ip constraint
webmozart authored
95 }
96 }
2d8d332 @fabpot fixed CS
fabpot authored
97 }
Something went wrong with that request. Please try again.