Completed
Pull Request — master (#117)
by
unknown
01:10
created

FeedItem::image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
     /** @var string */
42
    protected $imageUrl;
43
44
    /** @var int */
45
    protected $imageSize;
46
47
    public function __construct(array $data = [])
48
    {
49
        foreach ($data as $key => $value) {
50
            $this->$key = $value;
51
        }
52
    }
53
54
    public static function create(array $data = [])
55
    {
56
        return new static($data);
57
    }
58
59
    public function id(string $id)
60
    {
61
        $this->id = $id;
62
63
        return $this;
64
    }
65
66
    public function title(string $title)
67
    {
68
        $this->title = $title;
69
70
        return $this;
71
    }
72
73
    public function updated(Carbon $updated)
74
    {
75
        $this->updated = $updated;
76
77
        return $this;
78
    }
79
80
    public function summary(string $summary)
81
    {
82
        $this->summary = $summary;
83
84
        return $this;
85
    }
86
87
    public function link(string $link)
88
    {
89
        $this->link = $link;
90
91
        return $this;
92
    }
93
94
    public function enclosure(string $enclosure)
95
    {
96
        $this->enclosure = $enclosure;
97
98
        return $this;
99
    }
100
101
    public function enclosureLength(int $enclosureLength)
102
    {
103
        $this->enclosureLength = $enclosureLength;
104
105
        return $this;
106
    }
107
108
    public function enclosureType(string $enclosureType)
109
    {
110
        $this->enclosureType = $enclosureType;
111
112
        return $this;
113
    }
114
115
    public function author(string $author)
116
    {
117
        $this->author = $author;
118
119
        return $this;
120
    }
121
122
    public function category(string $category)
123
    {
124
        $this->category = $category;
125
126
        return $this;
127
    }
128
129
    public function image(string $imageUrl, int $imageSize)
130
    {
131
        $this->imageUrl = $imageUrl;
132
        $this->imageSize = $imageSize;
133
134
        return $this;
135
    }
136
137
    public function validate()
138
    {
139
        $requiredFields = ['id', 'title', 'updated', 'summary', 'link', 'author'];
140
141
        foreach ($requiredFields as $requiredField) {
142
            if (is_null($this->$requiredField)) {
143
                throw InvalidFeedItem::missingField($this, $requiredField);
144
            }
145
        }
146
    }
147
148
    public function __get($key)
149
    {
150
        if (! isset($this->$key)) {
151
            throw new Exception("Property `{$key}` doesn't exist");
152
        }
153
154
        return $this->$key;
155
    }
156
157
    public function __isset($key)
158
    {
159
        return isset($this->$key);
160
    }
161
}
162