Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SnakeCaseSlug
81.82% covered (warning)
81.82%
9 / 11
71.43% covered (warning)
71.43%
5 / 7
9.49
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
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 humanize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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\FakeMethod;
5use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
6use Apie\Core\ValueObjects\IsStringWithRegexValueObject;
7use Faker\Generator;
8use ReflectionClass;
9use ReflectionMethod;
10use ReflectionProperty;
11
12/**
13 * Indicate an identifier written with underscores and lowercase only(pascal_case).
14 */
15#[FakeMethod('createRandom')]
16class SnakeCaseSlug implements HasRegexValueObjectInterface
17{
18    use IsStringWithRegexValueObject;
19
20    public static function getRegularExpression(): string
21    {
22        return '/^[a-z0-9]+(_[a-z0-9]+)*$/';
23    }
24
25    /**
26     * @param ReflectionClass<object>|ReflectionMethod|ReflectionProperty|string $class
27     */
28    public static function fromClass(ReflectionClass|ReflectionMethod|ReflectionProperty|string $class): self
29    {
30        if (is_object($class)) {
31            $shortName = $class instanceof ReflectionClass ? $class->getShortName() : $class->name;
32            $short = preg_replace('/([a-z])([A-Z])/', '$1_$2', $shortName);
33        } else {
34            $short = $class;
35        }
36        return static::fromNative(strtolower($short));
37    }
38
39    public function humanize(): string
40    {
41        return str_replace('_', ' ', $this->internal);
42    }
43
44    public function toCamelCaseSlug(): CamelCaseSlug
45    {
46        return new CamelCaseSlug(lcfirst(str_replace('_', '', ucwords($this->internal, '_'))));
47    }
48
49    public function toPascalCaseSlug(): PascalCaseSlug
50    {
51        return new PascalCaseSlug(str_replace('_', '', ucwords($this->internal, '_')));
52    }
53
54    public function toKebabCaseSlug(): KebabCaseSlug
55    {
56        return new KebabCaseSlug(str_replace('_', '-', $this->internal));
57    }
58
59    public static function createRandom(Generator $faker): static
60    {
61        return static::fromNative(CamelCaseSlug::createRandom($faker)->toSnakeCaseSlug()->toNative());
62    }
63}