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
21/**
22 * Class AggregateConfig.
23 *
24 * Represents an aggregated configuration loader that combines multiple configuration sources.
25 * This class MUST be used to merge multiple configurations into a single, unified configuration structure.
26 *
27 * It SHALL lazily load and resolve configuration data only when invoked.
28 */
29 class AggregateConfig implements ConfigInterface
30{
31    use LazyLoadConfigTrait;
32
33    /**
34     * @var ConfigInterface[] A list of configuration providers to be aggregated.
35     *                        Each configuration MUST implement ConfigInterface.
36     */
37    private  array $configs;
38
39    /**
40     * Constructs a new instance by accepting a variadic list of configuration objects.
41     *
42     * These configuration objects MUST implement the ConfigInterface.
43     *
44     * @param ConfigInterface ...$configs One or more configuration instances to aggregate.
45     */
46    public function __construct(ConfigInterface ...$configs)
47    {
48        $this->configs = $configs;
49    }
50
51    /**
52     * Invokes the configuration aggregator.
53     *
54     * This method SHALL initialize a new ArrayConfig instance and populate it
55     * with the values from each provided configuration source.
56     * It MUST return a fully merged configuration in the form of a ConfigInterface implementation.
57     *
58     * @return ConfigInterface the resulting merged configuration object
59     */
60    public function __invoke(): ConfigInterface
61    {
62        $arrayConfig = new ArrayConfig();
63
64        foreach ($this->configs as $config) {
65            $arrayConfig->set($config->toArray());
66        }
67
68        return $arrayConfig;
69    }
70}