1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2020 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2020 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\CoreBundle\AppleNews\Converter; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Body; |
20
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\EmbedWebVideo; |
21
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\FacebookPost; |
22
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Figure; |
23
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Heading; |
24
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Instagram; |
25
|
|
|
use SWP\Bundle\CoreBundle\AppleNews\Component\Tweet; |
26
|
|
|
|
27
|
|
|
final class ArticleBodyToComponentsConverter |
28
|
|
|
{ |
29
|
|
|
public function convert(string $body): array |
30
|
|
|
{ |
31
|
|
|
$document = new \DOMDocument(); |
32
|
|
|
libxml_use_internal_errors(true); |
33
|
|
|
$document->loadHTML('<?xml encoding="UTF-8">'.self::stripHtmlTags($body)); |
34
|
|
|
$document->encoding = 'UTF-8'; |
35
|
|
|
libxml_clear_errors(); |
36
|
|
|
|
37
|
|
|
/** @var \DOMNodeList $body */ |
38
|
|
|
if (!($body = $document->getElementsByTagName('body')->item(0))) { |
39
|
|
|
throw new \InvalidArgumentException('Invalid HTML was provided'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$components = []; |
43
|
|
|
foreach ($body->childNodes as $node) { |
44
|
|
|
switch ($node->nodeName) { |
45
|
|
|
case 'h1': |
46
|
|
|
case 'h2': |
47
|
|
|
case 'h3': |
48
|
|
|
case 'h4': |
49
|
|
|
case 'h5': |
50
|
|
|
case 'h6': |
51
|
|
|
if ('' !== $node->textContent) { |
52
|
|
|
$level = substr($node->nodeName, 1); |
53
|
|
|
$components[] = new Heading($node->textContent, (int) $level); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
break; |
57
|
|
|
case 'p': |
58
|
|
|
if ('' !== $node->textContent) { |
59
|
|
|
$components[] = new Body($node->textContent); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
break; |
63
|
|
|
|
64
|
|
|
case 'figure': |
65
|
|
|
$src = $node->getElementsByTagName('img') |
66
|
|
|
->item(0) |
67
|
|
|
->getAttribute('src'); |
68
|
|
|
|
69
|
|
|
$caption = $node->getElementsByTagName('figcaption') |
70
|
|
|
->item(0) |
71
|
|
|
->textContent; |
72
|
|
|
|
73
|
|
|
$components[] = new Figure($src, $caption); |
74
|
|
|
|
75
|
|
|
break; |
76
|
|
|
case 'div': |
77
|
|
|
if (!$node->hasAttribute('class')) { |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$iframeElement = $node->getElementsByTagName('iframe') |
82
|
|
|
->item(0); |
83
|
|
|
|
84
|
|
|
if (null !== $iframeElement) { |
85
|
|
|
$webVideoUrl = $iframeElement->getAttribute('src'); |
86
|
|
|
$url = str_replace('\"', '', $webVideoUrl); |
87
|
|
|
if (false !== strpos($url, 'twitter.com')) { |
88
|
|
|
$components[] = new Tweet('https:'.$url); |
89
|
|
|
} elseif (false !== strpos($url, 'iframe.ly')) { |
90
|
|
|
$parsedUrl = parse_url($url); |
91
|
|
|
parse_str($parsedUrl['query'], $url); |
92
|
|
|
|
93
|
|
|
$components[] = new FacebookPost($url['url']); |
94
|
|
|
} elseif (false !== strpos($url, 'facebook.com')) { |
95
|
|
|
$parsedUrl = parse_url($url); |
96
|
|
|
parse_str($parsedUrl['query'], $url); |
97
|
|
|
|
98
|
|
|
$components[] = new FacebookPost($url['href']); |
99
|
|
|
} else { |
100
|
|
|
$components[] = new EmbedWebVideo($url); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
break; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$instagramElement = $node->getElementsByTagName('blockquote') |
107
|
|
|
->item(0); |
108
|
|
|
|
109
|
|
|
if (null !== $instagramElement) { |
110
|
|
|
$instagramUrl = $instagramElement->getAttribute('data-instgrm-permalink'); |
111
|
|
|
$url = str_replace('\"', '', $instagramUrl); |
112
|
|
|
$components[] = new Instagram($url); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $components; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function stripHtmlTags(string $html): string |
123
|
|
|
{ |
124
|
|
|
return preg_replace('/<script.*>.*<\/script>/isU', '', $html); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|