Completed
Push — master ( 9038ff...55d9a6 )
by Sebastian
03:06 queued 11s
created

FeedItem::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 0
1
<?php
2
3
namespace Spatie\Feed;
4
5
use Exception;
6
use Carbon\Carbon;
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 @var string */
24
    protected $link;
25
26
    /** @var string */
27
    protected $author;
28
29
    public function __construct(array $data = [])
30
    {
31
        foreach ($data as $key => $value) {
32
            $this->$key = $value;
33
        }
34
    }
35
36
    public static function create(array $data = [])
37
    {
38
        return new static($data);
39
    }
40
41
    public function id(string $id)
42
    {
43
        $this->id = $id;
44
45
        return $this;
46
    }
47
48
    public function title(string $title)
49
    {
50
        $this->title = $title;
51
52
        return $this;
53
    }
54
55
    public function updated(Carbon $updated)
56
    {
57
        $this->updated = $updated;
58
59
        return $this;
60
    }
61
62
    public function summary(string $summary)
63
    {
64
        $this->summary = $summary;
65
66
        return $this;
67
    }
68
69
    public function link(string $link)
70
    {
71
        $this->link = $link;
72
73
        return $this;
74
    }
75
76
    public function author(string $author)
77
    {
78
        $this->author = $author;
79
80
        return $this;
81
    }
82
83
    public function validate()
84
    {
85
        $requiredFields = ['id', 'title', 'updated', 'summary', 'link', 'author'];
86
87
        foreach ($requiredFields as $requiredField) {
88
            if (is_null($this->$requiredField)) {
89
                throw InvalidFeedItem::missingField($this, $requiredField);
90
            }
91
        }
92
    }
93
94
    public function __get($key)
95
    {
96
        if (! isset($this->$key)) {
97
            throw new Exception("Property `{$key}` doesn't exist");
98
        }
99
100
        return $this->$key;
101
    }
102
}
103