Passed
Pull Request — master (#241)
by Dmitriy
02:09
created

DynamicReferencesArray   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
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 62
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
final class DynamicReferencesArray
13
{
14
    /**
15
     *  A usage example
16
     *
17
     * ```php
18
     * //params.php
19
     * return [
20
     *      'yiisoft/data-response' => [
21
     *          'contentFormatters' => [
22
     *              'text/html' => HtmlDataResponseFormatter::class,
23
     *              'application/xml' => XmlDataResponseFormatter::class,
24
     *              'application/json' => JsonDataResponseFormatter::class,
25
     *          ],
26
     *      ],
27
     * ];
28
     * ```
29
     *
30
     * This definition
31
     *
32
     * ```php
33
     * //web.php
34
     *
35
     * ContentNegotiator::class => [
36
     *     '__construct()' => [
37
     *         'contentFormatters' => DynamicReferencesArray::from($params['yiisoft/data-response']['contentFormatters']),
38
     *     ],
39
     * ],
40
     * ```
41
     *
42
     * equals to
43
     *
44
     * ```php
45
     * //web.php
46
     *
47
     * ContentNegotiator::class => [
48
     *     '__construct()' => [
49
     *         'contentFormatters' => [
50
     *             'text/html' => DynamicReference::to(HtmlDataResponseFormatter())),
51
     *             'application/xml' => DynamicReference::to(XmlDataResponseFormatter()),
52
     *             'application/json' => DynamicReference::to(JsonDataResponseFormatter()),
53
     *         ],
54
     *     ],
55
     * ],
56
     * ```
57
     *
58
     * @param string[] $ids
59
     * @return DynamicReference[]
60
     * @throws InvalidConfigException
61
     */
62 2
    public static function from(array $ids)
63
    {
64 2
        $references = [];
65
66 2
        foreach ($ids as $key => $id) {
67 2
            if (!is_string($id)) {
68 1
                throw new InvalidConfigException('Values of an array must be string alias or class name.');
69
            }
70 2
            $references[$key] = DynamicReference::to($id);
71
        }
72
73 1
        return $references;
74
    }
75
}
76