Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a08c20

Browse files
committed
Added HostnameValidator
1 parent 818e291 commit 8a08c20

File tree

12 files changed

+324
-12
lines changed

12 files changed

+324
-12
lines changed

CHANGELOG-5.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
214214
* feature #32446 [Lock] rename and deprecate Factory into LockFactory (Simperfit)
215215
* feature #31975 Dynamic bundle assets (garak)
216216
* feature #32429 [VarDumper] Let browsers trigger their own search on double CMD/CTRL + F (ogizanagi)
217-
* feature #32198 [Lock] Split "StoreInterface" into multiple interfaces with less responsability (Simperfit)
217+
* feature #32198 [Lock] Split "StoreInterface" into multiple interfaces with less responsibility (Simperfit)
218218
* feature #31511 [Validator] Allow to use property paths to get limits in range constraint (Lctrs)
219219
* feature #32424 [Console] don't redraw progress bar more than every 100ms by default (nicolas-grekas)
220220
* feature #27905 [MonologBridge] Monolog 2 compatibility (derrabus)

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
228228
}
229229

230230
if (!\in_array($mappingConfig['type'], ['xml', 'yml', 'annotation', 'php', 'staticphp'])) {
231-
throw new \InvalidArgumentException(sprintf('Can only configure "xml", "yml", "annotation", "php" or '.
232-
'"staticphp" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. '.
233-
'You can register them by adding a new driver to the '.
234-
'"%s" service definition.', $this->getObjectManagerElementName($objectManagerName.'_metadata_driver')
235-
));
231+
throw new \InvalidArgumentException(sprintf('Can only configure "xml", "yml", "annotation", "php" or '.'"staticphp" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. '.'You can register them by adding a new driver to the '.'"%s" service definition.', $this->getObjectManagerElementName($objectManagerName.'_metadata_driver')));
236232
}
237233
}
238234

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ abstract class DoctrineType extends AbstractType implements ResetInterface
5050
*
5151
* For backwards compatibility, objects are cast to strings by default.
5252
*
53-
*
5453
* @internal This method is public to be usable as callback. It should not
5554
* be used in user code.
5655
*/

src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ public function refreshUser(UserInterface $user)
8787
// That's the case when the user has been changed by a form with
8888
// validation errors.
8989
if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
90-
throw new \InvalidArgumentException('You cannot refresh a user '.
91-
'from the EntityUserProvider that does not contain an identifier. '.
92-
'The user object has to be serialized with its own identifier '.
93-
'mapped by Doctrine.'
94-
);
90+
throw new \InvalidArgumentException('You cannot refresh a user '.'from the EntityUserProvider that does not contain an identifier. '.'The user object has to be serialized with its own identifier '.'mapped by Doctrine.');
9591
}
9692

9793
$refreshedUser = $repository->find($id);

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
* added the `Hostname` constraint and validator
7+
48
5.0.0
59
-----
610

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Dmitrii Poddubnyi <[email protected]>
21+
*/
22+
class Hostname extends Constraint
23+
{
24+
const INVALID_HOSTNAME_ERROR = '7057ffdb-0af4-4f7e-bd5e-e9acfa6d7a2d';
25+
26+
protected static $errorNames = [
27+
self::INVALID_HOSTNAME_ERROR => 'INVALID_HOSTNAME_ERROR',
28+
];
29+
30+
public $message = 'This value is not a valid hostname.';
31+
public $requireTld = true;
32+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
18+
19+
/**
20+
* @author Dmitrii Poddubnyi <[email protected]>
21+
*/
22+
class HostnameValidator extends ConstraintValidator
23+
{
24+
/**
25+
* https://tools.ietf.org/html/rfc2606.
26+
*/
27+
private const RESERVED_TLDS = [
28+
'example',
29+
'invalid',
30+
'localhost',
31+
'test',
32+
];
33+
34+
public function validate($value, Constraint $constraint)
35+
{
36+
if (!$constraint instanceof Hostname) {
37+
throw new UnexpectedTypeException($constraint, Hostname::class);
38+
}
39+
40+
if (null === $value || '' === $value) {
41+
return;
42+
}
43+
44+
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
45+
throw new UnexpectedValueException($value, 'string');
46+
}
47+
48+
$value = (string) $value;
49+
if ('' === $value) {
50+
return;
51+
}
52+
if (!$this->isValid($value) || ($constraint->requireTld && !$this->hasValidTld($value))) {
53+
$this->context->buildViolation($constraint->message)
54+
->setParameter('{{ value }}', $this->formatValue($value))
55+
->setCode(Hostname::INVALID_HOSTNAME_ERROR)
56+
->addViolation();
57+
}
58+
}
59+
60+
private function isValid(string $domain): bool
61+
{
62+
return false !== filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
63+
}
64+
65+
private function hasValidTld(string $domain): bool
66+
{
67+
return false !== strpos($domain, '.') && !\in_array(substr($domain, strrpos($domain, '.') + 1), self::RESERVED_TLDS, true);
68+
}
69+
}

src/Symfony/Component/Validator/Resources/translations/validators.de.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@
366366
<source>This value should be between {{ min }} and {{ max }}.</source>
367367
<target>Dieser Wert sollte zwischen {{ min }} und {{ max }} sein.</target>
368368
</trans-unit>
369+
<trans-unit id="95">
370+
<source>This value is not a valid hostname.</source>
371+
<target>Dieser Wert ist kein gültiger Hostname.</target>
372+
</trans-unit>
369373
</body>
370374
</file>
371375
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@
366366
<source>This value should be between {{ min }} and {{ max }}.</source>
367367
<target>This value should be between {{ min }} and {{ max }}.</target>
368368
</trans-unit>
369+
<trans-unit id="95">
370+
<source>This value is not a valid hostname.</source>
371+
<target>This value is not a valid hostname.</target>
372+
</trans-unit>
369373
</body>
370374
</file>
371375
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@
366366
<source>This value should be between {{ min }} and {{ max }}.</source>
367367
<target>Cette valeur doit être comprise entre {{ min }} et {{ max }}.</target>
368368
</trans-unit>
369+
<trans-unit id="95">
370+
<source>This value is not a valid hostname.</source>
371+
<target>Cette valeur n'est pas un nom d'hôte valide.</target>
372+
</trans-unit>
369373
</body>
370374
</file>
371375
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.ru.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@
366366
<source>This value should be between {{ min }} and {{ max }}.</source>
367367
<target>Значение должно быть между {{ min }} и {{ max }}.</target>
368368
</trans-unit>
369+
<trans-unit id="95">
370+
<source>This value is not a valid hostname.</source>
371+
<target>Значение не является корректным именем хоста.</target>
372+
</trans-unit>
369373
</body>
370374
</file>
371375
</xliff>

0 commit comments

Comments
 (0)