Passed
Pull Request — master (#2)
by Sergei
03:18 queued 01:11
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
eloc 6
c 1
b 0
f 0
dl 0
loc 69
ccs 7
cts 7
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A from() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions;
6
7
use Yiisoft\Definitions\Exception\InvalidConfigException;
8
9
use function is_string;
10
11
/**
12
 * Allows creating an array of dynamic references from key-reference pairs.
13
 *
14
 * @see DynamicReference
15
 */
16
final class DynamicReferencesArray
17
{
18
    /**
19
     * Create dynamic references array from name-reference pairs.
20
     *
21
     * For example if we want to define a set of named dynamic references, usually
22
     * it is done as:
23
     *
24
     * ```php
25
     * //web.php
26
     *
27
     * ContentNegotiator::class => [
28
     *     '__construct()' => [
29
     *         'contentFormatters' => [
30
     *             'text/html' => DynamicReference::to(HtmlDataResponseFormatter::class),
31
     *             'application/xml' => DynamicReference::to(XmlDataResponseFormatter::class),
32
     *             'application/json' => DynamicReference::to(JsonDataResponseFormatter::class),
33
     *         ],
34
     *     ],
35
     * ],
36
     * ```
37
     *
38
     * That is not very convenient so we can define formatters in a separate config and without explicitly using
39
     * `DynamicReference::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' => DynamicReferencesArray::from($params['yiisoft/data-response']['contentFormatters']),
62
     *     ],
63
     * ],
64
     * ```
65
     *
66
     * @param string[] $ids Name-reference pairs.
67
     *
68
     * @throws InvalidConfigException
69
     *
70
     * @return DynamicReference[]
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] = DynamicReference::to($id);
82
        }
83
84 1
        return $references;
85
    }
86
}
87