Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
19 / 20
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SnakeCaseSlug
95.00% covered (success)
95.00%
19 / 20
87.50% covered (warning)
87.50%
7 / 8
12
0.00% covered (danger)
0.00%
0 / 1
 getRegularExpression
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
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
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
 toKebabCaseSlug
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 underscores and lowercase only(pascal_case).
15 */
16#[FakeMethod('createRandom')]
17#[Description('Lowercase text written with underscores for separate words, for example "example_object"')]
18class SnakeCaseSlug implements HasRegexValueObjectInterface
19{
20    use IsStringWithRegexValueObject;
21
22    public static function getRegularExpression(): string
23    {
24        return '/^[a-z0-9]+(_[a-z0-9]+)*$/';
25    }
26
27    /**
28     * @param ReflectionClass<covariant object>|ReflectionMethod|ReflectionProperty|string $class
29     */
30    public static function fromClass(ReflectionClass|ReflectionMethod|ReflectionProperty|string $class): self
31    {
32        if (is_object($class)) {
33            if ($class instanceof ReflectionMethod && $class->name === '__invoke') {
34                $class = $class->getDeclaringClass();
35            }
36            $shortName = $class instanceof ReflectionClass ? $class->getShortName() : $class->name;
37            $shortName = preg_replace_callback('/^__(.)/', fn (array $m) => 'magicMethod' . strtoupper($m[1]), $shortName);
38            $short = preg_replace('/([a-z])([A-Z])/', '$1_$2', $shortName);
39        } else {
40            $short = $class;
41        }
42        return static::fromNative(strtolower($short));
43    }
44
45    public static function fromText(string $text): self
46    {
47        // Replace camelCase with snake_case
48        $text = preg_replace('/([a-z])([A-Z])/', '$1_$2', $text);
49        // Replace any non-alphanumeric character with underscore
50        $text = preg_replace('/[^a-zA-Z0-9]+/', '_', $text);
51        // Convert to lowercase
52        $text = strtolower($text);
53        // Remove leading/trailing underscores
54        $text = trim($text, '_');
55        // Collapse multiple underscores into one
56        $text = preg_replace('/_+/', '_', $text);
57        return static::fromNative($text);
58    }
59
60    public function humanize(): string
61    {
62        return str_replace('_', ' ', $this->internal);
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 toKebabCaseSlug(): KebabCaseSlug
76    {
77        return new KebabCaseSlug(str_replace('_', '-', $this->internal));
78    }
79
80    public static function createRandom(Generator $faker): static
81    {
82        return static::fromNative(CamelCaseSlug::createRandom($faker)->toSnakeCaseSlug()->toNative());
83    }
84}