Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

HelperFactory::getDefaultAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare (strict_types = 1);
3
4
namespace VGirol\JsonApiAssert\Factory;
5
6
class HelperFactory
7
{
8
    const ERROR_INEXISTANT_KEY = 'Inexistant key "%s".';
9
10
    protected static function getAliases(): array
11
    {
12
        return [];
13
    }
14
15
    private static function getDefaultAliases(): array
16
    {
17
        return [
18
            'collection' => CollectionFactory::class,
19
            'jsonapi' => JsonapiFactory::class,
20
            'relationship' => RelationshipFactory::class,
21
            'resource-identifier' => ResourceIdentifierFactory::class,
22
            'resource-object' => ResourceObjectFactory::class
23
        ];
24
    }
25
26
    public static function getClassName(string $key): string
27
    {
28
        $aliases = array_merge(static::getDefaultAliases(), static::getAliases());
29
        if (!isset($aliases[$key])) {
30
            throw new \Exception(
31
                sprintf(static::ERROR_INEXISTANT_KEY, $key)
32
            );
33
        }
34
35
        return $aliases[$key];
36
    }
37
38
    public static function create($alias, ...$args)
39
    {
40
        $className = static::getClassName($alias);
41
42
        return new $className(...$args);
43
    }
44
}
45