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

Skip to content

[Validator] Added HostnameValidator #31518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
* feature #32446 [Lock] rename and deprecate Factory into LockFactory (Simperfit)
* feature #31975 Dynamic bundle assets (garak)
* feature #32429 [VarDumper] Let browsers trigger their own search on double CMD/CTRL + F (ogizanagi)
* feature #32198 [Lock] Split "StoreInterface" into multiple interfaces with less responsability (Simperfit)
* feature #32198 [Lock] Split "StoreInterface" into multiple interfaces with less responsibility (Simperfit)
* feature #31511 [Validator] Allow to use property paths to get limits in range constraint (Lctrs)
* feature #32424 [Console] don't redraw progress bar more than every 100ms by default (nicolas-grekas)
* feature #27905 [MonologBridge] Monolog 2 compatibility (derrabus)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
}

if (!\in_array($mappingConfig['type'], ['xml', 'yml', 'annotation', 'php', 'staticphp'])) {
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')
));
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')));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted.

}
}

Expand Down
1 change: 0 additions & 1 deletion src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ abstract class DoctrineType extends AbstractType implements ResetInterface
*
* For backwards compatibility, objects are cast to strings by default.
*
*
* @internal This method is public to be usable as callback. It should not
* be used in user code.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ public function refreshUser(UserInterface $user)
// That's the case when the user has been changed by a form with
// validation errors.
if (!$id = $this->getClassMetadata()->getIdentifierValues($user)) {
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.'
);
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.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted.

}

$refreshedUser = $repository->find($id);
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.1.0
-----
* added the `Hostname` constraint and validator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing blank line above the entry.


5.0.0
-----

Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Hostname.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Dmitrii Poddubnyi <[email protected]>
*/
class Hostname extends Constraint
{
const INVALID_HOSTNAME_ERROR = '7057ffdb-0af4-4f7e-bd5e-e9acfa6d7a2d';

protected static $errorNames = [
self::INVALID_HOSTNAME_ERROR => 'INVALID_HOSTNAME_ERROR',
];

public $message = 'This value is not a valid hostname.';
public $requireTld = true;
}
69 changes: 69 additions & 0 deletions src/Symfony/Component/Validator/Constraints/HostnameValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Dmitrii Poddubnyi <[email protected]>
*/
class HostnameValidator extends ConstraintValidator
{
/**
* https://tools.ietf.org/html/rfc2606.
*/
private const RESERVED_TLDS = [
'example',
'invalid',
'localhost',
'test',
];

public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Hostname) {
throw new UnexpectedTypeException($constraint, Hostname::class);
}

if (null === $value || '' === $value) {
return;
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
if ('' === $value) {
return;
}
if (!$this->isValid($value) || ($constraint->requireTld && !$this->hasValidTld($value))) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Hostname::INVALID_HOSTNAME_ERROR)
->addViolation();
}
}

private function isValid(string $domain): bool
{
return false !== filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
}

private function hasValidTld(string $domain): bool
{
return false !== strpos($domain, '.') && !\in_array(substr($domain, strrpos($domain, '.') + 1), self::RESERVED_TLDS, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Dieser Wert sollte zwischen {{ min }} und {{ max }} sein.</target>
</trans-unit>
<trans-unit id="95">
<source>This value is not a valid hostname.</source>
<target>Dieser Wert ist kein gültiger Hostname.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>This value should be between {{ min }} and {{ max }}.</target>
</trans-unit>
<trans-unit id="95">
<source>This value is not a valid hostname.</source>
<target>This value is not a valid hostname.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Cette valeur doit être comprise entre {{ min }} et {{ max }}.</target>
</trans-unit>
<trans-unit id="95">
<source>This value is not a valid hostname.</source>
<target>Cette valeur n'est pas un nom d'hôte valide.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
<source>This value should be between {{ min }} and {{ max }}.</source>
<target>Значение должно быть между {{ min }} и {{ max }}.</target>
</trans-unit>
<trans-unit id="95">
<source>This value is not a valid hostname.</source>
<target>Значение не является корректным именем хоста.</target>
</trans-unit>
</body>
</file>
</xliff>
Loading