1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Offer\ReadModel\JSONLD; |
4
|
|
|
|
5
|
|
|
use CultureFeed_Cdb_Item_Base; |
6
|
|
|
use CultuurNet\UDB3\Cdb\DateTimeFactory; |
7
|
|
|
use CultuurNet\UDB3\Cdb\PriceDescriptionParser; |
8
|
|
|
use CultuurNet\UDB3\Language; |
9
|
|
|
use CultuurNet\UDB3\Offer\WorkflowStatus; |
10
|
|
|
use CultuurNet\UDB3\PriceInfo\BasePrice; |
11
|
|
|
use CultuurNet\UDB3\PriceInfo\Price; |
12
|
|
|
use CultuurNet\UDB3\PriceInfo\Tariff; |
13
|
|
|
use CultuurNet\UDB3\ReadModel\MultilingualJsonLDProjectorTrait; |
14
|
|
|
use CultuurNet\UDB3\ValueObject\MultilingualString; |
15
|
|
|
use ValueObjects\Money\Currency; |
16
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
17
|
|
|
|
18
|
|
|
class CdbXMLItemBaseImporter |
19
|
|
|
{ |
20
|
|
|
use MultilingualJsonLDProjectorTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PriceDescriptionParser |
24
|
|
|
*/ |
25
|
|
|
private $priceDescriptionParser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PriceDescriptionParser $priceDescriptionParser |
29
|
|
|
*/ |
30
|
|
|
public function __construct(PriceDescriptionParser $priceDescriptionParser) |
31
|
|
|
{ |
32
|
|
|
$this->priceDescriptionParser = $priceDescriptionParser; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
37
|
|
|
* @param \stdClass $jsonLD |
38
|
|
|
*/ |
39
|
|
|
public function importPublicationInfo( |
40
|
|
|
CultureFeed_Cdb_Item_Base $item, |
41
|
|
|
\stdClass $jsonLD |
42
|
|
|
) { |
43
|
|
|
$jsonLD->creator = $item->getCreatedBy(); |
44
|
|
|
|
45
|
|
|
$itemCreationDate = $item->getCreationDate(); |
46
|
|
|
|
47
|
|
|
if (!empty($itemCreationDate)) { |
48
|
|
|
// format using ISO-8601 with time zone designator |
49
|
|
|
$creationDate = DateTimeFactory::dateTimeFromDateString( |
50
|
|
|
$itemCreationDate |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$jsonLD->created = $creationDate->format('c'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$itemLastUpdatedDate = $item->getLastUpdated(); |
57
|
|
|
|
58
|
|
|
if (!empty($itemLastUpdatedDate)) { |
59
|
|
|
$lastUpdatedDate = DateTimeFactory::dateTimeFromDateString( |
60
|
|
|
$itemLastUpdatedDate |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$jsonLD->modified = $lastUpdatedDate->format('c'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$jsonLD->publisher = $item->getOwner(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
71
|
|
|
* @param \stdClass $jsonLD |
72
|
|
|
*/ |
73
|
|
|
public function importAvailable( |
74
|
|
|
\CultureFeed_Cdb_Item_Base $item, |
75
|
|
|
\stdClass $jsonLD |
76
|
|
|
) { |
77
|
|
|
$availableFromString = $item->getAvailableFrom(); |
78
|
|
|
if ($availableFromString) { |
79
|
|
|
$jsonLD->availableFrom = $this->formatAvailableString( |
80
|
|
|
$availableFromString |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$availableToString = $item->getAvailableTo(); |
85
|
|
|
if ($availableToString) { |
86
|
|
|
$jsonLD->availableTo = $this->formatAvailableString( |
87
|
|
|
$availableToString |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $availableString |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
private function formatAvailableString($availableString) |
97
|
|
|
{ |
98
|
|
|
$available = DateTimeFactory::dateTimeFromDateString( |
99
|
|
|
$availableString |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
return $available->format('c'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
107
|
|
|
* @param \stdClass $jsonLD |
108
|
|
|
*/ |
109
|
|
|
public function importExternalId( |
110
|
|
|
\CultureFeed_Cdb_Item_Base $item, |
111
|
|
|
\stdClass $jsonLD |
112
|
|
|
) { |
113
|
|
|
$externalId = $item->getExternalId(); |
114
|
|
|
if (empty($externalId)) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$externalIdIsCDB = (strpos($externalId, 'CDB:') === 0); |
119
|
|
|
|
120
|
|
|
if (!property_exists($jsonLD, 'sameAs')) { |
121
|
|
|
$jsonLD->sameAs = []; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!$externalIdIsCDB) { |
125
|
|
|
if (!in_array($externalId, $jsonLD->sameAs)) { |
126
|
|
|
array_push($jsonLD->sameAs, $externalId); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
133
|
|
|
* @param \stdClass $jsonLD |
134
|
|
|
*/ |
135
|
|
|
public function importWorkflowStatus( |
136
|
|
|
CultureFeed_Cdb_Item_Base $item, |
137
|
|
|
\stdClass $jsonLD |
138
|
|
|
) { |
139
|
|
|
$wfStatus = $item->getWfStatus(); |
140
|
|
|
|
141
|
|
|
$workflowStatus = $wfStatus ? WorkflowStatus::fromNative($wfStatus) : WorkflowStatus::READY_FOR_VALIDATION(); |
142
|
|
|
|
143
|
|
|
$jsonLD->workflowStatus = $workflowStatus->getName(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param \CultureFeed_Cdb_Data_DetailList $details |
148
|
|
|
* @param \stdClass $jsonLD |
149
|
|
|
*/ |
150
|
|
|
public function importPriceInfo( |
151
|
|
|
\CultureFeed_Cdb_Data_DetailList $details, |
152
|
|
|
$jsonLD |
153
|
|
|
) { |
154
|
|
|
$mainLanguage = $this->getMainLanguage($jsonLD); |
155
|
|
|
|
156
|
|
|
$detailsArray = []; |
157
|
|
|
foreach ($details as $detail) { |
158
|
|
|
$detailsArray[] = $detail; |
159
|
|
|
} |
160
|
|
|
$details = $detailsArray; |
161
|
|
|
|
162
|
|
|
$mainLanguageDetails = array_filter( |
163
|
|
|
$details, |
164
|
|
|
function (\CultureFeed_Cdb_Data_EventDetail $detail) use ($mainLanguage) { |
165
|
|
|
return $detail->getLanguage() === $mainLanguage->getCode(); |
166
|
|
|
} |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
/* @var \CultureFeed_Cdb_Data_EventDetail $mainLanguageDetail */ |
170
|
|
|
$mainLanguageDetail = reset($mainLanguageDetails); |
171
|
|
|
if (!$mainLanguageDetail) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$mainLanguagePrice = $mainLanguageDetail->getPrice(); |
176
|
|
|
|
177
|
|
|
if (!$mainLanguagePrice) { |
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$basePrice = $mainLanguagePrice->getValue(); |
182
|
|
|
if ($basePrice !== null) { |
183
|
|
|
$basePrice = floatval($basePrice); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (!$basePrice) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$basePrice = new BasePrice( |
191
|
|
|
Price::fromFloat($basePrice), |
192
|
|
|
Currency::fromNative('EUR') |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
/* @var Tariff[] $tariffs */ |
196
|
|
|
$tariffs = []; |
197
|
|
|
foreach ($details as $detail) { |
198
|
|
|
$language = $detail->getLanguage(); |
199
|
|
|
|
200
|
|
|
$price = $detail->getPrice(); |
201
|
|
|
if (!$price) { |
202
|
|
|
continue; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$description = $price->getDescription(); |
206
|
|
|
if (!$description) { |
207
|
|
|
continue; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$translatedTariffs = $this->priceDescriptionParser->parse($description); |
211
|
|
|
|
212
|
|
|
// Skip the base price tariff. |
213
|
|
|
array_shift($translatedTariffs); |
214
|
|
|
|
215
|
|
|
$tariffIndex = 0; |
216
|
|
|
foreach ($translatedTariffs as $tariffName => $tariffPrice) { |
217
|
|
|
if (!isset($tariffs[$tariffIndex])) { |
218
|
|
|
$tariff = new Tariff( |
219
|
|
|
new MultilingualString(new Language($language), new StringLiteral($tariffName)), |
220
|
|
|
Price::fromFloat($tariffPrice), |
221
|
|
|
Currency::fromNative('EUR') |
222
|
|
|
); |
223
|
|
|
} else { |
224
|
|
|
$tariff = $tariffs[$tariffIndex]; |
225
|
|
|
$name = $tariff->getName(); |
226
|
|
|
$name = $name->withTranslation(new Language($language), new StringLiteral($tariffName)); |
227
|
|
|
$tariff = new Tariff( |
228
|
|
|
$name, |
229
|
|
|
$tariff->getPrice(), |
230
|
|
|
$tariff->getCurrency() |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$tariffs[$tariffIndex] = $tariff; |
235
|
|
|
$tariffIndex++; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$jsonLD->priceInfo = [ |
240
|
|
|
[ |
241
|
|
|
'category' => 'base', |
242
|
|
|
'name' => [ |
243
|
|
|
'nl' => 'Basistarief', |
244
|
|
|
], |
245
|
|
|
'price' => $basePrice->getPrice()->toFloat(), |
246
|
|
|
'priceCurrency' => $basePrice->getCurrency()->getCode()->toNative(), |
247
|
|
|
] |
248
|
|
|
]; |
249
|
|
|
|
250
|
|
View Code Duplication |
foreach ($tariffs as $tariff) { |
|
|
|
|
251
|
|
|
$jsonLD->priceInfo[] = [ |
252
|
|
|
'category' => 'tariff', |
253
|
|
|
'name' => $tariff->getName()->serialize(), |
254
|
|
|
'price' => $tariff->getPrice()->toFloat(), |
255
|
|
|
'priceCurrency' => $tariff->getCurrency()->getCode()->toNative(), |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.