Completed
Pull Request — master (#68)
by
unknown
08:34
created

FeedItem   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 114
rs 10
c 0
b 0
f 0

14 Methods

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