Skip to content

Commit

Permalink
[import] Add MMP importer
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Dec 19, 2024
1 parent 9851bdd commit f1ae49c
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 0 deletions.
148 changes: 148 additions & 0 deletions app/Importers/MmpImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace App\Importers;

use Illuminate\Support\Facades\Date;

class MmpImporter extends AbstractImporter
{
protected $mapping = [
'identifier' => '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;
}
}
96 changes: 96 additions & 0 deletions tests/Importers/MmpImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Tests\Importers;

use App\Import;
use App\Importers\MmpImporter;
use App\ImportRecord;
use App\Item;
use App\Matchers\AuthorityMatcher;
use App\Repositories\CsvRepository;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
use Tests\WithoutSearchIndexing;

class MmpImporterTest extends TestCase
{
use RefreshDatabase, WithoutSearchIndexing;

protected CsvRepository|MockObject $repositoryMock;

protected MmpImporter $importer;

public function setUp(): void
{
parent::setUp();
$this->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" => "",
];
}
}

0 comments on commit f1ae49c

Please sign in to comment.