Skip to content

Commit a599bc0

Browse files
committed
refactor: place info
1 parent b3bdc00 commit a599bc0

5 files changed

Lines changed: 103 additions & 62 deletions

File tree

library/Controller/SingularEvent/Mappers/MapCurrentOccasion.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ public function __construct(OccasionInterface ...$occasions)
1818

1919
public function map(Event $event): ?OccasionInterface
2020
{
21+
2122
foreach ($this->occasions as $occasion) {
2223
if ($occasion->isCurrent()) {
2324
return $occasion;
2425
}
2526
}
26-
27+
// die;
2728
return null;
2829
}
2930
}

library/Controller/SingularEvent/Mappers/MapOccasions.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public function __construct(
3434
*/
3535
public function map(Event $event): array
3636
{
37-
return array_filter(array_map(
37+
$time = array_filter(array_map(
3838
fn(Schedule $schedule) => $this->mapScheduleToOccasion($schedule),
3939
EnsureArrayOf::ensureArrayOf($event->getProperty('eventSchedule'), Schedule::class),
4040
));
41+
42+
return $time;
4143
}
4244

4345
/**
@@ -51,13 +53,15 @@ private function mapScheduleToOccasion(Schedule $schedule): ?OccasionInterface
5153
$startDate = $this->getStartDateAsStringFromSchedule($schedule);
5254
$endDate = $this->getEndDateAsStringFromSchedule($schedule);
5355

54-
return !empty($startDate) && !empty($endDate)
56+
$item = !empty($startDate) && !empty($endDate)
5557
? new Occasion(
5658
$startDate,
5759
$endDate,
5860
$this->isCurrentOccasion($schedule),
5961
$this->getUrlFromSchedule($schedule),
6062
) : null;
63+
64+
return $item;
6165
}
6266

6367
/**

library/Controller/SingularPlace.php

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Municipio\Controller;
44

5-
use Municipio\Helper\EnsureArrayOf\EnsureArrayOf;
6-
use Municipio\Helper\Listing;
5+
use Municipio\Controller\SingularPlace\GetPlaceActions;
6+
use Municipio\Controller\SingularPlace\GetPlaceInfoList;
77

88
/**
99
* Class SingularPlace
@@ -22,70 +22,18 @@ public function init()
2222

2323
$this->data['relatedPosts'] = $this->getRelatedPosts($pageID);
2424
$this->data['placeInfoList'] = $this->createPlaceInfoList();
25-
$this->data['placeActions'] = $this->createActions($pageID);
25+
$this->data['placeActions'] = $this->createActions();
2626
}
2727

2828
private function createActions(): array
2929
{
30-
$tourBookingPage = $this->createTourBookingPageAction();
31-
32-
return $this->wpService->applyFilters('Municipio/Controller/SingularPlace/actions', $tourBookingPage, $this->post);
30+
$placeActions = GetPlaceActions::getPlaceActions($this->post);
31+
return $this->wpService->applyFilters('Municipio/Controller/SingularPlace/actions', $placeActions, $this->post);
3332
}
3433

3534
private function createPlaceInfoList(): array
3635
{
37-
$telephone = $this->createTelephoneListingItems();
38-
$website = $this->createWebsiteListingItems();
39-
$listing = array_merge($telephone, $website);
40-
41-
return $this->wpService->applyFilters('Municipio/Controller/SingularPlace/placeInfoList', $listing, $this->post);
42-
}
43-
44-
private function createTourBookingPageAction(): array
45-
{
46-
$tourBookingPage = EnsureArrayOf::ensureArrayOf($this->post->getSchemaProperty('tourBookingPage'), 'string');
47-
48-
$formattedTourBookingPages = [];
49-
foreach ($tourBookingPage as $index => $bookingLink) {
50-
$formattedTourBookingPages[] = [
51-
'text' => $this->wpService->__('Book here', 'municipio'),
52-
'href' => $bookingLink,
53-
'color' => 'primary',
54-
'style' => 'filled',
55-
'classList' => ['u-width--100'],
56-
];
57-
}
58-
59-
return $formattedTourBookingPages;
60-
}
61-
62-
private function createTelephoneListingItems(): array
63-
{
64-
$telephoneData = EnsureArrayOf::ensureArrayOf($this->post->getSchemaProperty('telephone'), 'string');
65-
66-
if (empty($telephoneData)) {
67-
return [];
68-
}
69-
70-
return array_map(fn($phone) => Listing::createListingItem(
71-
$phone,
72-
'',
73-
['src' => 'call'],
74-
), $telephoneData);
75-
}
76-
77-
private function createWebsiteListingItems(): array
78-
{
79-
$websiteData = EnsureArrayOf::ensureArrayOf($this->post->getSchemaProperty('url'), 'string');
80-
81-
if (empty($websiteData)) {
82-
return [];
83-
}
84-
85-
return array_map(fn($url) => Listing::createListingItem(
86-
$url,
87-
$url,
88-
['src' => 'language'],
89-
), $websiteData);
36+
$placeInfoList = GetPlaceInfoList::getPlaceInfoList($this->post);
37+
return $this->wpService->applyFilters('Municipio/Controller/SingularPlace/placeInfoList', $placeInfoList, $this->post);
9038
}
9139
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Municipio\Controller\SingularPlace;
4+
5+
use Municipio\Helper\EnsureArrayOf\EnsureArrayOf;
6+
use Municipio\Helper\WpService;
7+
use Municipio\PostObject\PostObjectInterface;
8+
9+
class GetPlaceActions
10+
{
11+
public static function getPlaceActions(PostObjectInterface $place): array
12+
{
13+
$tourBookingPage = self::createTourBookingPageAction($place);
14+
return $tourBookingPage;
15+
}
16+
17+
private static function createTourBookingPageAction(PostObjectInterface $place): array
18+
{
19+
$wpService = WpService::get();
20+
$tourBookingPage = EnsureArrayOf::ensureArrayOf($place->getSchemaProperty('tourBookingPage'), 'string');
21+
22+
$formattedTourBookingPages = [];
23+
foreach ($tourBookingPage as $index => $bookingLink) {
24+
$formattedTourBookingPages[] = [
25+
'text' => $wpService->__('Book here', 'municipio'),
26+
'href' => $bookingLink,
27+
'color' => 'primary',
28+
'style' => 'filled',
29+
'classList' => ['u-width--100'],
30+
];
31+
}
32+
33+
return $formattedTourBookingPages;
34+
}
35+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Municipio\Controller\SingularPlace;
4+
5+
use Municipio\Helper\EnsureArrayOf\EnsureArrayOf;
6+
use Municipio\Helper\Listing;
7+
use Municipio\PostObject\PostObjectInterface;
8+
9+
class GetPlaceInfoList
10+
{
11+
public static function getPlaceInfoList(PostObjectInterface $place): array
12+
{
13+
if ($place->getSchemaProperty('@type') !== 'Place') {
14+
return [];
15+
}
16+
17+
$telephone = self::createTelephoneListingItems($place);
18+
$website = self::createWebsiteListingItems($place);
19+
$listing = array_merge($telephone, $website);
20+
21+
return $listing;
22+
}
23+
24+
private static function createTelephoneListingItems(PostObjectInterface $place): array
25+
{
26+
$telephoneData = EnsureArrayOf::ensureArrayOf($place->getSchemaProperty('telephone'), 'string');
27+
28+
if (empty($telephoneData)) {
29+
return [];
30+
}
31+
32+
return array_map(fn($phone) => Listing::createListingItem(
33+
$phone,
34+
'',
35+
['src' => 'call'],
36+
), $telephoneData);
37+
}
38+
39+
private static function createWebsiteListingItems(PostObjectInterface $place): array
40+
{
41+
$websiteData = EnsureArrayOf::ensureArrayOf($place->getSchemaProperty('url'), 'string');
42+
43+
if (empty($websiteData)) {
44+
return [];
45+
}
46+
47+
return array_map(fn($url) => Listing::createListingItem(
48+
$url,
49+
$url,
50+
['src' => 'language'],
51+
), $websiteData);
52+
}
53+
}

0 commit comments

Comments
 (0)