1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 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 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Hydrator; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
20
|
|
|
use SWP\Bundle\ContentBundle\Provider\RouteProviderInterface; |
21
|
|
|
use SWP\Component\Bridge\Model\ItemInterface; |
22
|
|
|
use SWP\Component\Bridge\Model\PackageInterface; |
23
|
|
|
|
24
|
|
|
final class ArticleHydrator implements ArticleHydratorInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RouteProviderInterface |
28
|
|
|
*/ |
29
|
|
|
private $routeProvider; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $allowedTypes = [ |
35
|
|
|
ItemInterface::TYPE_PICTURE, |
36
|
|
|
ItemInterface::TYPE_FILE, |
37
|
|
|
ItemInterface::TYPE_TEXT, |
38
|
|
|
ItemInterface::TYPE_COMPOSITE, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ArticleHydrator constructor. |
43
|
|
|
* |
44
|
|
|
* @param RouteProviderInterface $routeProvider |
45
|
|
|
*/ |
46
|
21 |
|
public function __construct(RouteProviderInterface $routeProvider) |
47
|
|
|
{ |
48
|
21 |
|
$this->routeProvider = $routeProvider; |
49
|
21 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
6 |
|
public function hydrate(ArticleInterface $article, PackageInterface $package): ArticleInterface |
55
|
|
|
{ |
56
|
6 |
|
$article->setBody($this->populateBody($package)); |
57
|
6 |
|
$article->setTitle($package->getHeadline()); |
58
|
6 |
|
if (null !== $package->getSlugline()) { |
59
|
6 |
|
$article->setSlug($package->getSlugline()); |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
$article->setLocale($package->getLanguage()); |
63
|
6 |
|
$article->setLead($this->populateLead($package)); |
64
|
6 |
|
$article->setMetadata($package->getMetadata()); |
65
|
|
|
// assign default route |
66
|
6 |
|
$article->setRoute($this->routeProvider->getRouteForArticle($article)); |
67
|
|
|
|
68
|
6 |
|
return $article; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param PackageInterface $package |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
6 |
|
protected function populateLead(PackageInterface $package) |
77
|
|
|
{ |
78
|
6 |
|
if (null === $package->getDescription() || '' === $package->getDescription()) { |
79
|
|
|
return trim($package->getDescription().implode('', array_map(function (ItemInterface $item) { |
80
|
3 |
|
$this->ensureTypeIsAllowed($item->getType()); |
81
|
|
|
|
82
|
3 |
|
return ' '.$item->getDescription(); |
83
|
3 |
|
}, $package->getItems()->toArray()))); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $package->getDescription(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param PackageInterface $package |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function populateBody(PackageInterface $package) |
95
|
|
|
{ |
96
|
6 |
|
return $package->getBody().' '.implode('', array_map(function (ItemInterface $item) { |
97
|
3 |
|
$this->ensureTypeIsAllowed($item->getType()); |
98
|
|
|
|
99
|
3 |
|
return $item->getBody(); |
100
|
6 |
|
}, $package->getItems()->toArray())); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $type |
105
|
|
|
* |
106
|
|
|
* @throws \InvalidArgumentException |
107
|
|
|
*/ |
108
|
3 |
|
protected function ensureTypeIsAllowed(string $type) |
109
|
|
|
{ |
110
|
3 |
|
if (!in_array($type, $this->allowedTypes)) { |
111
|
|
|
throw new \InvalidArgumentException(sprintf( |
112
|
|
|
'Item type "%s" is not supported. Supported types are: %s', |
113
|
|
|
$type, |
114
|
|
|
implode(', ', $this->allowedTypes) |
115
|
|
|
)); |
116
|
|
|
} |
117
|
3 |
|
} |
118
|
|
|
} |
119
|
|
|
|