Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.82% |
9 / 11 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
SnakeCaseSlug | |
81.82% |
9 / 11 |
|
71.43% |
5 / 7 |
9.49 | |
0.00% |
0 / 1 |
getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fromClass | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
humanize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
toCamelCaseSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toPascalCaseSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toKebabCaseSlug | |
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\FakeMethod; |
5 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
6 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
7 | use Faker\Generator; |
8 | use ReflectionClass; |
9 | use ReflectionMethod; |
10 | use ReflectionProperty; |
11 | |
12 | /** |
13 | * Indicate an identifier written with underscores and lowercase only(pascal_case). |
14 | */ |
15 | #[FakeMethod('createRandom')] |
16 | class 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 | } |