1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Feed; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
use Spatie\Feed\Exceptions\InvalidFeedItem; |
8
|
|
|
|
9
|
|
|
class FeedItem |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
protected $id; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $title; |
16
|
|
|
|
17
|
|
|
/** @var \Carbon\Carbon */ |
18
|
|
|
protected $updated; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $summary; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $link; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $enclosure; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
protected $enclosureLength; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $enclosureType; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
protected $author; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
protected $category; |
40
|
|
|
|
41
|
|
|
public function __construct(array $data = []) |
42
|
|
|
{ |
43
|
|
|
foreach ($data as $key => $value) { |
44
|
|
|
$this->$key = $value; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function create(array $data = []) |
49
|
|
|
{ |
50
|
|
|
return new static($data); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function id(string $id) |
54
|
|
|
{ |
55
|
|
|
$this->id = $id; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function title(string $title) |
61
|
|
|
{ |
62
|
|
|
$this->title = $title; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function updated(Carbon $updated) |
68
|
|
|
{ |
69
|
|
|
$this->updated = $updated; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function summary(string $summary) |
75
|
|
|
{ |
76
|
|
|
$this->summary = $summary; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function link(string $link) |
82
|
|
|
{ |
83
|
|
|
$this->link = $link; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function enclosure(string $enclosure) |
89
|
|
|
{ |
90
|
|
|
$this->enclosure = $enclosure; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function enclosureLength(int $enclosureLength) |
96
|
|
|
{ |
97
|
|
|
$this->enclosureLength = $enclosureLength; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function enclosureType(string $enclosureType) |
103
|
|
|
{ |
104
|
|
|
$this->enclosureType = $enclosureType; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function author(string $author) |
110
|
|
|
{ |
111
|
|
|
$this->author = $author; |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function category(string $category) |
117
|
|
|
{ |
118
|
|
|
$this->category = $category; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function validate() |
124
|
|
|
{ |
125
|
|
|
$requiredFields = ['id', 'title', 'updated', 'summary', 'link', 'author']; |
126
|
|
|
|
127
|
|
|
foreach ($requiredFields as $requiredField) { |
128
|
|
|
if (is_null($this->$requiredField)) { |
129
|
|
|
throw InvalidFeedItem::missingField($this, $requiredField); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function __get($key) |
135
|
|
|
{ |
136
|
|
|
if (! isset($this->$key)) { |
137
|
|
|
throw new Exception("Property `{$key}` doesn't exist"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->$key; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function __isset($key) |
144
|
|
|
{ |
145
|
|
|
return isset($this->$key); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|