1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response as HttpResponse; |
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
|
|
|
|
18
|
|
|
if (!str_contains($feedConfiguration['items'], '@')) { |
19
|
|
|
throw InvalidConfiguration::delimiterNotPresent($feedConfiguration['items']); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getFeedResponse() : HttpResponse |
24
|
|
|
{ |
25
|
|
|
$feedContent = $this->getFeedContent($this->feedConfiguration); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return response($feedContent, 200, ['Content-Type' => 'application/atom+xml; chartset=UTF-8']); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getFeedContent() : string |
31
|
|
|
{ |
32
|
|
|
list($class, $method) = explode('@', $this->feedConfiguration['items']); |
33
|
|
|
|
34
|
|
|
$items = app($class)->$method(); |
35
|
|
|
|
36
|
|
|
$meta = [ |
37
|
|
|
'link' => $this->feedConfiguration['url'], |
38
|
|
|
'description' => $this->feedConfiguration['description'], |
39
|
|
|
'title' => $this->feedConfiguration['title'], |
40
|
|
|
'updated' => $this->getLastUpdatedDate($items, 'Y-m-d H:i:s'), |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
return view('laravel-feed::feed', compact('meta', 'items'))->render(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getLastUpdatedDate(Collection $items, string $format) : string |
47
|
|
|
{ |
48
|
|
|
if (!count($items)) { |
49
|
|
|
return ''; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$lastItem = $items |
53
|
|
|
->sortBy(function (FeedItem $feedItem) { |
54
|
|
|
return $feedItem->getFeedItemUpdated()->format('YmdHis'); |
55
|
|
|
}) |
56
|
|
|
->last(); |
57
|
|
|
|
58
|
|
|
return $lastItem->getFeedItemUpdated()->format($format); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.