Skip to content
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

[make:entity] Prevent entity name from having an accent #1474

Merged
merged 1 commit into from
Mar 12, 2024
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
14 changes: 14 additions & 0 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
if ($input->getArgument('name')) {
if (!$this->verifyEntityName($input->getArgument('name'))) {
throw new \InvalidArgumentException('An entity can only have ASCII letters');
}

return;
}

Expand All @@ -122,6 +126,11 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$question = $this->createEntityClassQuestion($argument->getDescription());
$entityClassName = $io->askQuestion($question);

while (!$this->verifyEntityName($entityClassName)) {
$io->error('An entity can only have ASCII letter")');
$entityClassName = $io->askQuestion($question);
}

$input->setArgument('name', $entityClassName);

if (
Expand Down Expand Up @@ -805,6 +814,11 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $
return $io->askQuestion($question);
}

private function verifyEntityName(string $entityName): bool
{
return preg_match('/^[a-zA-Z\\\\]+$/', $entityName);
}

private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite): ClassSourceManipulator
{
$manipulator = new ClassSourceManipulator(
Expand Down
15 changes: 15 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_does_not_validate_entity_name_with_accent' => [$this->createMakeEntityTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker([
// entity class with accent
'Usé',
// entity class without accent
'User',
// no fields
'',
]);

$this->runEntityTest($runner);
}),
];

yield 'it_creates_a_new_class_and_api_resource' => [$this->createMakeEntityTest()
->addExtraDependencies('api')
->run(function (MakerTestRunner $runner) {
Expand Down
Loading