diff --git a/src/Autocomplete/composer.json b/src/Autocomplete/composer.json index 94b94e39918..8698cd4d7d5 100644 --- a/src/Autocomplete/composer.json +++ b/src/Autocomplete/composer.json @@ -29,8 +29,7 @@ "symfony/deprecation-contracts": "^2.5|^3", "symfony/http-foundation": "^6.3|^7.0", "symfony/http-kernel": "^6.3|^7.0", - "symfony/property-access": "^6.3|^7.0", - "symfony/string": "^6.3|^7.0" + "symfony/property-access": "^6.3|^7.0" }, "require-dev": { "doctrine/collections": "^1.6.8|^2.0", diff --git a/src/Autocomplete/src/Form/AsEntityAutocompleteField.php b/src/Autocomplete/src/Form/AsEntityAutocompleteField.php index d707c4b71b5..94119fba8b8 100644 --- a/src/Autocomplete/src/Form/AsEntityAutocompleteField.php +++ b/src/Autocomplete/src/Form/AsEntityAutocompleteField.php @@ -11,8 +11,6 @@ namespace Symfony\UX\Autocomplete\Form; -use Symfony\Component\String\UnicodeString; - /** * All form types that want to expose autocomplete functionality should have this. * @@ -37,13 +35,25 @@ public function getRoute(): string return $this->route; } + /** + * @internal + * + * @param class-string $class + */ public static function shortName(string $class): string { - $string = new UnicodeString($class); + if ($pos = (int) strrpos($class, '\\')) { + $class = substr($class, $pos + 1); + } - return $string->afterLast('\\')->snake()->toString(); + return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class)); } + /** + * @internal + * + * @param class-string $class + */ public static function getInstance(string $class): ?self { $reflectionClass = new \ReflectionClass($class); diff --git a/src/Autocomplete/tests/Unit/Form/AsEntityAutocompleteFieldTest.php b/src/Autocomplete/tests/Unit/Form/AsEntityAutocompleteFieldTest.php new file mode 100644 index 00000000000..c7fabeafd7d --- /dev/null +++ b/src/Autocomplete/tests/Unit/Form/AsEntityAutocompleteFieldTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\Autocomplete\Tests\Unit\Form; + +use PHPUnit\Framework\TestCase; +use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField; +use Symfony\UX\Autocomplete\Tests\Fixtures\Form\ProductType; + +class AsEntityAutocompleteFieldTest extends TestCase +{ + /** + * @dataProvider provideClassNames + */ + public function testShortName(string $shortName, string $className): void + { + $this->assertEquals($shortName, AsEntityAutocompleteField::shortName($className)); + } + + /** + * @return iterable<{string, string}> + */ + public static function provideClassNames(): iterable + { + yield from [ + ['as_entity_autocomplete_field', AsEntityAutocompleteField::class], + ['product_type', ProductType::class], + ['bar', 'Bar'], + ['foo_bar', 'FooBar'], + ['foo_bar', 'Foo\FooBar'], + ['foo_bar', 'Foo\Bar\FooBar'], + ]; + } +}