Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| StarRating | |
100.00% |
11 / 11 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| provideSchema | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| toNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\CommonValueObjects; |
| 4 | |
| 5 | use Apie\Core\Attributes\Description; |
| 6 | use Apie\Core\Attributes\FakeMethod; |
| 7 | use Apie\Core\Attributes\SchemaMethod; |
| 8 | use Apie\Core\Exceptions\InvalidTypeException; |
| 9 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
| 10 | use Apie\Core\ValueObjects\Utils; |
| 11 | use Faker\Generator; |
| 12 | |
| 13 | #[FakeMethod('createRandom')] |
| 14 | #[SchemaMethod('provideSchema')] |
| 15 | #[Description('A rating on a scale of 0-5, 0 being bad, 5 being best')] |
| 16 | final class StarRating implements ValueObjectInterface |
| 17 | { |
| 18 | public function __construct( |
| 19 | private int $input |
| 20 | ) { |
| 21 | if ($input < 0 || $input > 5) { |
| 22 | throw new InvalidTypeException($input, '0-5'); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | public static function createRandom(Generator $faker): self |
| 27 | { |
| 28 | return new self($faker->numberBetween(0, 5)); |
| 29 | } |
| 30 | public static function fromNative(mixed $input): self |
| 31 | { |
| 32 | return new StarRating(Utils::toInt($input)); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return array<string, string|int> |
| 37 | */ |
| 38 | public static function provideSchema(): array |
| 39 | { |
| 40 | return [ |
| 41 | 'type' => 'number', |
| 42 | 'format' => 'integer', |
| 43 | 'minimum' => 0, |
| 44 | 'maximum' => 5, |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | public function toNative(): int |
| 49 | { |
| 50 | return $this->input; |
| 51 | } |
| 52 | } |