2023-10-25 15:01:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\Naming;
|
|
|
|
|
2024-12-01 00:43:07 +00:00
|
|
|
use RectorPrefix202412\Doctrine\Inflector\Inflector;
|
|
|
|
use RectorPrefix202412\Nette\Utils\Strings;
|
2023-10-25 15:01:11 +00:00
|
|
|
final class RectorNamingInflector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
*/
|
2024-11-20 15:58:53 +00:00
|
|
|
private Inflector $inflector;
|
2023-10-25 15:01:11 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @see https://regex101.com/r/VqVvke/3
|
|
|
|
*/
|
|
|
|
private const DATA_INFO_SUFFIX_REGEX = '#^(?<prefix>.+)(?<suffix>Data|Info)$#';
|
|
|
|
public function __construct(Inflector $inflector)
|
|
|
|
{
|
|
|
|
$this->inflector = $inflector;
|
|
|
|
}
|
|
|
|
public function singularize(string $name) : string
|
|
|
|
{
|
|
|
|
$matches = Strings::match($name, self::DATA_INFO_SUFFIX_REGEX);
|
|
|
|
if ($matches === null) {
|
|
|
|
return $this->inflector->singularize($name);
|
|
|
|
}
|
|
|
|
$singularized = $this->inflector->singularize($matches['prefix']);
|
|
|
|
$uninflectable = $matches['suffix'];
|
|
|
|
return $singularized . $uninflectable;
|
|
|
|
}
|
|
|
|
}
|