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