Add address to tourist attraction

This commit is contained in:
Daniel Siepmann 2021-02-16 12:02:10 +01:00
parent 9176ba0cec
commit eab2eee8b3
13 changed files with 518 additions and 27 deletions

View file

@ -71,6 +71,7 @@ class TouristAttraction implements Converter
'managed_by' => $manager ? $manager->getUid() : 0,
'town' => $town ? $town->getUid() : 0,
'opening_hours' => json_encode($this->parser->getOpeningHours($jsonLD)),
'address' => json_encode($this->parser->getAddress($jsonLD)),
]
);
$entities->add($entity);

View file

@ -23,16 +23,20 @@ namespace WerkraumMedia\ThueCat\Domain\Import\JsonLD;
* 02110-1301, USA.
*/
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Address;
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\OpeningHours;
class Parser
{
private OpeningHours $openingHours;
private Address $address;
public function __construct(
OpeningHours $openingHours
OpeningHours $openingHours,
Address $address
) {
$this->openingHours = $openingHours;
$this->address = $address;
}
public function getId(array $jsonLD): string
{
@ -69,6 +73,11 @@ class Parser
return $this->openingHours->get($jsonLD);
}
public function getAddress(array $jsonLD): array
{
return $this->address->get($jsonLD);
}
/**
* @return string[]
*/

View file

@ -0,0 +1,72 @@
<?php
namespace WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
class Address
{
public function get(array $jsonLD): array
{
$address = $jsonLD['schema:address'] ?? [];
if (isset($address['@id']) === false) {
return [];
}
return [
'street' => $this->getStreet($address),
'zip' => $this->getZip($address),
'city' => $this->getCity($address),
'email' => $this->getEmail($address),
'phone' => $this->getPhone($address),
'fax' => $this->getFax($address),
];
}
private function getStreet(array $address): string
{
return $address['schema:streetAddress']['@value'] ?? '';
}
private function getZip(array $address): string
{
return $address['schema:postalCode']['@value'] ?? '';
}
private function getCity(array $address): string
{
return $address['schema:addressLocality']['@value'] ?? '';
}
private function getEmail(array $address): string
{
return $address['schema:email']['@value'] ?? '';
}
private function getPhone(array $address): string
{
return $address['schema:telephone']['@value'] ?? '';
}
private function getFax(array $address): string
{
return $address['schema:faxNumber']['@value'] ?? '';
}
}

View file

@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace WerkraumMedia\ThueCat\Domain\Model\Frontend;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use TYPO3\CMS\Core\Type\TypeInterface;
class Address implements TypeInterface
{
private string $serialized;
private array $data;
public function __construct(string $serialized)
{
$this->serialized = $serialized;
$this->data = json_decode($serialized, true);
}
public function getStreet(): string
{
return $this->data['street'] ?? '';
}
public function getZip(): string
{
return $this->data['zip'] ?? '';
}
public function getCity(): string
{
return $this->data['city'] ?? '';
}
public function getEmail(): string
{
return $this->data['email'] ?? '';
}
public function getPhone(): string
{
return $this->data['phone'] ?? '';
}
public function getFax(): string
{
return $this->data['fax'] ?? '';
}
public function __toString(): string
{
return $this->serialized;
}
}

View file

@ -30,6 +30,7 @@ class TouristAttraction extends AbstractEntity
protected string $title = '';
protected string $description = '';
protected ?OpeningHours $openingHours = null;
protected ?Address $address = null;
protected ?Town $town = null;
public function getTitle(): string
@ -47,6 +48,11 @@ class TouristAttraction extends AbstractEntity
return $this->openingHours;
}
public function getAddress(): ?Address
{
return $this->address;
}
public function getTown(): ?Town
{
return $this->town;

View file

@ -42,6 +42,13 @@ return (static function (string $extensionKey, string $tableName) {
'readOnly' => true,
],
],
'address' => [
'label' => $languagePath . '.address',
'config' => [
'type' => 'text',
'readOnly' => true,
],
],
'remote_id' => [
'label' => $languagePath . '.remote_id',
'config' => [
@ -82,7 +89,7 @@ return (static function (string $extensionKey, string $tableName) {
],
'types' => [
'0' => [
'showitem' => 'title, description, opening_hours, remote_id, town, managed_by',
'showitem' => 'title, description, opening_hours, address, remote_id, town, managed_by',
],
],
];

View file

@ -84,6 +84,12 @@
<trans-unit id="tx_thuecat_tourist_attraction.remote_id" xml:space="preserve">
<source>Remote ID</source>
</trans-unit>
<trans-unit id="tx_thuecat_tourist_attraction.opening_hours" xml:space="preserve">
<source>Opening Hours</source>
</trans-unit>
<trans-unit id="tx_thuecat_tourist_attraction.address" xml:space="preserve">
<source>Address</source>
</trans-unit>
<trans-unit id="tx_thuecat_tourist_attraction.town" xml:space="preserve">
<source>Town</source>
</trans-unit>

View file

@ -1,21 +1,27 @@
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"
data-namespace-typo3-fluid="true">
<f:for each="{entities}" as="entity">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">{entity.title} ({entity.town.title})</h5>
<p class="card-text">{entity.description}</p>
<h5>{entity.title} ({entity.town.title})</h5>
<p>{entity.description}</p>
<f:for each="{entity.openingHours}" as="openingHour">
<p>
<f:for each="{openingHour.daysOfWeekWithMondayFirstWeekDay}" as="weekday">
{weekday}: {openingHour.opens} - {openingHour.closes}<br>
</f:for>
{openingHour.from -> f:format.date(format: 'd.m.Y')} -
{openingHour.through -> f:format.date(format: 'd.m.Y')}
</p>
<f:for each="{entity.openingHours}" as="openingHour">
<p>
<f:for each="{openingHour.daysOfWeekWithMondayFirstWeekDay}" as="weekday">
{weekday}: {openingHour.opens} - {openingHour.closes}<br>
</f:for>
</div>
</div>
{openingHour.from -> f:format.date(format: 'd.m.Y')} -
{openingHour.through -> f:format.date(format: 'd.m.Y')}
</p>
</f:for>
<f:if condition="{entity.address}">
<p>
{entity.address.street}<br>
{entity.address.zip} {entity.address.city}<br>
{entity.address.email}<br>
{entity.address.phone}<br>
{entity.address.fax}
</p>
</f:if>
</f:for>
</html>

View file

@ -117,6 +117,7 @@ class TouristAttractionTest extends TestCase
$parser->getTitle($jsonLD, 'de')->willReturn('Title');
$parser->getDescription($jsonLD, 'de')->willReturn('Description');
$parser->getOpeningHours($jsonLD)->willReturn([]);
$parser->getAddress($jsonLD)->willReturn([]);
$organisationRepository = $this->prophesize(OrganisationRepository::class);
$townRepository = $this->prophesize(TownRepository::class);
@ -142,6 +143,7 @@ class TouristAttractionTest extends TestCase
'managed_by' => 0,
'town' => 0,
'opening_hours' => '[]',
'address' => '[]',
], $entity->getData());
}
@ -184,6 +186,7 @@ class TouristAttractionTest extends TestCase
$parser->getTitle($jsonLD, 'de')->willReturn('Title');
$parser->getDescription($jsonLD, 'de')->willReturn('Description');
$parser->getOpeningHours($jsonLD)->willReturn([]);
$parser->getAddress($jsonLD)->willReturn([]);
$organisation = $this->prophesize(Organisation::class);
$organisation->getUid()->willReturn(10);
@ -219,6 +222,7 @@ class TouristAttractionTest extends TestCase
'managed_by' => 10,
'town' => 20,
'opening_hours' => '[]',
'address' => '[]',
], $entity->getData());
}
}

View file

@ -0,0 +1,115 @@
<?php
namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import\JsonLD\Parser;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Address;
use PHPUnit\Framework\TestCase;
/**
* @covers WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Address
*/
class AddressTest extends TestCase
{
/**
* @test
*/
public function instanceCanBeCreated(): void
{
$subject = new Address(
);
self::assertInstanceOf(Address::class, $subject);
}
/**
* @test
*/
public function returnsEmptyArrayAsFallback(): void
{
$subject = new Address(
);
$result = $subject->get([]);
self::assertSame([], $result);
}
/**
* @test
*/
public function returnsFullAddress(): void
{
$subject = new Address(
);
$result = $subject->get([
'schema:address' => [
'@id' => 'genid-28b33237f71b41e3ad54a99e1da769b9-b0',
'schema:addressLocality' => [
'@language' => 'de',
'@value' => 'Erfurt',
],
'schema:addressCountry' => [
'@type' => 'thuecat:AddressCountry',
'@value' => 'thuecat:Germany',
],
'schema:postalCode' => [
'@language' => 'de',
'@value' => '99084',
],
'schema:addressRegion' => [
'@type' => 'thuecat:AddressFederalState',
'@value' => 'thuecat:Thuringia',
],
'schema:telephone' => [
'@language' => 'de',
'@value' => '+49 361 999999',
],
'schema:email' => [
'@language' => 'de',
'@value' => 'altesynagoge@example.com',
],
'schema:streetAddress' => [
'@language' => 'de',
'@value' => 'Waagegasse 8',
],
'schema:faxNumber' => [
'@language' => 'de',
'@value' => '+49 361 999998',
],
'thuecat:typOfAddress' => [
'@type' => 'thuecat:TypOfAddress',
'@value' => 'thuecat:HouseAddress',
]
],
]);
self::assertSame([
'street' => 'Waagegasse 8',
'zip' => '99084',
'city' => 'Erfurt',
'email' => 'altesynagoge@example.com',
'phone' => '+49 361 999999',
'fax' => '+49 361 999998',
], $result);
}
}

