1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\Cdb\Description; |
4
|
|
|
|
5
|
|
|
use CultuurNet\UDB3\StringFilter\StringFilterInterface; |
6
|
|
|
use ValueObjects\StringLiteral\StringLiteral; |
7
|
|
|
|
8
|
|
|
class MergedDescription extends StringLiteral |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var StringFilterInterface |
12
|
|
|
*/ |
13
|
|
|
private static $shortDescriptionUDB2FormattingFilter; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var StringFilterInterface |
17
|
|
|
*/ |
18
|
|
|
private static $shortDescriptionUDB3FormattingFilter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \CultureFeed_Cdb_Data_Detail $detail |
22
|
|
|
* @return MergedDescription |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public static function fromCdbDetail(\CultureFeed_Cdb_Data_Detail $detail) |
26
|
|
|
{ |
27
|
|
|
$longDescription = $detail->getLongDescription(); |
28
|
|
|
if ($longDescription) { |
29
|
|
|
$longDescription = LongDescription::fromCdbXmlToJsonLdFormat($longDescription); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$shortDescription = $detail->getShortDescription(); |
33
|
|
|
if ($shortDescription) { |
34
|
|
|
$shortDescription = ShortDescription::fromCdbXmlToJsonLdFormat($shortDescription); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($longDescription && $shortDescription) { |
38
|
|
|
return MergedDescription::merge($shortDescription, $longDescription); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($longDescription) { |
42
|
|
|
return new MergedDescription($longDescription->toNative()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($shortDescription) { |
46
|
|
|
return new MergedDescription($shortDescription->toNative()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new \InvalidArgumentException( |
50
|
|
|
'Could not create MergedDescription object from given ' . get_class($detail) . '.' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ShortDescription $shortDescription |
56
|
|
|
* @param LongDescription $longDescription |
57
|
|
|
* @return MergedDescription $longDescription |
58
|
|
|
*/ |
59
|
|
|
public static function merge(ShortDescription $shortDescription, LongDescription $longDescription) |
60
|
|
|
{ |
61
|
|
|
$shortAsString = $shortDescription->toNative(); |
62
|
|
|
$longAsString = $longDescription->toNative(); |
63
|
|
|
|
64
|
|
|
$shortAsStringWithoutEllipsis = rtrim($shortAsString, '. '); |
65
|
|
|
|
66
|
|
|
$longFormattedAsUdb2Short = self::getShortDescriptionUDB2FormattingFilter()->filter($longAsString); |
67
|
|
|
$longFormattedAsUdb3Short = self::getShortDescriptionUDB3FormattingFilter()->filter($longAsString); |
68
|
|
|
|
69
|
|
|
$udb2Comparison = strncmp( |
70
|
|
|
$longFormattedAsUdb2Short, |
71
|
|
|
$shortAsStringWithoutEllipsis, |
72
|
|
|
mb_strlen($shortAsStringWithoutEllipsis) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$udb3Comparison = strncmp( |
76
|
|
|
$longFormattedAsUdb3Short, |
77
|
|
|
$shortAsStringWithoutEllipsis, |
78
|
|
|
mb_strlen($shortAsStringWithoutEllipsis) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$shortIncludedInLong = $udb2Comparison === 0 || $udb3Comparison === 0; |
82
|
|
|
|
83
|
|
|
if ($shortIncludedInLong) { |
84
|
|
|
return new MergedDescription($longAsString); |
85
|
|
|
} else { |
86
|
|
|
return new MergedDescription($shortAsString . PHP_EOL . PHP_EOL . $longAsString); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return StringFilterInterface |
92
|
|
|
*/ |
93
|
|
|
private static function getShortDescriptionUDB2FormattingFilter() |
94
|
|
|
{ |
95
|
|
|
if (!isset(self::$shortDescriptionUDB2FormattingFilter)) { |
96
|
|
|
self::$shortDescriptionUDB2FormattingFilter = new ShortDescriptionUDB2FormattingFilter(); |
97
|
|
|
} |
98
|
|
|
return self::$shortDescriptionUDB2FormattingFilter; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return StringFilterInterface |
103
|
|
|
*/ |
104
|
|
|
private static function getShortDescriptionUDB3FormattingFilter() |
105
|
|
|
{ |
106
|
|
|
if (!isset(self::$shortDescriptionUDB3FormattingFilter)) { |
107
|
|
|
self::$shortDescriptionUDB3FormattingFilter = new ShortDescriptionUDB3FormattingFilter(); |
108
|
|
|
} |
109
|
|
|
return self::$shortDescriptionUDB3FormattingFilter; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.