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

ReferencesArray::from()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
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