Passed
Push — master ( 2172fa...eaad1b )
by Alexander
07:07
created

DefinitionResolver::resolveArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definitions;
6
7
use Psr\Container\ContainerInterface;
8
9
class DefinitionResolver
10
{
11
    /**
12
     * Resolves dependencies by replacing them with the actual object instances.
13
     * @param ContainerInterface $container
14
     * @param DefinitionInterface[] $dependencies the dependencies
15
     * @return array the resolved dependencies
16
     */
17 37
    public static function resolveArray(ContainerInterface $container, array $dependencies): array
18
    {
19 37
        $result = [];
20 37
        foreach ($dependencies as $key => $definition) {
21 14
            $result[$key] = self::resolve($container, $definition);
22
        }
23
24 35
        return $result;
25
    }
26
27
    /**
28
     * This function resolves a definition recursively, checking for loops.
29
     * @param ContainerInterface $container
30
     * @param mixed $definition
31
     * @return mixed
32
     */
33 14
    public static function resolve(ContainerInterface $container, $definition)
34
    {
35 14
        if ($definition instanceof DefinitionInterface) {
36 14
            $definition = $definition->resolve($container);
37 2
        } elseif (is_array($definition)) {
38 2
            return self::resolveArray($container, $definition);
39
        }
40
41 12
        return $definition;
42
    }
43
44 8
    public static function ensureResolvable($value)
45
    {
46 8
        if ($value instanceof DefinitionInterface || is_array($value)) {
47 2
            return $value;
48
        }
49 6
        if (is_callable($value)) {
50
            return new CallableDefinition($value);
51
        }
52
53 6
        return new ValueDefinition($value);
54
    }
55
}
56