Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
18 / 20
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
KebabCaseSlug
90.00% covered (success)
90.00%
18 / 20
75.00% covered (warning)
75.00%
6 / 8
12.14
0.00% covered (danger)
0.00%
0 / 1
 fromClass
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 fromText
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 humanize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRegularExpression
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toCamelCaseSlug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toPascalCaseSlug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toSnakeCaseSlug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createRandom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\Identifiers;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Attributes\FakeMethod;
6use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
7use Apie\Core\ValueObjects\IsStringWithRegexValueObject;
8use Faker\Generator;
9use ReflectionClass;
10use ReflectionMethod;
11use ReflectionProperty;
12
13/**
14 * Indicate an identifier written with dashes (kebab-case).
15 */
16#[FakeMethod('createRandom')]
17#[Description('lowercase text written in kebab case, for example "example-object"')]
18class KebabCaseSlug implements HasRegexValueObjectInterface
19{
20    use IsStringWithRegexValueObject;
21
22    /**
23     * @param ReflectionClass<covariant object>|ReflectionMethod|ReflectionProperty|string $class
24     */
25    public static function fromClass(ReflectionClass|ReflectionMethod|ReflectionProperty|string $class): self
26    {
27        if (is_object($class)) {
28            if ($class instanceof ReflectionMethod && $class->name === '__invoke') {
29                $class = $class->getDeclaringClass();
30            }
31            $shortName = $class instanceof ReflectionClass ? $class->getShortName() : $class->name;
32            $shortName = preg_replace_callback('/^__(.)/', fn (array $m) => 'magicMethod' . strtoupper($m[1]), $shortName);
33            $short = str_replace('_', '-', preg_replace('/([a-z])([A-Z])/', '$1-$2', $shortName));
34        } else {
35            $short = $class;
36        }
37        return static::fromNative(strtolower($short));
38    }
39
40    public static function fromText(string $text): self
41    {
42        // Replace camelCase with kebab-case
43        $text = preg_replace('/([a-z])([A-Z])/', '$1-$2', $text);
44        // Replace any non-alphanumeric character with dash
45        $text = preg_replace('/[^a-zA-Z0-9]+/', '-', $text);
46        // Convert to lowercase
47        $text = strtolower($text);
48        // Remove leading/trailing dashes
49        $text = trim($text, '-');
50        // Collapse multiple dashes into one
51        $text = preg_replace('/-+/', '-', $text);
52        return static::fromNative($text);
53    }
54
55    public function humanize(): string
56    {
57        return str_replace('-', ' ', $this->internal);
58    }
59
60    public static function getRegularExpression(): string
61    {
62        return '/^[a-z0-9]+(\-[a-z0-9]+)*$/';
63    }
64
65    public function toCamelCaseSlug(): CamelCaseSlug
66    {
67        return new CamelCaseSlug(lcfirst(str_replace('-', '', ucwords($this->internal, '-'))));
68    }
69
70    public function toPascalCaseSlug(): PascalCaseSlug
71    {
72        return new PascalCaseSlug(str_replace('-', '', ucwords($this->internal, '-')));
73    }
74
75    public function toSnakeCaseSlug(): SnakeCaseSlug
76    {
77        return new SnakeCaseSlug(str_replace('-', '_', $this->internal));
78    }
79
80    public static function createRandom(Generator $faker): static
81    {
82        return static::fromNative(CamelCaseSlug::createRandom($faker)->toKebabCaseSlug()->toNative());
83    }
84}