Passed
Push — master ( 3a280d...539d3e )
by Alexander
02:49 queued 01:18
created

Normalizer::validate()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9.2312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 27
ccs 9
cts 14
cp 0.6429
rs 8.8333
c 1
b 0
f 0
cc 7
nc 7
nop 2
crap 9.2312
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 39
    public static function normalize($definition, string $id = null, array $params = []): DefinitionInterface
53
    {
54 39
        if ($definition instanceof DefinitionInterface) {
55 2
            return $definition;
56
        }
57
58 39
        if (\is_string($definition)) {
59 34
            if ($id === $definition || (!empty($params) && class_exists($definition))) {
60 10
                return ArrayDefinition::fromArray($definition, $params);
61
            }
62 24
            return Reference::to($definition);
63
        }
64
65 15
        if (\is_callable($definition)) {
66 2
            return new CallableDefinition($definition);
67
        }
68
69 15
        if (\is_array($definition)) {
70 10
            return ArrayDefinition::fromArray($id, [], $definition);
71
        }
72
73 7
        if (\is_object($definition)) {
74 7
            return new ValueDefinition($definition);
75
        }
76
77
        throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
78
    }
79
80
    /**
81
     * Validates defintion for corectness.
82
     * @param mixed $definition @see normalize()
83
     * @param bool $id
84
     * @return bool
85
     * @throws InvalidConfigException
86
     */
87 22
    public static function validate($definition, bool $throw = true): bool
88
    {
89 22
        if ($definition instanceof DefinitionInterface) {
90
            return true;
91
        }
92
93 22
        if (\is_string($definition)) {
94 4
            return true;
95
        }
96
97 22
        if (\is_callable($definition)) {
98
            return true;
99
        }
100
101 22
        if (\is_array($definition)) {
102 1
            return true;
103
        }
104
105 22
        if (\is_object($definition)) {
106 22
            return true;
107
        }
108
109
        if ($throw) {
110
            throw new InvalidConfigException('Invalid definition:' . var_export($definition, true));
111
        }
112
113
        return false;
114
    }
115
}
116