Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
63.64% |
14 / 22 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
ApieLib | |
63.64% |
14 / 22 |
|
62.50% |
5 / 8 |
25.82 | |
0.00% |
0 / 1 |
resetAliases | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
registerAlias | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasAlias | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAlias | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
registerValueObject | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
getPsrClock | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setPsrClock | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
dumpValueException | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace Apie\Core; |
3 | |
4 | use Apie\Core\Exceptions\IndexNotFoundException; |
5 | use Apie\Core\FileStorage\StoredFile; |
6 | use Apie\Core\Permissions\PermissionInterface; |
7 | use Apie\Core\Permissions\SerializedPermission; |
8 | use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface; |
9 | use Apie\Core\ValueObjects\Utils; |
10 | use Apie\SchemaGenerator\Other\JsonSchemaFormatValidator; |
11 | use Beste\Clock\SystemClock; |
12 | use League\OpenAPIValidation\Schema\TypeFormats\FormatsContainer; |
13 | use Psr\Clock\ClockInterface; |
14 | use Psr\Http\Message\UploadedFileInterface; |
15 | use ReflectionClass; |
16 | use Symfony\Component\VarDumper\Cloner\VarCloner; |
17 | use Symfony\Component\VarDumper\Dumper\CliDumper; |
18 | |
19 | final class ApieLib |
20 | { |
21 | /** |
22 | * @var array<class-string<object>, string|class-string<object>> $aliases |
23 | */ |
24 | private static $aliases = [ |
25 | UploadedFileInterface::class => StoredFile::class, |
26 | PermissionInterface::class => SerializedPermission::class, |
27 | ]; |
28 | |
29 | public static function resetAliases(): void |
30 | { |
31 | $prop = new \ReflectionProperty(__CLASS__, 'aliases'); |
32 | $prop->setValue(null, $prop->getDefaultValue()); |
33 | } |
34 | |
35 | /** |
36 | * @param class-string<object> $alias |
37 | * @param string|class-string<object> $target |
38 | * @return void |
39 | */ |
40 | public static function registerAlias(string $alias, string $target): void |
41 | { |
42 | self::$aliases[$alias] = $target; |
43 | } |
44 | |
45 | /** |
46 | * @param class-string<object> $alias |
47 | */ |
48 | public static function hasAlias(string $alias): bool |
49 | { |
50 | return isset(self::$aliases[$alias]); |
51 | } |
52 | |
53 | /** |
54 | * @param class-string<object> $alias |
55 | * @return string|class-string<object> |
56 | */ |
57 | public static function getAlias(string $alias): string |
58 | { |
59 | if (!isset(self::$aliases[$alias])) { |
60 | throw new IndexNotFoundException($alias); |
61 | } |
62 | return self::$aliases[$alias]; |
63 | } |
64 | |
65 | /** |
66 | * @codeCoverageIgnore |
67 | */ |
68 | private function __construct() |
69 | { |
70 | } |
71 | |
72 | public const VERSION = '1.0.0-RC2'; |
73 | |
74 | public const APIE_FORM_ELEMENTS = '0.7.0'; |
75 | |
76 | public const APIE_STACKTRACE = '0.1.6'; |
77 | |
78 | private static ClockInterface $clock; |
79 | |
80 | |
81 | /** |
82 | * Workaround for many integration tests in validating string value objects in OneOf or AllOf schema's. |
83 | * |
84 | * @param class-string<ValueObjectInterface> $class |
85 | */ |
86 | public static function registerValueObject(string $class): void |
87 | { |
88 | if (class_exists(FormatsContainer::class) && class_exists(JsonSchemaFormatValidator::class)) { |
89 | $format = strtolower(Utils::getDisplayNameForValueObject(new ReflectionClass($class))); |
90 | if (!FormatsContainer::getFormat('string', $format)) { |
91 | FormatsContainer::registerFormat('string', $format, new JsonSchemaFormatValidator($class)); |
92 | } |
93 | if (!FormatsContainer::getFormat('number', $format)) { |
94 | FormatsContainer::registerFormat('number', $format, new JsonSchemaFormatValidator($class)); |
95 | } |
96 | } |
97 | } |
98 | |
99 | public static function getPsrClock(): ClockInterface |
100 | { |
101 | if (!isset(self::$clock)) { |
102 | self::$clock = SystemClock::create(); |
103 | } |
104 | return self::$clock; |
105 | } |
106 | |
107 | public static function setPsrClock(ClockInterface $clock): void |
108 | { |
109 | self::$clock = $clock; |
110 | } |
111 | |
112 | public static function dumpValueException(mixed $input): never |
113 | { |
114 | $cloner = new VarCloner(); |
115 | $dumper = new CliDumper(); |
116 | $output = fopen('php://memory', 'r+b'); |
117 | |
118 | $dumper->dump($cloner->cloneVar($input), $output); |
119 | throw new \LogicException(stream_get_contents($output, -1, 0)); |
120 | } |
121 | } |