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