2024-10-30 21:13:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Exports\ZipExports;
|
|
|
|
|
2024-11-03 01:17:34 +08:00
|
|
|
use BookStack\Exceptions\ZipExportException;
|
2024-10-30 23:26:23 +08:00
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportBook;
|
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportChapter;
|
|
|
|
use BookStack\Exports\ZipExports\Models\ZipExportPage;
|
2024-10-30 21:13:41 +08:00
|
|
|
|
|
|
|
class ZipExportValidator
|
|
|
|
{
|
|
|
|
public function __construct(
|
2024-11-05 23:41:58 +08:00
|
|
|
protected ZipExportReader $reader,
|
2024-10-30 21:13:41 +08:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
public function validate(): array
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
2024-11-03 01:17:34 +08:00
|
|
|
try {
|
2024-11-05 23:41:58 +08:00
|
|
|
$importData = $this->reader->readData();
|
2024-11-03 01:17:34 +08:00
|
|
|
} catch (ZipExportException $exception) {
|
|
|
|
return ['format' => $exception->getMessage()];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-11-05 23:41:58 +08:00
|
|
|
$helper = new ZipValidationHelper($this->reader);
|
2024-10-30 23:26:23 +08:00
|
|
|
|
2024-10-30 21:13:41 +08:00
|
|
|
if (isset($importData['book'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportBook::validate($helper, $importData['book']);
|
|
|
|
$keyPrefix = 'book';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else if (isset($importData['chapter'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportChapter::validate($helper, $importData['chapter']);
|
|
|
|
$keyPrefix = 'chapter';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else if (isset($importData['page'])) {
|
2024-10-30 23:26:23 +08:00
|
|
|
$modelErrors = ZipExportPage::validate($helper, $importData['page']);
|
|
|
|
$keyPrefix = 'page';
|
2024-10-30 21:13:41 +08:00
|
|
|
} else {
|
2024-11-02 22:51:04 +08:00
|
|
|
return ['format' => trans('errors.import_zip_no_data')];
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
2024-10-30 23:26:23 +08:00
|
|
|
|
|
|
|
return $this->flattenModelErrors($modelErrors, $keyPrefix);
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 23:26:23 +08:00
|
|
|
protected function flattenModelErrors(array $errors, string $keyPrefix): array
|
2024-10-30 21:13:41 +08:00
|
|
|
{
|
2024-10-30 23:26:23 +08:00
|
|
|
$flattened = [];
|
|
|
|
|
|
|
|
foreach ($errors as $key => $error) {
|
|
|
|
if (is_array($error)) {
|
|
|
|
$flattened = array_merge($flattened, $this->flattenModelErrors($error, $keyPrefix . '.' . $key));
|
|
|
|
} else {
|
|
|
|
$flattened[$keyPrefix . '.' . $key] = $error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $flattened;
|
2024-10-30 21:13:41 +08:00
|
|
|
}
|
|
|
|
}
|