Skip to content

[Validator] Add Base64Encoded constraint #59666

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

Closed
Closed
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
25 changes: 25 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Base64Encoded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;

/**
* Ensures that the value is a valid Base64-encoded.
*
* @author Refat Alsakka <refatalsakka@gmail.com>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Base64Encoded extends Constraint
{
public string $message = 'The string "{{ value }}" is not a valid Base64-encoded value.';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;

/**
* @author Refat Alsakka <refatalsakka@gmail.com>
*/
class Base64EncodedValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Base64Encoded) {
throw new UnexpectedTypeException($constraint, Base64Encoded::class);
}

if (!$this->isValidBase64($value)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();
}
}

private function isValidBase64(string $value): bool
{
return base64_encode(base64_decode($value, true)) == $value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

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

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\Base64Encoded;
use Symfony\Component\Validator\Constraints\Base64EncodedValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @author Refat Alsakka <refatalsakka@gmail.com>
*/
class Base64EncodedValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): Base64EncodedValidator
{
return new Base64EncodedValidator();
}

public function testValidBase64()
{
$this->validator->validate('U3ltZm9ueQ==', new Base64Encoded());
$this->assertNoViolation();
}

public function testInvalidBase64()
{
$this->validator->validate('invalid@base64', new Base64Encoded());

$this->buildViolation('The value "{{ value }}" is not a valid Base64-encoded string.')
->setParameter('{{ value }}', 'invalid@base64')
->assertRaised();
}
}
Loading