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