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\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 underscores and lowercase only(pascal_case). |
15 | */ |
16 | #[FakeMethod('createRandom')] |
17 | #[Description('Lowercase text written with underscores for separate words, for example "example_object"')] |
18 | class 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<object>|ReflectionMethod|ReflectionProperty|string $class |
29 | */ |
30 | public static function fromClass(ReflectionClass|ReflectionMethod|ReflectionProperty|string $class): self |
31 | { |
32 | if (is_object($class)) { |
33 | $shortName = $class instanceof ReflectionClass ? $class->getShortName() : $class->name; |
34 | $short = preg_replace('/([a-z])([A-Z])/', '$1_$2', $shortName); |
35 | } else { |
36 | $short = $class; |
37 | } |
38 | return static::fromNative(strtolower($short)); |
39 | } |
40 | |
41 | public function humanize(): string |
42 | { |
43 | return str_replace('_', ' ', $this->internal); |
44 | } |
45 | |
46 | public function toCamelCaseSlug(): CamelCaseSlug |
47 | { |
48 | return new CamelCaseSlug(lcfirst(str_replace('_', '', ucwords($this->internal, '_')))); |
49 | } |
50 | |
51 | public function toPascalCaseSlug(): PascalCaseSlug |
52 | { |
53 | return new PascalCaseSlug(str_replace('_', '', ucwords($this->internal, '_'))); |
54 | } |
55 | |
56 | public function toKebabCaseSlug(): KebabCaseSlug |
57 | { |
58 | return new KebabCaseSlug(str_replace('_', '-', $this->internal)); |
59 | } |
60 | |
61 | public static function createRandom(Generator $faker): static |
62 | { |
63 | return static::fromNative(CamelCaseSlug::createRandom($faker)->toSnakeCaseSlug()->toNative()); |
64 | } |
65 | } |