Passed
Push — master ( 3633ed...8de50e )
by Alexander
21:09 queued 19:43
created

Normalizer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 105
ccs 23
cts 30
cp 0.7667
rs 10
wmc 18

2 Methods

Rating   Name   Duplication   Size   Complexity  
B normalize() 0 29 10
B validate() 0 27 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definitions;
6
7
use Yiisoft\Factory\Exceptions\InvalidConfigException;
8
9
/**
10
 * Class Definition represents a definition in a container
11
 */
12
class Normalizer
13
{
14
    /**
15
     * Definition may be defined multiple ways.
16
     * Interface name as string:
17
     *
18
     * ```php
19
     * $container->set('interface_name', EngineInterface::class);
20
     * ```
21
     *
22
     * A closure:
23
     *
24
     * ```php
25
     * $container->set('closure', function($container) {
26
     *     return new MyClass($container->get('db'));
27
     * });
28
     * ```
29
     *
30
     * A callable array:
31
     *
32
     * ```php
33
     * $container->set('static_call', [MyClass::class, 'create']);
34
     * ```
35
     *
36
     * A definition array:
37
     *
38
     * ```php
39
     * $container->set('full_definition', [
40
     *     '__class' => EngineMarkOne::class,
41
     *     '__construct()' => [42],
42
     *     'argName' => 'value',
43
     *     'setX()' => [42],
44
     * ]);
45
     * ```
46
     *
47
     * @param mixed $definition
48
     * @param string $id
49
     * @param array $params
50
     * @throws InvalidConfigException
51
     */
52 41
    public static function normalize($definition, string $id = null, array $params = []): DefinitionInterface
53
    {
54 41
        if ($definition instanceof DefinitionInterface) {
55 2
            return $definition;
56
        }
57
58 41
        if (\is_string($definition)) {
59 36
            if (empty($definition)) {
60
                throw new InvalidConfigException('Invalid definition: empty string.');
61
            }
62 36
            if ($id === $definition || (!empty($params) && class_exists($definition))) {
63 12
                return ArrayDefinition::fromArray($definition, $params);
64
            }
65 24
            return Reference::to($definition);
66
        }
67
68 28
        if (\is_callable($definition, true)) {
69 2
            return new CallableDefinition($definition);
70
        }
71
72 28
        if (\is_array($definition)) {
73 10
            return ArrayDefinition::fromArray($id, [], $definition);
74
        }
75
76 24
        if (\is_object($definition)) {
77 24
            return new ValueDefinition($definition);
78
        }
79
80
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
81
    }
82
83
    /**
84
     * Validates defintion for corectness.
85
     * @param mixed $definition @see normalize()
86
     * @param bool $id
87
     * @return bool
88
     * @throws InvalidConfigException
89
     */
90 7
    public static function validate($definition, bool $throw = true): bool
91
    {
92 7
        if ($definition instanceof DefinitionInterface) {
93
            return true;
94
        }
95
96 7
        if (\is_string($definition) && !empty($definition)) {
97 4
            return true;
98
        }
99
100 3
        if (\is_callable($definition)) {
101
            return true;
102
        }
103
104 3
        if (\is_array($definition)) {
105 1
            return true;
106
        }
107
108 2
        if (\is_object($definition)) {
109 2
            return true;
110
        }
111
112
        if ($throw) {
113
            throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
114
        }
115
116
        return false;
117
    }
118
}
119