View file

@ -26,6 +26,7 @@ namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Import\JsonLD;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser;
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\Address;
use WerkraumMedia\ThueCat\Domain\Import\JsonLD\Parser\OpeningHours;
/**
@ -41,8 +42,10 @@ class ParserTest extends TestCase
public function canBeCreated(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
self::assertInstanceOf(Parser::class, $subject);
@ -54,9 +57,12 @@ class ParserTest extends TestCase
public function returnsId(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getId([
'@id' => 'https://example.com/resources/165868194223-id',
]);
@ -70,9 +76,12 @@ class ParserTest extends TestCase
public function returnsManagerId(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getManagerId([
'thuecat:contentResponsible' => [
'@id' => 'https://example.com/resources/165868194223-manager',
@ -89,10 +98,14 @@ class ParserTest extends TestCase
public function returnsTitle(array $jsonLD, string $language, string $expected): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getTitle($jsonLD, $language);
self::assertSame($expected, $result);
}
@ -210,10 +223,14 @@ class ParserTest extends TestCase
public function returnsDescription(array $jsonLD, string $language, string $expected): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getDescription($jsonLD, $language);
self::assertSame($expected, $result);
}
@ -330,9 +347,12 @@ class ParserTest extends TestCase
public function returnsContainedInPlaceIds(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getContainedInPlaceIds([
'schema:containedInPlace' => [
['@id' => 'https://thuecat.org/resources/043064193523-jcyt'],
@ -342,6 +362,7 @@ class ParserTest extends TestCase
['@id' => 'https://thuecat.org/resources/573211638937-gmqb'],
],
]);
self::assertSame([
'https://thuecat.org/resources/043064193523-jcyt',
'https://thuecat.org/resources/349986440346-kbkf',
@ -357,8 +378,10 @@ class ParserTest extends TestCase
public function returnsLanguages(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getLanguages([
@ -391,8 +414,10 @@ class ParserTest extends TestCase
public function throwsExceptionOnUnkownLanguage(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$this->expectExceptionCode(1612367481);
@ -412,8 +437,10 @@ class ParserTest extends TestCase
public function returnsNoLanguagesIfInfoIsMissing(): void
{
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getLanguages([]);
@ -445,13 +472,51 @@ class ParserTest extends TestCase
$openingHours = $this->prophesize(OpeningHours::class);
$openingHours->get($jsonLD)->willReturn($generatedOpeningHours);
$address = $this->prophesize(Address::class);
$subject = new Parser(
$openingHours->reveal()
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getOpeningHours($jsonLD);
self::assertSame($generatedOpeningHours, $result);
}
/**
* @test
*/
public function returnsAddress(): void
{
$jsonLD = [
'schema:address' => [
'@id' => 'genid-28b33237f71b41e3ad54a99e1da769b9-b0',
'schema:addressLocality' => [
'@language' => 'de',
'@value' => 'Erfurt',
],
],
];
$generatedAddress = [
'street' => '',
'zip' => '',
'city' => 'Erfurt',
'email' => '',
'phone' => '',
'fax' => '',
];
$openingHours = $this->prophesize(OpeningHours::class);
$address = $this->prophesize(Address::class);
$address->get($jsonLD)->willReturn($generatedAddress);
$subject = new Parser(
$openingHours->reveal(),
$address->reveal()
);
$result = $subject->getAddress($jsonLD);
self::assertSame($generatedAddress, $result);
}
}

