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