-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathFeedItem.php
172 lines (122 loc) · 3.25 KB
/
FeedItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Spatie\Feed;
use Carbon\CarbonInterface;
use Exception;
use Spatie\Feed\Exceptions\InvalidFeedItem;
class FeedItem
{
public ?Feed $feed = null;
protected ?string $id = null;
protected string $title;
protected CarbonInterface $updated;
protected string $summary;
protected string $link;
protected string $enclosure;
protected string $image;
protected int $enclosureLength;
protected string $enclosureType;
protected string $authorName;
protected string $authorEmail;
protected array $category = [];
public function __construct(array $data = [])
{
foreach ($data as $key => $value) {
if ($key === 'category') {
$this->category = (array) $value;
continue;
}
$this->$key = $value;
}
}
public static function create(array $data = []): static
{
return new static($data);
}
public function id(string | int $id): self
{
$this->id = (string) $id;
return $this;
}
public function title(string $title): self
{
$this->title = $title;
return $this;
}
public function updated(CarbonInterface $updated): self
{
$this->updated = $updated;
return $this;
}
public function summary(string $summary): self
{
$this->summary = $summary;
return $this;
}
public function link(string $link): self
{
$this->link = $link;
return $this;
}
public function enclosure(string $enclosure): self
{
$this->enclosure = $enclosure;
return $this;
}
public function enclosureLength(int $enclosureLength): self
{
$this->enclosureLength = $enclosureLength;
return $this;
}
public function enclosureType(string $enclosureType): self
{
$this->enclosureType = $enclosureType;
return $this;
}
public function image(string $image): self
{
$this->image = $image;
return $this;
}
public function authorName(string $authorName): self
{
$this->authorName = $authorName;
return $this;
}
public function authorEmail(string $authorEmail): self
{
$this->authorEmail = $authorEmail;
return $this;
}
public function category(string ...$category): self
{
$this->category = $category;
return $this;
}
public function timestamp(): string
{
if ($this->feed->format() === 'rss') {
return $this->updated->toRssString();
}
return $this->updated->toRfc3339String();
}
public function validate(): void
{
$requiredFields = ['id', 'title', 'updated', 'summary', 'link', 'authorName'];
foreach ($requiredFields as $requiredField) {
if (is_null($this->$requiredField)) {
throw InvalidFeedItem::missingField($this, $requiredField);
}
}
}
public function __get($key)
{
if (! isset($this->$key)) {
throw new Exception("Property `{$key}` doesn't exist");
}
return $this->$key;
}
public function __isset($key)
{
return isset($this->$key);
}
}