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