-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* Nils Adermann <naderman@naderman.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Validator; | ||
|
||
use Attribute; | ||
use Symfony\Component\Validator\Constraint; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY)] | ||
class NotReservedWord extends Constraint | ||
{ | ||
public string $message = 'This is a reserved word.'; | ||
|
||
public function getTargets(): string | ||
{ | ||
return self::PROPERTY_CONSTRAINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Packagist. | ||
* | ||
* (c) Jordi Boggiano <j.boggiano@seld.be> | ||
* Nils Adermann <naderman@naderman.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Validator; | ||
|
||
use App\Entity\User; | ||
use Symfony\Component\Form\Form; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class NotReservedWordValidator extends ConstraintValidator | ||
{ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof NotReservedWord) { | ||
throw new UnexpectedTypeException($constraint, NotReservedWord::class); | ||
} | ||
|
||
if (!is_string($value)) { | ||
return; | ||
} | ||
|
||
$reservedWords = [ | ||
'composer', | ||
'packagist', | ||
'php', | ||
'automation', // used to describe background workers doing things automatically in audit log | ||
]; | ||
|
||
foreach ($reservedWords as $reservedWord) { | ||
if ($reservedWord === mb_strtolower($value)) { | ||
$this->context | ||
->buildViolation($constraint->message) | ||
->addViolation(); | ||
|
||
return; | ||
} | ||
} | ||
|
||
} | ||
} |