View file

@ -0,0 +1,126 @@
<?php
namespace WerkraumMedia\ThueCat\Tests\Unit\Domain\Model\Frontend;
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use WerkraumMedia\ThueCat\Domain\Model\Frontend\Address;
use PHPUnit\Framework\TestCase;
/**
* @covers WerkraumMedia\ThueCat\Domain\Model\Frontend\Address
*/
class AddressTest extends TestCase
{
/**
* @test
*/
public function canBeCreated(): void
{
$subject = new Address('[]');
self::assertInstanceOf(Address::class, $subject);
}
/**
* @test
*/
public function returnsProperDefaults(): void
{
$subject = new Address('[]');
self::assertSame('', $subject->getStreet());
self::assertSame('', $subject->getZip());
self::assertSame('', $subject->getCity());
self::assertSame('', $subject->getEmail());
self::assertSame('', $subject->getPhone());
self::assertSame('', $subject->getFax());
}
/**
* @test
*/
public function returnsStreet(): void
{
$subject = new Address('{"street": "Example Street 10"}');
self::assertSame('Example Street 10', $subject->getStreet());
}
/**
* @test
*/
public function returnsZip(): void
{
$subject = new Address('{"zip": "09084"}');
self::assertSame('09084', $subject->getZip());
}
/**
* @test
*/
public function returnsCity(): void
{
$subject = new Address('{"city": "Erfurt"}');
self::assertSame('Erfurt', $subject->getCity());
}
/**
* @test
*/
public function returnsEmail(): void
{
$subject = new Address('{"email": "example@example.com"}');
self::assertSame('example@example.com', $subject->getEmail());
}
/**
* @test
*/
public function returnsPhone(): void
{
$subject = new Address('{"phone": "+49 361 99999"}');
self::assertSame('+49 361 99999', $subject->getPhone());
}
/**
* @test
*/
public function returnsFax(): void
{
$subject = new Address('{"fax": "+49 361 99998"}');
self::assertSame('+49 361 99998', $subject->getFax());
}
/**
* @test
*/
public function returnsSerializedString(): void
{
$subject = new Address('{"street": "Example Street 10"}');
self::assertSame('{"street": "Example Street 10"}', (string) $subject);
}
}

View file

@ -48,4 +48,5 @@ CREATE TABLE tx_thuecat_tourist_attraction (
title varchar(255) DEFAULT '' NOT NULL,
description text DEFAULT '' NOT NULL,
opening_hours text DEFAULT '' NOT NULL,
address text DEFAULT '' NOT NULL,
);