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