Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
3 / 12
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClassNameReference
25.00% covered (danger)
25.00%
3 / 12
33.33% covered (danger)
33.33%
1 / 3
21.19
0.00% covered (danger)
0.00%
0 / 1
 validate
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
12.19
 createRandom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\Maker\ValueObjects;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Attributes\FakeMethod;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\Core\Lists\StringList;
8use Apie\Core\Lists\StringSet;
9use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
10use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface;
11use Apie\Core\ValueObjects\Interfaces\LimitedOptionsInterface;
12use Apie\Core\ValueObjects\IsStringValueObject;
13use Apie\Maker\Concerns\IsClassNameReference;
14use DateTime;
15use DateTimeImmutable;
16use DateTimeInterface;
17use DateTimeZone;
18use Faker\Generator;
19use Psr\Http\Message\UploadedFileInterface;
20use ReflectionClass;
21
22#[FakeMethod('createRandom')]
23#[Description('Any existing PHP class name with fully qualified namespace')]
24class 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}