Skip to content

Commit

Permalink
minor #3228 Improved how the automatic labels are generated (javiereg…
Browse files Browse the repository at this point in the history
…uiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Improved how the automatic labels are generated

The great Symfony String component makes everything much better and much more readable.

Commits
-------

20a80c6 Improved how the automatic labels are generated
  • Loading branch information
javiereguiluz committed May 16, 2020
2 parents bc2db6f + 20a80c6 commit 3b17380
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Field/Configurator/CommonPreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function Symfony\Component\String\u;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -194,10 +195,22 @@ private function buildRequiredOption(FieldDto $field, EntityDto $entityDto): boo
return !$doctrinePropertyMetadata->get('nullable');
}

// copied from Symfony\Component\Form\FormRenderer::humanize()
// (author: Bernhard Schussek <bschussek@gmail.com>).
private function humanizeString(string $string): string
{
return ucfirst(mb_strtolower(trim(preg_replace(['/([A-Z])/', '/[_\s]+/'], ['_$1', ' '], $string))));
$uString = u($string);
$upperString = $uString->upper()->toString();

// this prevents humanizing all-uppercase labels (e.g. 'UUID' -> 'U u i d')
// and other special labels which look better in uppercase
if ($uString->toString() === $upperString || in_array($upperString, ['ID', 'URL'], true)) {
return $upperString;
}

return $uString
->replaceMatches('/([A-Z])/', '_$1')
->replaceMatches('/[_\s]+/', ' ')
->trim()
->lower()
->title(true);
}
}

0 comments on commit 3b17380

Please sign in to comment.