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