3 namespace BookStack\Exports\ZipExports;
5 use BookStack\Exceptions\ZipExportException;
6 use BookStack\Exports\ZipExports\Models\ZipExportBook;
7 use BookStack\Exports\ZipExports\Models\ZipExportChapter;
8 use BookStack\Exports\ZipExports\Models\ZipExportPage;
10 class ZipExportValidator
12 public function __construct(
13 protected ZipExportReader $reader,
17 public function validate(): array
20 $importData = $this->reader->readData();
21 } catch (ZipExportException $exception) {
22 return ['format' => $exception->getMessage()];
25 $helper = new ZipValidationHelper($this->reader);
27 if (isset($importData['book'])) {
28 $modelErrors = ZipExportBook::validate($helper, $importData['book']);
30 } else if (isset($importData['chapter'])) {
31 $modelErrors = ZipExportChapter::validate($helper, $importData['chapter']);
32 $keyPrefix = 'chapter';
33 } else if (isset($importData['page'])) {
34 $modelErrors = ZipExportPage::validate($helper, $importData['page']);
37 return ['format' => trans('errors.import_zip_no_data')];
40 return $this->flattenModelErrors($modelErrors, $keyPrefix);
43 protected function flattenModelErrors(array $errors, string $keyPrefix): array
47 foreach ($errors as $key => $error) {
48 if (is_array($error)) {
49 $flattened = array_merge($flattened, $this->flattenModelErrors($error, $keyPrefix . '.' . $key));
51 $flattened[$keyPrefix . '.' . $key] = $error;