Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.00% |
9 / 10 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| PascalCaseSlug | |
90.00% |
9 / 10 |
|
83.33% |
5 / 6 |
7.05 | |
0.00% |
0 / 1 |
| getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| humanize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toCamelCaseSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toSnakeCaseSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toKebabCaseSlug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createRandom | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Identifiers; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Core\Utils\IdentifierConstants; |
| 7 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 8 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
| 9 | use Faker\Generator; |
| 10 | |
| 11 | /** |
| 12 | * Indicate camel case string starting with a capital (for example PascalCase) |
| 13 | */ |
| 14 | #[FakeMethod('createRandom')] |
| 15 | #[Description('String in PascalCase, for example "ExampleObject"')] |
| 16 | class PascalCaseSlug implements HasRegexValueObjectInterface |
| 17 | { |
| 18 | use IsStringWithRegexValueObject; |
| 19 | |
| 20 | public static function getRegularExpression(): string |
| 21 | { |
| 22 | return '/^[A-Z][a-zA-Z0-9]*$/'; |
| 23 | } |
| 24 | |
| 25 | public function humanize(): string |
| 26 | { |
| 27 | return strtolower(preg_replace('/(?<!^)[A-Z]/', ' $0', $this->internal)); |
| 28 | } |
| 29 | |
| 30 | public function toCamelCaseSlug(): CamelCaseSlug |
| 31 | { |
| 32 | return new CamelCaseSlug(lcfirst($this->internal)); |
| 33 | } |
| 34 | |
| 35 | public function toSnakeCaseSlug(): SnakeCaseSlug |
| 36 | { |
| 37 | return new SnakeCaseSlug(strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $this->internal))); |
| 38 | } |
| 39 | |
| 40 | public function toKebabCaseSlug(): KebabCaseSlug |
| 41 | { |
| 42 | return new KebabCaseSlug(strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $this->internal))); |
| 43 | } |
| 44 | |
| 45 | public static function createRandom(Generator $faker): static |
| 46 | { |
| 47 | if ($faker->boolean()) { |
| 48 | return static::fromNative(ucfirst($faker->randomElement(IdentifierConstants::RANDOM_DOMAIN_OBJECT_NAMES))); |
| 49 | } |
| 50 | $words = $faker->words($faker->numberBetween(1, 3)); |
| 51 | $words = array_map('ucfirst', $words); |
| 52 | return static::fromNative(implode('', $words)); |
| 53 | } |
| 54 | } |