Passed
Push — master ( 05ac37...a315d3 )
by Alexander
02:48
created

DynamicReferencesArray   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 69
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A from() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Yiisoft\Factory\Definition\DynamicReference;
8
use Yiisoft\Factory\Exception\InvalidConfigException;
9
10
use function is_string;
11
12
/**
13
 * Allows creating an array of dynamic references from key-reference pairs.
14
 *
15
 * @see DynamicReference
16
 */
17
final class DynamicReferencesArray
18
{
19
    /**
20
     * Create dynamic references array from name-reference pairs.
21
     *
22
     * For example if we want to define a set of named dynamic references, usually
23
     * it is done as:
24
     *
25
     * ```php
26
     * //web.php
27
     *
28
     * ContentNegotiator::class => [
29
     *     '__construct()' => [
30
     *         'contentFormatters' => [
31
     *             'text/html' => DynamicReference::to(HtmlDataResponseFormatter())),
32
     *             'application/xml' => DynamicReference::to(XmlDataResponseFormatter()),
33
     *             'application/json' => DynamicReference::to(JsonDataResponseFormatter()),
34
     *         ],
35
     *     ],
36
     * ],
37
     * ```
38
     *
39
     * That is not very convenient so we can define formatters in a separate config and without explicitly using
40
     * `DynamicReference::to()` for each formatter:
41
     *
42
     * ```php
43
     * //params.php
44
     * return [
45
     *      'yiisoft/data-response' => [
46
     *          'contentFormatters' => [
47
     *              'text/html' => HtmlDataResponseFormatter::class,
48
     *              'application/xml' => XmlDataResponseFormatter::class,
49
     *              'application/json' => JsonDataResponseFormatter::class,
50
     *          ],
51
     *      ],
52
     * ];
53
     * ```
54
     *
55
     * Then we can use it like the following:
56
     *
57
     * ```php
58
     * //web.php
59
     *
60
     * ContentNegotiator::class => [
61
     *     '__construct()' => [
62
     *         'contentFormatters' => DynamicReferencesArray::from($params['yiisoft/data-response']['contentFormatters']),
63
     *     ],
64
     * ],
65
     * ```
66
     *
67
     * @param string[] $ids Name-reference pairs.
68
     *
69
     * @throws InvalidConfigException
70
     *
71
     * @return DynamicReference[]
72
     * @psalm-suppress DocblockTypeContradiction
73
     */
74 2
    public static function from(array $ids)
75
    {
76 2
        $references = [];
77
78 2
        foreach ($ids as $key => $id) {
79 2
            if (!is_string($id)) {
80 1
                throw new InvalidConfigException('Values of an array must be string alias or class name.');
81
            }
82 2
            $references[$key] = DynamicReference::to($id);
83
        }
84
85 1
        return $references;
86
    }
87
}
88