|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Responsable; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Spatie\Feed\Exceptions\InvalidFeedItem; |
|
9
|
|
|
|
|
10
|
|
|
class Feed implements Responsable |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
protected $title; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $description; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
protected $language; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $url; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $view; |
|
26
|
|
|
|
|
27
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
28
|
|
|
protected $feedItems; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
string $title, |
|
32
|
|
|
Collection $items, |
|
33
|
|
|
string $url = '', |
|
34
|
|
|
string $view = 'feed::feed', |
|
35
|
|
|
string $description = '', |
|
36
|
|
|
string $language = '' |
|
37
|
|
|
) { |
|
38
|
|
|
$this->title = $title; |
|
39
|
|
|
$this->description = $description; |
|
40
|
|
|
$this->language = $language; |
|
41
|
|
|
$this->url = $url ?? request()->url(); |
|
42
|
|
|
$this->view = $view; |
|
43
|
|
|
|
|
44
|
|
|
$this->feedItems = $items->map(function ($feedable) { |
|
45
|
|
|
return $this->castToFeedItem($feedable); |
|
46
|
|
|
}); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function toResponse($request): Response |
|
50
|
|
|
{ |
|
51
|
|
|
$meta = [ |
|
52
|
|
|
'id' => url($this->url), |
|
53
|
|
|
'link' => url($this->url), |
|
54
|
|
|
'title' => $this->title, |
|
55
|
|
|
'description' => $this->description, |
|
56
|
|
|
'language' => $this->language, |
|
57
|
|
|
'updated' => $this->lastUpdated(), |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$contents = view($this->view, [ |
|
61
|
|
|
'meta' => $meta, |
|
62
|
|
|
'items' => $this->feedItems, |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
return new Response($contents, 200, [ |
|
|
|
|
|
|
66
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8', |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function castToFeedItem($feedable): FeedItem |
|
71
|
|
|
{ |
|
72
|
|
|
if (is_array($feedable)) { |
|
73
|
|
|
$feedable = new FeedItem($feedable); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($feedable instanceof FeedItem) { |
|
77
|
|
|
$feedable->validate(); |
|
78
|
|
|
|
|
79
|
|
|
return $feedable; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (! $feedable instanceof Feedable) { |
|
83
|
|
|
throw InvalidFeedItem::notFeedable($feedable); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$feedItem = $feedable->toFeedItem(); |
|
87
|
|
|
|
|
88
|
|
|
if (! $feedItem instanceof FeedItem) { |
|
89
|
|
|
throw InvalidFeedItem::notAFeedItem($feedItem); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$feedItem->validate(); |
|
93
|
|
|
|
|
94
|
|
|
return $feedItem; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function lastUpdated(): string |
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->feedItems->isEmpty()) { |
|
100
|
|
|
return ''; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->feedItems->sortBy(function ($feedItem) { |
|
104
|
|
|
return $feedItem->updated; |
|
105
|
|
|
})->last()->updated->toRssString(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: