2024-10-30 21:13:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportModel;
|
2024-10-30 21:13:41 +08:00
|
|
|
use Illuminate\Validation\Factory;
|
|
|
|
|
|
|
|
class ZipValidationHelper
|
|
|
|
{
|
|
|
|
protected Factory $validationFactory;
|
|
|
|
|
2024-11-18 23:53:21 +08:00
|
|
|
/**
|
|
|
|
* Local store of validated IDs (in format "<type>:<id>". Example: "book:2")
|
|
|
|
* which we can use to check uniqueness.
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
|
|
|
protected array $validatedIds = [];
|
|
|
|
|
2024-10-30 21:13:41 +08:00
|
|
|
public function __construct(
|
2024-11-03 01:17:34 +08:00
|
|
|
public ZipExportReader $zipReader,
|
2024-10-30 21:13:41 +08:00
|
|
|
) {
|
|
|
|
$this->validationFactory = app(Factory::class);
|
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
public function validateData(array $data, array $rules): array
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
2024-10-30 23:26:23 +08:00
|
|
|
$messages = $this->validationFactory->make($data, $rules)->errors()->messages();
|
|
|
|
|
|
|
|
foreach ($messages as $key => $message) {
|
|
|
|
$messages[$key] = implode("\n", $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-11-19 01:42:49 +08:00
|
|
|
public function fileReferenceRule(array $acceptedMimes = []): ZipFileReferenceRule
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
2024-11-19 01:42:49 +08:00
|
|
|
return new ZipFileReferenceRule($this, $acceptedMimes);
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
2024-10-30 23:26:23 +08:00
|
|
|
|
2024-11-18 23:53:21 +08:00
|
|
|
public function uniqueIdRule(string $type): ZipUniqueIdRule
|
|
|
|
{
|
|
|
|
return new ZipUniqueIdRule($this, $type);
|
|
|
|
}
|
|
|
|
|
2024-11-19 01:42:49 +08:00
|
|
|
public function hasIdBeenUsed(string $type, mixed $id): bool
|
2024-11-18 23:53:21 +08:00
|
|
|
{
|
|
|
|
$key = $type . ':' . $id;
|
|
|
|
if (isset($this->validatedIds[$key])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validatedIds[$key] = true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|