|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
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
|
|
|
const SORT_KEY_UPDATED = 'updated'; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $title; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
protected $url; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
protected $view; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \Illuminate\Support\Collection */ |
|
25
|
|
|
protected $items; |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $sortKey; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct($title, $url, $resolver, $view, string $sortKey = self::SORT_KEY_UPDATED) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->title = $title; |
|
32
|
|
|
$this->url = $url; |
|
33
|
|
|
$this->view = $view; |
|
34
|
|
|
$this->sortKey = $sortKey; |
|
35
|
|
|
|
|
36
|
|
|
$this->items = $this->resolveItems($resolver); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function toResponse($request): Response |
|
40
|
|
|
{ |
|
41
|
|
|
$meta = [ |
|
42
|
|
|
'id' => url($this->url), |
|
43
|
|
|
'link' => url($this->url), |
|
44
|
|
|
'title' => $this->title, |
|
45
|
|
|
'updated' => $this->lastUpdated(), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$contents = view($this->view, [ |
|
49
|
|
|
'meta' => $meta, |
|
50
|
|
|
'items' => $this->sortItemsDescBy($this->sortKey), |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
return new Response($contents, 200, [ |
|
|
|
|
|
|
54
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8', |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function resolveItems($resolver): Collection |
|
59
|
|
|
{ |
|
60
|
|
|
$resolver = Arr::wrap($resolver); |
|
61
|
|
|
|
|
62
|
|
|
$items = app()->call( |
|
63
|
|
|
array_shift($resolver), $resolver |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
return collect($items)->map(function ($feedable) { |
|
67
|
|
|
return $this->castToFeedItem($feedable); |
|
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 sortItemsDescBy(string $sortKey): Collection |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->items->sortByDesc(function (FeedItem $feedItem) use ($sortKey) { |
|
101
|
|
|
return $feedItem->$sortKey; |
|
102
|
|
|
}); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function lastUpdated(): string |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->items->isEmpty()) { |
|
108
|
|
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->sortItemsDescBy(self::SORT_KEY_UPDATED)->first()->updated->toAtomString(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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: