Passed
Pull Request — master (#241)
by Dmitriy
02:49 queued 24s
created

ReferencesArray   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 3
eloc 6
c 4
b 1
f 0
dl 0
loc 64
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\Reference;
8
use Yiisoft\Factory\Exception\InvalidConfigException;
9
10
use function is_string;
11
12
final class ReferencesArray
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' => ReferencesArray::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' => Reference::to(HtmlDataResponseFormatter())),
51
     *             'application/xml' => Reference::to(XmlDataResponseFormatter()),
52
     *             'application/json' => Reference::to(JsonDataResponseFormatter()),
53
     *         ],
54
     *     ],
55
     * ],
56
     * ```
57
     *
58
     * @param string[] $ids
59
     *
60
     * @throws InvalidConfigException
61
     *
62
     * @return Reference[]
63
     */
64 2
    public static function from(array $ids)
65
    {
66 2
        $references = [];
67
68 2
        foreach ($ids as $key => $id) {
69 2
            if (!is_string($id)) {
70 1
                throw new InvalidConfigException('Values of an array must be string alias or class name.');
71
            }
72 2
            $references[$key] = Reference::to($id);
73
        }
74
75 1
        return $references;
76
    }
77
}
78