Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
FullName | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIndexes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createRandom | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | namespace Apie\CommonValueObjects; |
3 | |
4 | use Apie\Core\Attributes\FakeMethod; |
5 | use Apie\Core\Attributes\ProvideIndex; |
6 | use Apie\Core\ValueObjects\CompositeValueObject; |
7 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
8 | use Apie\CountWords\WordCounter; |
9 | use Apie\TextValueObjects\FirstName; |
10 | use Apie\TextValueObjects\LastName; |
11 | use Faker\Generator; |
12 | use Stringable; |
13 | |
14 | /** |
15 | * Represents a full name: gender + first name + last name. |
16 | */ |
17 | #[FakeMethod('createRandom')] |
18 | #[ProvideIndex('getIndexes')] |
19 | class FullName implements ValueObjectInterface, Stringable |
20 | { |
21 | use CompositeValueObject; |
22 | |
23 | public function __construct( |
24 | private Gender $gender, |
25 | private FirstName $firstName, |
26 | private LastName $lastName |
27 | ) { |
28 | } |
29 | |
30 | public function __toString(): string |
31 | { |
32 | return $this->gender->getSalutation() . ' ' . $this->firstName . ' ' . $this->lastName; |
33 | } |
34 | |
35 | /** |
36 | * @return array<string, int> |
37 | */ |
38 | public function getIndexes(): array |
39 | { |
40 | return WordCounter::countFromString($this->__toString()); |
41 | } |
42 | |
43 | public static function createRandom(Generator $generator): self |
44 | { |
45 | $gender = $generator->randomElement(['male', 'female']); |
46 | |
47 | return new self( |
48 | $gender === 'male' ? Gender::MALE : Gender::FEMALE, |
49 | new FirstName($generator->firstName($gender)), |
50 | new LastName($generator->lastName()) |
51 | ); |
52 | } |
53 | } |