1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\Feed\Exceptions\InvalidConfiguration; |
8
|
|
|
|
9
|
|
|
class Feed |
10
|
|
|
{ |
11
|
|
|
/** @var array */ |
12
|
|
|
protected $feedConfiguration; |
13
|
|
|
|
14
|
|
|
public function __construct(array $feedConfiguration) |
15
|
|
|
{ |
16
|
|
|
$this->feedConfiguration = $feedConfiguration; |
17
|
|
|
if (! str_contains($this->getFeedMethod(), '@')) { |
18
|
|
|
throw InvalidConfiguration::delimiterNotPresent($this->getFeedMethod()); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getFeedResponse() |
23
|
|
|
{ |
24
|
|
|
return response($this->getFeedContent(), 200, ['Content-Type' => 'application/xml;charset=UTF-8']); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getFeedContent() |
28
|
|
|
{ |
29
|
|
|
list($class, $method) = explode('@', $this->getFeedMethod()); |
30
|
|
|
|
31
|
|
|
$items = app($class)->{$method}($this->getFeedArguments()); |
32
|
|
|
|
33
|
|
|
$meta = ['id' => url($this->feedConfiguration['url']), 'link' => url($this->feedConfiguration['url']), 'title' => $this->feedConfiguration['title'], 'updated' => $this->getLastUpdatedDate($items)]; |
34
|
|
|
|
35
|
|
|
return view('laravel-feed::feed', compact('meta', 'items'))->render(); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function getFeedMethod() |
39
|
|
|
{ |
40
|
|
|
return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][0] : $this->feedConfiguration['items']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getFeedArguments() |
44
|
|
|
{ |
45
|
|
|
return is_array($this->feedConfiguration['items']) ? $this->feedConfiguration['items'][1] : null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getLastUpdatedDate(Collection $items) |
49
|
|
|
{ |
50
|
|
|
if (! count($items)) { |
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$lastItem = $items->sortBy(function (FeedItem $feedItem) { |
55
|
|
|
return $feedItem->getFeedItemUpdated()->format('YmdHis'); |
56
|
|
|
})->last(); |
57
|
|
|
|
58
|
|
|
return $lastItem->getFeedItemUpdated()->toAtomString(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: