humbug /
box
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the box project. |
||
| 7 | * |
||
| 8 | * (c) Kevin Herrera <[email protected]> |
||
| 9 | * Théo Fidry <[email protected]> |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace KevinGH\Box\Configuration; |
||
| 16 | |||
| 17 | use KevinGH\Box\Json\Json; |
||
| 18 | use stdClass; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @private |
||
| 22 | */ |
||
| 23 | final readonly class ConfigurationLoader |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 24 | { |
||
| 25 | private const SCHEMA_FILE = __DIR__.'/../../res/schema.json'; |
||
| 26 | |||
| 27 | public function __construct(private Json $json = new Json()) |
||
| 28 | { |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param null|non-empty-string $file |
||
| 33 | */ |
||
| 34 | public function loadFile(?string $file): Configuration |
||
| 35 | { |
||
| 36 | if (null === $file) { |
||
| 37 | return Configuration::create(null, new stdClass()); |
||
| 38 | } |
||
| 39 | |||
| 40 | $json = $this->json->decodeFile($file); |
||
| 41 | |||
| 42 | $this->json->validate( |
||
| 43 | $file, |
||
| 44 | $json, |
||
| 45 | $this->json->decodeFile(self::SCHEMA_FILE), |
||
| 46 | ); |
||
| 47 | |||
| 48 | return Configuration::create($file, $json); |
||
| 49 | } |
||
| 50 | } |
||
| 51 |