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

ReferencesArray   A

Complexity

Total Complexity 3

Size/Duplication

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