Skip to content

Commit

Permalink
[Autocomplete] Remove symfony/string dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre committed Oct 12, 2024
1 parent 7a626ef commit b9e3037
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/Autocomplete/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 14 additions & 4 deletions src/Autocomplete/src/Form/AsEntityAutocompleteField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions src/Autocomplete/tests/Unit/Form/AsEntityAutocompleteFieldTest.php
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 <[email protected]>
*
* 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'],
];
}
}

0 comments on commit b9e3037

Please sign in to comment.