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
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $basePriceTranslations; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param PriceDescriptionParser $priceDescriptionParser |
34
|
|
|
* @param array $basePriceTranslations |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
PriceDescriptionParser $priceDescriptionParser, |
38
|
|
|
array $basePriceTranslations |
39
|
|
|
) { |
40
|
|
|
$this->priceDescriptionParser = $priceDescriptionParser; |
41
|
|
|
$this->basePriceTranslations = $basePriceTranslations; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
46
|
|
|
* @param \stdClass $jsonLD |
47
|
|
|
*/ |
48
|
|
|
public function importPublicationInfo( |
49
|
|
|
CultureFeed_Cdb_Item_Base $item, |
50
|
|
|
\stdClass $jsonLD |
51
|
|
|
) { |
52
|
|
|
$jsonLD->creator = $item->getCreatedBy(); |
53
|
|
|
|
54
|
|
|
$itemCreationDate = $item->getCreationDate(); |
55
|
|
|
|
56
|
|
|
if (!empty($itemCreationDate)) { |
57
|
|
|
// format using ISO-8601 with time zone designator |
58
|
|
|
$creationDate = DateTimeFactory::dateTimeFromDateString( |
59
|
|
|
$itemCreationDate |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$jsonLD->created = $creationDate->format('c'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$itemLastUpdatedDate = $item->getLastUpdated(); |
66
|
|
|
|
67
|
|
|
if (!empty($itemLastUpdatedDate)) { |
68
|
|
|
$lastUpdatedDate = DateTimeFactory::dateTimeFromDateString( |
69
|
|
|
$itemLastUpdatedDate |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$jsonLD->modified = $lastUpdatedDate->format('c'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$jsonLD->publisher = $item->getOwner(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
80
|
|
|
* @param \stdClass $jsonLD |
81
|
|
|
*/ |
82
|
|
|
public function importAvailable( |
83
|
|
|
\CultureFeed_Cdb_Item_Base $item, |
84
|
|
|
\stdClass $jsonLD |
85
|
|
|
) { |
86
|
|
|
$availableFromString = $item->getAvailableFrom(); |
87
|
|
|
if ($availableFromString) { |
88
|
|
|
$jsonLD->availableFrom = $this->formatAvailableString( |
89
|
|
|
$availableFromString |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$availableToString = $item->getAvailableTo(); |
94
|
|
|
if ($availableToString) { |
95
|
|
|
$jsonLD->availableTo = $this->formatAvailableString( |
96
|
|
|
$availableToString |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $availableString |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
private function formatAvailableString($availableString) |
106
|
|
|
{ |
107
|
|
|
$available = DateTimeFactory::dateTimeFromDateString( |
108
|
|
|
$availableString |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $available->format('c'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
116
|
|
|
* @param \stdClass $jsonLD |
117
|
|
|
*/ |
118
|
|
|
public function importExternalId( |
119
|
|
|
\CultureFeed_Cdb_Item_Base $item, |
120
|
|
|
\stdClass $jsonLD |
121
|
|
|
) { |
122
|
|
|
$externalId = $item->getExternalId(); |
123
|
|
|
if (empty($externalId)) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$externalIdIsCDB = (strpos($externalId, 'CDB:') === 0); |
128
|
|
|
|
129
|
|
|
if (!property_exists($jsonLD, 'sameAs')) { |
130
|
|
|
$jsonLD->sameAs = []; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!$externalIdIsCDB) { |
134
|
|
|
if (!in_array($externalId, $jsonLD->sameAs)) { |
135
|
|
|
array_push($jsonLD->sameAs, $externalId); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param CultureFeed_Cdb_Item_Base $item |
142
|
|
|
* @param \stdClass $jsonLD |
143
|
|
|
*/ |
144
|
|
|
public function importWorkflowStatus( |
145
|
|
|
CultureFeed_Cdb_Item_Base $item, |
146
|
|
|
\stdClass $jsonLD |
147
|
|
|
) { |
148
|
|
|
$wfStatus = $item->getWfStatus(); |
149
|
|
|
|
150
|
|
|
$workflowStatus = $wfStatus ? WorkflowStatus::fromNative($wfStatus) : WorkflowStatus::READY_FOR_VALIDATION(); |
151
|
|
|
|
152
|
|
|
$jsonLD->workflowStatus = $workflowStatus->getName(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param \CultureFeed_Cdb_Data_DetailList|\CultureFeed_Cdb_Data_Detail[] $details |
157
|
|
|
* @param \stdClass $jsonLD |
158
|
|
|
*/ |
159
|
|
|
public function importPriceInfo( |
160
|
|
|
\CultureFeed_Cdb_Data_DetailList $details, |
161
|
|
|
$jsonLD |
162
|
|
|
) { |
163
|
|
|
$mainLanguage = $this->getMainLanguage($jsonLD); |
164
|
|
|
|
165
|
|
|
$detailsArray = []; |
166
|
|
|
foreach ($details as $detail) { |
167
|
|
|
$detailsArray[] = $detail; |
168
|
|
|
} |
169
|
|
|
$details = $detailsArray; |
170
|
|
|
|
171
|
|
|
$mainLanguageDetails = array_filter( |
172
|
|
|
$details, |
173
|
|
|
function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) { |
174
|
|
|
return $detail->getLanguage() === $mainLanguage->getCode(); |
175
|
|
|
} |
176
|
|
|
); |
177
|
|
|
|
178
|
|
|
/* @var \CultureFeed_Cdb_Data_EventDetail $mainLanguageDetail */ |
179
|
|
|
$mainLanguageDetail = reset($mainLanguageDetails); |
180
|
|
|
if (!$mainLanguageDetail) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$mainLanguagePrice = $mainLanguageDetail->getPrice(); |
185
|
|
|
|
186
|
|
|
if (!$mainLanguagePrice) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$basePrice = $mainLanguagePrice->getValue(); |
191
|
|
|
if ($basePrice !== null) { |
192
|
|
|
$basePrice = floatval($basePrice); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (!$basePrice) { |
196
|
|
|
return; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$basePrice = new BasePrice( |
200
|
|
|
Price::fromFloat($basePrice), |
201
|
|
|
Currency::fromNative('EUR') |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
/* @var Tariff[] $tariffs */ |
205
|
|
|
$tariffs = []; |
206
|
|
|
foreach ($details as $detail) { |
207
|
|
|
$language = null; |
|
|
|
|
208
|
|
|
$price = null; |
|
|
|
|
209
|
|
|
$description = null; |
|
|
|
|
210
|
|
|
|
211
|
|
|
$language = $detail->getLanguage(); |
212
|
|
|
|
213
|
|
|
$price = $detail->getPrice(); |
214
|
|
|
if (!$price) { |
215
|
|
|
continue; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$description = $price->getDescription(); |
219
|
|
|
if (!$description) { |
220
|
|
|
continue; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$translatedTariffs = $this->priceDescriptionParser->parse($description); |
224
|
|
|
if (empty($translatedTariffs)) { |
225
|
|
|
continue; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Skip the base price. |
229
|
|
|
array_shift($translatedTariffs); |
230
|
|
|
|
231
|
|
|
$tariffIndex = 0; |
232
|
|
|
foreach ($translatedTariffs as $tariffName => $tariffPrice) { |
233
|
|
|
if (!isset($tariffs[$tariffIndex])) { |
234
|
|
|
$tariff = new Tariff( |
235
|
|
|
new MultilingualString(new Language($language), new StringLiteral($tariffName)), |
236
|
|
|
Price::fromFloat($tariffPrice), |
237
|
|
|
Currency::fromNative('EUR') |
238
|
|
|
); |
239
|
|
|
} else { |
240
|
|
|
$tariff = $tariffs[$tariffIndex]; |
241
|
|
|
$name = $tariff->getName(); |
242
|
|
|
$name = $name->withTranslation(new Language($language), new StringLiteral($tariffName)); |
243
|
|
|
$tariff = new Tariff( |
244
|
|
|
$name, |
245
|
|
|
$tariff->getPrice(), |
246
|
|
|
$tariff->getCurrency() |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$tariffs[$tariffIndex] = $tariff; |
251
|
|
|
$tariffIndex++; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$jsonLD->priceInfo = [ |
256
|
|
|
[ |
257
|
|
|
'category' => 'base', |
258
|
|
|
'name' => $this->basePriceTranslations, |
259
|
|
|
'price' => $basePrice->getPrice()->toFloat(), |
260
|
|
|
'priceCurrency' => $basePrice->getCurrency()->getCode()->toNative(), |
261
|
|
|
], |
262
|
|
|
]; |
263
|
|
|
|
264
|
|
View Code Duplication |
foreach ($tariffs as $tariff) { |
|
|
|
|
265
|
|
|
$jsonLD->priceInfo[] = [ |
266
|
|
|
'category' => 'tariff', |
267
|
|
|
'name' => $tariff->getName()->serialize(), |
268
|
|
|
'price' => $tariff->getPrice()->toFloat(), |
269
|
|
|
'priceCurrency' => $tariff->getCurrency()->getCode()->toNative(), |
270
|
|
|
]; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.