Final Class Yiisoft\Definitions\ReferencesArray
| Inheritance | Yiisoft\Definitions\ReferencesArray |
|---|
Allows creating an array of references from key-reference pairs.
See also Yiisoft\Definitions\Reference.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| from() | Create references array from name-reference pairs. | Yiisoft\Definitions\ReferencesArray |
Method Details
Create references array from name-reference pairs.
For example if we want to define a set of named references, usually it is done as:
//web.php
ContentNegotiator::class => [
'__construct()' => [
'contentFormatters' => [
'text/html' => Reference::to(HtmlDataResponseFormatter::class),
'application/xml' => Reference::to(XmlDataResponseFormatter::class),
'application/json' => Reference::to(JsonDataResponseFormatter::class),
],
],
],
That is not very convenient, so we can define formatters in a separate config and without explicitly using
Reference::to() for each formatter:
//params.php
return [
'yiisoft/data-response' => [
'contentFormatters' => [
'text/html' => HtmlDataResponseFormatter::class,
'application/xml' => XmlDataResponseFormatter::class,
'application/json' => JsonDataResponseFormatter::class,
],
],
];
Then we can use it like the following:
//web.php
ContentNegotiator::class => [
'__construct()' => [
'contentFormatters' => ReferencesArray::from($params['yiisoft/data-response']['contentFormatters']),
],
],
| public static Yiisoft\Definitions\Reference[] from ( string[] $ids ) | ||
| $ids | string[] |
Name-reference pairs. |
| throws | Yiisoft\Definitions\Exception\InvalidConfigException | |
|---|---|---|
public static function from(array $ids): array
{
$references = [];
foreach ($ids as $key => $id) {
if (!is_string($id)) {
throw new InvalidConfigException('Values of an array must be string alias or class name.');
}
$references[$key] = Reference::to($id);
}
return $references;
}
Signup or Login in order to comment.