Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| CamelCaseSlug | |
100.00% |
11 / 11 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| getRegularExpression | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| humanize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toKebabCaseSlug | |
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% |
6 / 6 |
|
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 for id's starting with a lower case |
| 13 | */ |
| 14 | #[FakeMethod('createRandom')] |
| 15 | #[Description('Any string in camel case with first letter being lower case, for example "exampleObject"')] |
| 16 | class CamelCaseSlug 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 toKebabCaseSlug(): KebabCaseSlug |
| 31 | { |
| 32 | return new KebabCaseSlug(strtolower(preg_replace('/(?<!^)[A-Z]/', '-$0', $this->internal))); |
| 33 | } |
| 34 | |
| 35 | public function toPascalCaseSlug(): PascalCaseSlug |
| 36 | { |
| 37 | return new PascalCaseSlug(ucfirst($this->internal)); |
| 38 | } |
| 39 | |
| 40 | public function toSnakeCaseSlug(): SnakeCaseSlug |
| 41 | { |
| 42 | return new SnakeCaseSlug(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($faker->randomElement(IdentifierConstants::RANDOM_IDENTIFIERS)); |
| 49 | } |
| 50 | $words = $faker->words($faker->numberBetween(2, 3)); |
| 51 | $firstWord = array_shift($words); |
| 52 | $words = array_map('ucfirst', $words); |
| 53 | return static::fromNative($firstWord . implode('', $words)); |
| 54 | } |
| 55 | } |