|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CultuurNet\UDB3\Place\ReadModel\JSONLD; |
|
7
|
|
|
|
|
8
|
|
|
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\CdbXMLItemBaseImporter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Takes care of importing actors in the CdbXML format (UDB2) that represent |
|
12
|
|
|
* a place, into a UDB3 JSON-LD document. |
|
13
|
|
|
*/ |
|
14
|
|
|
class CdbXMLImporter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var CdbXMLItemBaseImporter |
|
18
|
|
|
*/ |
|
19
|
|
|
private $cdbXMLItemBaseImporter; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(CdbXMLItemBaseImporter $dbXMLItemBaseImporter) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->cdbXMLItemBaseImporter = $dbXMLItemBaseImporter; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Imports a UDB2 organizer actor into a UDB3 JSON-LD document. |
|
31
|
|
|
* |
|
32
|
|
|
* @param \stdClass $base |
|
33
|
|
|
* The JSON-LD document object to start from. |
|
34
|
|
|
* @param \CultureFeed_Cdb_Item_Base $item |
|
35
|
|
|
* The event/actor data from UDB2 to import. |
|
36
|
|
|
* |
|
37
|
|
|
* @return \stdClass |
|
38
|
|
|
* A new JSON-LD document object with the UDB2 actor data merged in. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function documentWithCdbXML( |
|
41
|
|
|
$base, |
|
42
|
|
|
\CultureFeed_Cdb_Item_Base $item |
|
43
|
|
|
) { |
|
44
|
|
|
$jsonLD = clone $base; |
|
45
|
|
|
|
|
46
|
|
|
$detail = null; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \CultureFeed_Cdb_Data_ActorDetail[] $details */ |
|
49
|
|
|
$details = $item->getDetails(); |
|
50
|
|
|
|
|
51
|
|
|
foreach ($details as $languageDetail) { |
|
52
|
|
|
// The first language detail found will be used to retrieve |
|
53
|
|
|
// properties from which in UDB3 are not any longer considered |
|
54
|
|
|
// to be language specific. |
|
55
|
|
|
if (!$detail) { |
|
56
|
|
|
$detail = $languageDetail; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$descriptions = [ |
|
61
|
|
|
trim($detail->getShortDescription()), |
|
62
|
|
|
trim($detail->getLongDescription()) |
|
63
|
|
|
]; |
|
64
|
|
|
$descriptions = array_filter($descriptions); |
|
65
|
|
|
if (count($descriptions) > 0) { |
|
66
|
|
|
$jsonLD->description = implode('<br/>', $descriptions); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$jsonLD->name = $detail->getTitle(); |
|
70
|
|
|
|
|
71
|
|
|
$this->cdbXMLItemBaseImporter->importPublicationInfo($item, $jsonLD); |
|
72
|
|
|
$this->cdbXMLItemBaseImporter->importAvailable($item, $jsonLD); |
|
73
|
|
|
$this->cdbXMLItemBaseImporter->importExternalId($item, $jsonLD); |
|
74
|
|
|
|
|
75
|
|
|
// Address |
|
76
|
|
|
$contact_cdb = $item->getContactInfo(); |
|
|
|
|
|
|
77
|
|
|
if ($contact_cdb) { |
|
78
|
|
|
$addresses = $contact_cdb->getAddresses(); |
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
foreach ($addresses as $address) { |
|
|
|
|
|
|
81
|
|
|
$address = $address->getPhysicalAddress(); |
|
82
|
|
|
|
|
83
|
|
|
if ($address) { |
|
84
|
|
|
$jsonLD->address = array( |
|
85
|
|
|
'addressCountry' => $address->getCountry(), |
|
86
|
|
|
'addressLocality' => $address->getCity(), |
|
87
|
|
|
'postalCode' => $address->getZip(), |
|
88
|
|
|
'streetAddress' => |
|
89
|
|
|
$address->getStreet() . ' ' . |
|
90
|
|
|
$address->getHouseNumber(), |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
break; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Booking info. |
|
99
|
|
|
$bookingInfo = array( |
|
100
|
|
|
'description' => '', |
|
101
|
|
|
'name' => 'standard price', |
|
102
|
|
|
'price' => 0.0, |
|
103
|
|
|
'priceCurrency' => 'EUR', |
|
104
|
|
|
); |
|
105
|
|
|
$price = $detail->getPrice(); |
|
106
|
|
|
|
|
107
|
|
|
if ($price) { |
|
108
|
|
|
$bookingInfo['description'] = floatval($price->getDescription()); |
|
109
|
|
|
$bookingInfo['name'] = floatval($price->getTitle()); |
|
110
|
|
|
$bookingInfo['price'] = floatval($price->getValue()); |
|
111
|
|
|
} |
|
112
|
|
|
$jsonLD->bookingInfo = $bookingInfo; |
|
113
|
|
|
|
|
114
|
|
|
// Image. |
|
115
|
|
|
$images = $detail->getMedia()->byMediaType( |
|
116
|
|
|
\CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO |
|
117
|
|
|
); |
|
118
|
|
|
$images->rewind(); |
|
119
|
|
|
$image = count($images) > 0 ? $images->current() : null; |
|
120
|
|
|
if ($image) { |
|
121
|
|
|
$jsonLD->image = $image->getHLink(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->importTerms($item, $jsonLD); |
|
125
|
|
|
|
|
126
|
|
|
return $jsonLD; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function eventDocumentWithCdbXML( |
|
130
|
|
|
$base, |
|
131
|
|
|
\CultureFeed_Cdb_Item_Base $item |
|
132
|
|
|
) { |
|
133
|
|
|
$jsonLD = $this->documentWithCdbXML($base, $item); |
|
134
|
|
|
|
|
135
|
|
|
return $jsonLD; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param \CultureFeed_Cdb_Item_Actor $actor |
|
140
|
|
|
* @param \stdClass $jsonLD |
|
141
|
|
|
*/ |
|
142
|
|
|
private function importTerms(\CultureFeed_Cdb_Item_Base $actor, $jsonLD) |
|
143
|
|
|
{ |
|
144
|
|
|
$themeBlacklist = []; |
|
145
|
|
|
$categories = array(); |
|
146
|
|
View Code Duplication |
foreach ($actor->getCategories() as $category) { |
|
|
|
|
|
|
147
|
|
|
/* @var \Culturefeed_Cdb_Data_Category $category */ |
|
148
|
|
|
if ($category && !in_array($category->getName(), $themeBlacklist)) { |
|
149
|
|
|
$categories[] = array( |
|
150
|
|
|
'label' => $category->getName(), |
|
151
|
|
|
'domain' => $category->getType(), |
|
152
|
|
|
'id' => $category->getId(), |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
$jsonLD->terms = $categories; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: