From f1ae49cb868b8b488ced2a30def06a93b0e6a069 Mon Sep 17 00:00:00 2001 From: Rastislav Chynoransky Date: Thu, 19 Dec 2024 16:26:32 +0700 Subject: [PATCH] [import] Add MMP importer --- app/Importers/MmpImporter.php | 148 ++++++++++++++++++++++++++++ tests/Importers/MmpImporterTest.php | 96 ++++++++++++++++++ 2 files changed, 244 insertions(+) create mode 100644 app/Importers/MmpImporter.php create mode 100644 tests/Importers/MmpImporterTest.php diff --git a/app/Importers/MmpImporter.php b/app/Importers/MmpImporter.php new file mode 100644 index 000000000..1397525f5 --- /dev/null +++ b/app/Importers/MmpImporter.php @@ -0,0 +1,148 @@ + 'Inventární číslo', + 'title' => 'Titul', + 'dating' => 'Datace vzniku', + 'inscription' => 'Signatura', + ]; + + protected $defaults = [ + 'gallery:sk' => 'Múzeum Prahy, MMP', + 'gallery:cs' => 'Muzeum Prahy, MMP', + ]; + + private array $workTypeTranslationKeys; + private array $techniqueTranslationKeys; + private array $mediumTranslationKeys; + private array $topicTranslationKeys; + + protected function init(): void + { + $this->sanitizers[] = function ($value) { + return empty_to_null(trim($value)); + }; + + $this->workTypeTranslationKeys = array_flip(trans('item.work_types', locale: 'cs')); + $this->techniqueTranslationKeys = array_flip(trans('item.techniques', locale: 'cs')); + $this->mediumTranslationKeys = array_flip(trans('item.media', locale: 'cs')); + $this->topicTranslationKeys = array_flip(trans('item.topics', locale: 'cs')); + } + + protected function getItemId(array $record): string + { + return str($record['Inventární číslo']) + ->swap([ + ' ' => '_', + '/' => '-', + ]) + ->prepend('CZE:MP.'); + } + + protected function getItemImageFilenameFormat(array $record): string + { + return str($record['Inventární číslo']) + ->replace('/', '_') + ->append('(-.*)?'); + } + + protected function hydrateAuthor(array $record): string + { + if ($record['Autor'] === 'Anonym') { + return 'Neznámy autor'; + } + + return $record['Autor']; + } + + protected function hydrateWorkType(array $record, string $locale): ?string + { + $replacements = [ + 'malba' => 'malířství', + ]; + + return str($record['Výtvarný druh']) + ->split('/\s*;\s*/') + ->map(fn (string $workType) => $replacements[$workType] ?? $workType) + ->when($locale !== 'cs', fn ($workTypes) => $workTypes->map(function (string $workType) use ($locale) { + $key = $this->workTypeTranslationKeys[$workType] ?? null; + return $key ? trans("item.work_types.$key", locale: $locale) : null; + })) + ->filter() + ->join('; ') ?: null; + } + + protected function hydrateTechnique(array $record, string $locale): ?string + { + if ($locale === 'cs') { + return $record['Technika']; + } + + return str($record['Technika']) + ->split('/\s*;\s*/') + ->map(function (string $technique) use ($locale) { + $key = $this->techniqueTranslationKeys[$technique] ?? null; + return $key ? trans("item.techniques.$key", locale: $locale) : null; + }) + ->filter() + ->join('; ') ?: null; + } + + protected function hydrateMedium(array $record, string $locale): ?string + { + if ($locale === 'cs') { + return $record['Materiál']; + } + + return str($record['Materiál']) + ->split('/\s*;\s*/') + ->map(function (string $medium) use ($locale) { + $key = $this->mediumTranslationKeys[$medium] ?? null; + return $key ? trans("item.media.$key", locale: $locale) : null; + }) + ->filter() + ->join('; ') ?: null; + } + + protected function hydrateTopic(array $record, string $locale): ?string + { + if ($locale === 'cs') { + return $record['Námět/téma']; + } + + return str($record['Námět/téma']) + ->split('/\s*;\s*/') + ->map(function (string $topic) use ($locale) { + $key = $this->topicTranslationKeys[$topic] ?? null; + return $key ? trans("item.topics.$key", locale: $locale) : null; + }) + ->filter() + ->join('; ') ?: null; + } + + protected function hydrateMeasurement(array $record, $locale): ?string + { + if (empty($record['Rozměr'])) { + return null; + } + + $replacements = trans('item.measurement_replacements', [], $locale); + return strtr($record['Rozměr'], $replacements); + } + + protected function hydrateDateEarliest(array $record): int + { + return Date::createFromFormat('d.m.Y', $record['(n) Datace OD'])->year; + } + + protected function hydrateDateLatest(array $record): int + { + return Date::createFromFormat('d.m.Y', $record['(n) Datace DO'])->year; + } +} \ No newline at end of file diff --git a/tests/Importers/MmpImporterTest.php b/tests/Importers/MmpImporterTest.php new file mode 100644 index 000000000..0279c2709 --- /dev/null +++ b/tests/Importers/MmpImporterTest.php @@ -0,0 +1,96 @@ +repositoryMock = $this->createMock(CsvRepository::class); + $this->importer = new MmpImporter( + app(AuthorityMatcher::class), + $this->repositoryMock, + app(Translator::class) + ); + } + + public function testImport() + { + $this->importData(); + + $item = Item::find('CZE:MP.H_014_487'); + + $this->assertEquals('H 014 487', $item->identifier); + $this->assertEquals('Neznámy autor', $item->author); + $this->assertEquals(1701, $item->date_earliest); + $this->assertEquals(1800, $item->date_latest); + $this->assertEquals('Panna Marie Karlovská', $item->title); + $this->assertEquals('počátek 18.stol.', $item->dating); + $this->assertEquals('maliarstvo', $item->translate('sk')->work_type); + $this->assertEquals('malířství', $item->translate('cs')->work_type); + $this->assertEquals('painting', $item->translate('en')->work_type); + $this->assertEquals('olej', $item->translate('sk')->technique); + $this->assertEquals('olej', $item->translate('cs')->technique); + $this->assertEquals('oil', $item->translate('en')->technique); + $this->assertEquals('drevo', $item->translate('sk')->medium); + $this->assertEquals('dřevo', $item->translate('cs')->medium); + $this->assertEquals('wood', $item->translate('en')->medium); + $this->assertEquals(null, $item->translate('sk')->topic); + $this->assertEquals('obraz náboženský', $item->translate('cs')->topic); + $this->assertEquals(null, $item->translate('en')->topic); + $this->assertEquals('', $item->translate('sk')->measurement); + $this->assertEquals('', $item->translate('cs')->measurement); + $this->assertEquals('', $item->translate('en')->measurement); + } + + private function importData(array $data = []): ImportRecord + { + $data = $this->fakeData($data); + + $this->repositoryMock->method('getFiltered')->willReturn(new \ArrayIterator([$data])); + + $importRecord = ImportRecord::factory() + ->for(Import::factory()) + ->create(); + $this->importer->import($importRecord, stream: null); + return $importRecord; + } + + private function fakeData(array $overrides = []): array + { + return $overrides + [ + "Řada" => "H", + "Inventární číslo" => "H 014 487", + "Titul" => "Panna Marie Karlovská", + "Autor" => "Anonym", + "Datace vzniku" => "počátek 18.stol.", + "(n) Datace OD" => "1.1.1701", + "(n) Datace DO" => "31.12.1800", + "Výtvarný druh" => "malba", + "Materiál" => "dřevo", + "Technika" => "olej", + "Rozměr" => "rv.=31cm; rš.=21,5cm; v-cm=24,5cm; š-cm=14,5cm", + "Námět/téma" => "obraz náboženský", + "Signatura" => "", + ]; + } +} \ No newline at end of file