Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
25.00% |
3 / 12 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ClassNameReference | |
25.00% |
3 / 12 |
|
33.33% |
1 / 3 |
21.19 | |
0.00% |
0 / 1 |
| validate | |
20.00% |
2 / 10 |
|
0.00% |
0 / 1 |
12.19 | |||
| createRandom | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Maker\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Attributes\Description; |
| 5 | use Apie\Core\Attributes\FakeMethod; |
| 6 | use Apie\Core\FileStorage\StoredFile; |
| 7 | use Apie\Core\Lists\StringList; |
| 8 | use Apie\Core\Lists\StringSet; |
| 9 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 10 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 11 | use Apie\Core\ValueObjects\Interfaces\LimitedOptionsInterface; |
| 12 | use Apie\Core\ValueObjects\IsStringValueObject; |
| 13 | use Apie\Maker\Concerns\IsClassNameReference; |
| 14 | use DateTime; |
| 15 | use DateTimeImmutable; |
| 16 | use DateTimeInterface; |
| 17 | use DateTimeZone; |
| 18 | use Faker\Generator; |
| 19 | use Psr\Http\Message\UploadedFileInterface; |
| 20 | use ReflectionClass; |
| 21 | |
| 22 | #[FakeMethod('createRandom')] |
| 23 | #[Description('Any existing PHP class name with fully qualified namespace')] |
| 24 | class ClassNameReference implements HasRegexValueObjectInterface, LimitedOptionsInterface |
| 25 | { |
| 26 | use IsClassNameReference; |
| 27 | use IsStringValueObject; |
| 28 | |
| 29 | private const CLASSNAMES = [ |
| 30 | StringList::class, |
| 31 | __CLASS__, |
| 32 | VendorValueObject::class, |
| 33 | UploadedFileInterface::class, |
| 34 | StoredFile::class, |
| 35 | DateTimeInterface::class, |
| 36 | DateTimeImmutable::class, |
| 37 | DateTime::class, |
| 38 | DateTimeZone::class, |
| 39 | ]; |
| 40 | |
| 41 | public static function validate(string $input): void |
| 42 | { |
| 43 | if (!preg_match(static::getRegularExpression(), $input)) { |
| 44 | throw new InvalidStringForValueObjectException( |
| 45 | $input, |
| 46 | new ReflectionClass(self::class) |
| 47 | ); |
| 48 | } |
| 49 | if (!class_exists($input) && !interface_exists($input)) { |
| 50 | throw new InvalidStringForValueObjectException( |
| 51 | $input, |
| 52 | new ReflectionClass(self::class) |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public static function createRandom(Generator $factory): self |
| 58 | { |
| 59 | return new self($factory->randomElement(self::CLASSNAMES)); |
| 60 | } |
| 61 | |
| 62 | public static function getOptions(): StringSet |
| 63 | { |
| 64 | return new StringSet(self::CLASSNAMES); |
| 65 | } |
| 66 | } |