respect-validation/src-dev/Helpers/DataSaver.php
Henrique Moody 4390e4feb6
Simplify how we load and save files in data/
We had different ways of saving and loading files from `data/`, so I decided to
unify them to simplify things. I repurposed the `DomainInfo` class and named it
`DataLoader`, so we can use the same class to load anything from the `data/`
directory.
2026-01-26 20:28:29 +01:00

50 lines
1.4 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Dev\Helpers;
use RuntimeException;
use Symfony\Component\VarExporter\VarExporter;
use function array_is_list;
use function dirname;
use function file_put_contents;
use function implode;
use function ksort;
use function preg_replace;
use function str_replace;
use const DIRECTORY_SEPARATOR;
use const PHP_EOL;
final class DataSaver
{
/** @param array<string|int, mixed> $data */
public function save(array $data, string $fileCopyrightText, string $licenseIdentifier, string $path): void
{
if (!array_is_list($data)) {
ksort($data);
}
$fileContent = implode(PHP_EOL, [
// REUSE-IgnoreStart
'<?php declare(strict_types=1);',
'// SPDX-FileCopyrightText: ' . $fileCopyrightText,
'// SPDX-License-Identifier: ' . $licenseIdentifier,
// REUSE-IgnoreEnd
'return ' . preg_replace('/\\\([dws])/', '\\1', VarExporter::export($data)) . ';' . PHP_EOL,
]);
$filename = str_replace('/', DIRECTORY_SEPARATOR, dirname(__DIR__, 2) . '/data/' . $path);
if (file_put_contents($filename, $fileContent) === false) {
throw new RuntimeException('Failed to write data file: ' . $filename);
}
}
}