1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Spatie\Feed\Exceptions\InvalidFeedItem; |
9
|
|
|
use Illuminate\Contracts\Support\Responsable; |
10
|
|
|
|
11
|
|
|
class Feed implements Responsable |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $title; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $description; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $language; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $url; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $view; |
27
|
|
|
|
28
|
|
|
/** @var \Illuminate\Support\Collection */ |
29
|
|
|
protected $feedItems; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
string $title, |
33
|
|
|
Collection $items, |
34
|
|
|
string $url = '', |
35
|
|
|
string $view = 'feed::feed', |
36
|
|
|
string $description = '', |
37
|
|
|
string $language = '' |
38
|
|
|
) { |
39
|
|
|
$this->title = $title; |
40
|
|
|
$this->description = $description; |
41
|
|
|
$this->language = $language; |
42
|
|
|
$this->url = $url ?? request()->url(); |
43
|
|
|
$this->view = $view; |
44
|
|
|
|
45
|
|
|
$this->feedItems = $items->map(function($feedable) { |
46
|
|
|
return $this->castToFeedItem($feedable); |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function toResponse($request): Response |
51
|
|
|
{ |
52
|
|
|
$meta = [ |
53
|
|
|
'id' => url($this->url), |
54
|
|
|
'link' => url($this->url), |
55
|
|
|
'title' => $this->title, |
56
|
|
|
'description' => $this->description, |
57
|
|
|
'language' => $this->language, |
58
|
|
|
'updated' => $this->lastUpdated(), |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$contents = view($this->view, [ |
62
|
|
|
'meta' => $meta, |
63
|
|
|
'items' => $this->feedItems, |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
return new Response($contents, 200, [ |
|
|
|
|
67
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8', |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function castToFeedItem($feedable): FeedItem |
72
|
|
|
{ |
73
|
|
|
if (is_array($feedable)) { |
74
|
|
|
$feedable = new FeedItem($feedable); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($feedable instanceof FeedItem) { |
78
|
|
|
$feedable->validate(); |
79
|
|
|
|
80
|
|
|
return $feedable; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (! $feedable instanceof Feedable) { |
84
|
|
|
throw InvalidFeedItem::notFeedable($feedable); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$feedItem = $feedable->toFeedItem(); |
88
|
|
|
|
89
|
|
|
if (! $feedItem instanceof FeedItem) { |
90
|
|
|
throw InvalidFeedItem::notAFeedItem($feedItem); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$feedItem->validate(); |
94
|
|
|
|
95
|
|
|
return $feedItem; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function lastUpdated(): string |
99
|
|
|
{ |
100
|
|
|
if ($this->feedItems->isEmpty()) { |
101
|
|
|
return ''; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->feedItems->sortBy(function ($feedItem) { |
105
|
|
|
return $feedItem->updated; |
106
|
|
|
})->last()->updated->toRssString(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
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: