Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
41.18% |
7 / 17 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
VendorValueObject | |
41.18% |
7 / 17 |
|
66.67% |
2 / 3 |
21.03 | |
0.00% |
0 / 1 |
validate | |
23.08% |
3 / 13 |
|
0.00% |
0 / 1 |
16.38 | |||
getOptions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Maker\ValueObjects; |
3 | |
4 | use Apie\ApieCommonPlugin\ObjectProviderFactory; |
5 | use Apie\Core\Attributes\Description; |
6 | use Apie\Core\Attributes\FakeMethod; |
7 | use Apie\Core\Lists\StringSet; |
8 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
9 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
10 | use Apie\Core\ValueObjects\Interfaces\LimitedOptionsInterface; |
11 | use Apie\Core\ValueObjects\IsStringValueObject; |
12 | use Apie\Maker\Concerns\IsClassNameReference; |
13 | use Faker\Generator; |
14 | use ReflectionClass; |
15 | |
16 | #[FakeMethod('createRandom')] |
17 | #[Description('A PHP class name loaded in from a vendor library (probably Composer).')] |
18 | final class VendorValueObject implements HasRegexValueObjectInterface, LimitedOptionsInterface |
19 | { |
20 | use IsClassNameReference; |
21 | use IsStringValueObject; |
22 | |
23 | private static StringSet $options; |
24 | |
25 | public static function validate(string $input): void |
26 | { |
27 | if (!preg_match(static::getRegularExpression(), $input)) { |
28 | throw new InvalidStringForValueObjectException( |
29 | $input, |
30 | new ReflectionClass(self::class) |
31 | ); |
32 | } |
33 | if ($input === __CLASS__) { |
34 | return; |
35 | } |
36 | $objects = self::getOptions(); |
37 | if (!empty($objects) && !isset($objects[$input])) { |
38 | throw new InvalidStringForValueObjectException( |
39 | $input, |
40 | new ReflectionClass(self::class) |
41 | ); |
42 | } |
43 | } |
44 | |
45 | public static function getOptions(): StringSet |
46 | { |
47 | if (!isset(self::$options)) { |
48 | self::$options = new StringSet([__CLASS__, ...ObjectProviderFactory::create()->getAvailableValueObjects()]); |
49 | } |
50 | return self::$options; |
51 | } |
52 | |
53 | public static function createRandom(Generator $factory): self |
54 | { |
55 | return new self($factory->randomElement(self::getOptions()->toArray())); |
56 | } |
57 | } |