BookStack/app/Exports/ZipExports/ZipValidationHelper.php
Dan Brown c4ec50d437
Some checks failed
analyse-php / build (push) Has been cancelled
lint-php / build (push) Has been cancelled
test-migrations / build (8.1) (push) Has been cancelled
test-migrations / build (8.2) (push) Has been cancelled
test-migrations / build (8.3) (push) Has been cancelled
test-php / build (8.1) (push) Has been cancelled
test-php / build (8.2) (push) Has been cancelled
test-php / build (8.3) (push) Has been cancelled
ZIP Exports: Got zip format validation functionally complete
2024-10-30 15:26:23 +00:00

60 lines
1.5 KiB
PHP

<?php
namespace BookStack\Exports\ZipExports;
use BookStack\Exports\ZipExports\Models\ZipExportModel;
use Illuminate\Validation\Factory;
use ZipArchive;
class ZipValidationHelper
{
protected Factory $validationFactory;
public function __construct(
protected ZipArchive $zip,
) {
$this->validationFactory = app(Factory::class);
}
public function validateData(array $data, array $rules): array
{
$messages = $this->validationFactory->make($data, $rules)->errors()->messages();
foreach ($messages as $key => $message) {
$messages[$key] = implode("\n", $message);
}
return $messages;
}
public function zipFileExists(string $name): bool
{
return $this->zip->statName("files/{$name}") !== false;
}
public function fileReferenceRule(): ZipFileReferenceRule
{
return new ZipFileReferenceRule($this);
}
/**
* Validate an array of relation data arrays that are expected
* to be for the given ZipExportModel.
* @param class-string<ZipExportModel> $model
*/
public function validateRelations(array $relations, string $model): array
{
$results = [];
foreach ($relations as $key => $relationData) {
if (is_array($relationData)) {
$results[$key] = $model::validate($this, $relationData);
} else {
$results[$key] = [trans('validation.zip_model_expected', ['type' => gettype($relationData)])];
}
}
return $results;
}
}