1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Contract\Core\Normalisable;
6: use Salient\Contract\Core\NormaliserFactory;
7: use Salient\Utility\Str;
8: use Closure;
9:
10: /**
11: * Implements NormaliserFactory and Normalisable
12: *
13: * @see NormaliserFactory
14: * @see Normalisable
15: *
16: * @phpstan-require-implements NormaliserFactory
17: * @phpstan-require-implements Normalisable
18: */
19: trait HasNormaliser
20: {
21: /** @var array<string,Closure(string $name, bool $greedy=, string...$hints): string> */
22: private static $Normaliser = [];
23:
24: /**
25: * @inheritDoc
26: */
27: public static function getNormaliser(): Closure
28: {
29: return fn(string $name): string =>
30: Str::snake($name);
31: }
32:
33: /**
34: * @inheritDoc
35: */
36: final public static function normalise(
37: string $name,
38: bool $greedy = true,
39: string ...$hints
40: ): string {
41: $normaliser = self::$Normaliser[static::class]
42: ??= static::getNormaliser();
43:
44: return $normaliser($name, $greedy, ...$hints);
45: }
46: }
47: