Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
AggregateConfig
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/config.
7 *
8 * This source file is subject to the license bundled
9 * with this source code in the file LICENSE.
10 *
11 * @copyright Copyright (c) 2025-2026 Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
12 * @license   https://opensource.org/licenses/MIT MIT License
13 *
14 * @see       https://github.com/php-fast-forward/config
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Config;
20
21use Dflydev\DotAccessData\Data;
22use Dflydev\DotAccessData\DataInterface;
23use FastForward\Config\Helper\ConfigHelper;
24
25/**
26 * Class AggregateConfig.
27 *
28 * Represents an aggregated configuration loader that combines multiple configuration sources.
29 * This class MUST be used to merge multiple configurations into a single, unified configuration structure.
30 *
31 * It SHALL lazily load and resolve configuration data only when invoked.
32 */
33 class AggregateConfig implements ConfigInterface
34{
35    use LazyLoadConfigTrait;
36
37    /**
38     * @var ConfigInterface[] A list of configuration providers to be aggregated.
39     *                        Each configuration MUST implement ConfigInterface.
40     */
41    private  array $configs;
42
43    /**
44     * Constructs a new instance by accepting a variadic list of configuration objects.
45     *
46     * These configuration objects MUST implement the ConfigInterface.
47     *
48     * @param ConfigInterface ...$configs One or more configuration instances to aggregate.
49     */
50    public function __construct(ConfigInterface ...$configs)
51    {
52        $this->configs = $configs;
53    }
54
55    /**
56     * Invokes the configuration aggregator.
57     *
58     * This method SHALL merge normalized configuration sources and return an ArrayConfig. Sequential lists SHALL be
59     * concatenated in source order, retaining duplicates. Maps SHALL merge recursively,
60     * and later scalar values SHALL replace earlier values.
61     * It MUST return a fully merged configuration in the form of a ConfigInterface implementation.
62     *
63     * @return ConfigInterface the resulting merged configuration object
64     */
65    public function __invoke(): ConfigInterface
66    {
67        $data = new Data();
68
69        foreach ($this->configs as $config) {
70            $data->import(ConfigHelper::normalize($config->toArray()), DataInterface::MERGE);
71        }
72
73        return new ArrayConfig($data->export());
74    }
75}