-
Notifications
You must be signed in to change notification settings - Fork 25
/
RssView.php
166 lines (150 loc) · 4.12 KB
/
RssView.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
<?php
namespace Zelenin\yii\extensions\Rss;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\widgets\BaseListView;
use Zelenin\Feed;
class RssView extends BaseListView
{
/**
* @var Feed
*/
public $feed;
/**
* @var array
*/
public $channel;
/**
* @var array
*/
public $items;
/**
* @var array
*/
public $requiredChannelElements = ['title', 'link', 'description'];
/**
* @var array
*/
public $requiredItemElements = ['title', 'description', 'link', 'pubDate'];
/**
* @var array
*/
private $channelAttributes = [];
/**
* @var array
*/
private $itemAttributes = [];
/**
* @inheritdoc
*
* @throws InvalidConfigException
*/
public function init()
{
if ($this->dataProvider === null) {
throw new InvalidConfigException('The "dataProvider" property must be set');
}
$this->channelAttributes = $this->getAttributes($this->channel);
foreach ($this->requiredChannelElements as $channelElement) {
if (!in_array($channelElement, $this->channelAttributes)) {
throw new InvalidConfigException('Required channel attribute "' . $channelElement . '" must be set');
}
}
$this->itemAttributes = $this->getAttributes($this->items);
foreach ($this->requiredItemElements as $itemElement) {
if (!in_array($itemElement, $this->itemAttributes)) {
throw new InvalidConfigException('Required item attribute "' . $itemElement . '" must be set');
}
}
$this->feed = new Feed;
}
/**
* @inheritdoc
*
* @return string|Feed
*/
public function run()
{
$this->renderChannel();
if ($this->dataProvider->getCount() > 0) {
$this->renderItems();
}
return $this->feed;
}
/**
* @return Feed
*/
public function getFeed()
{
return $this->feed;
}
public function renderChannel()
{
$this->getFeed()->addChannel(ArrayHelper::getValue($this->channel, 'link'));
foreach ($this->channel as $element => $value) {
if (is_string($value)) {
$this->getFeed()->addChannelElement($element, $value);
} else {
$result = call_user_func($value, $this, $this->getFeed());
if (is_string($result)) {
$this->getFeed()->addChannelElement($element, $result);
}
}
}
}
/**
* @inheritdoc
*/
public function renderItems()
{
$models = $this->dataProvider->getModels();
foreach ($models as $model) {
$this->renderItem($model);
}
}
/**
* @param $model
*/
public function renderItem($model)
{
$this->getFeed()->addItem();
foreach ($this->items as $element => $value) {
if (is_string($value)) {
if (is_string($element)) {
$this->getFeed()->addItemElement($element, $value);
} else {
$this->getFeed()->addItemElement($value, ArrayHelper::getValue($model, $value));
}
} else {
$result = call_user_func($value, $model, $this, $this->getFeed());
if (is_string($result)) {
$this->getFeed()->addItemElement($element, $result);
}
}
}
}
/**
* @param $configArray
*
* @return array
*
* @throws InvalidConfigException
*/
private function getAttributes($configArray)
{
$attributes = [];
foreach ($configArray as $key => $value) {
if (is_string($key)) {
$attributes[] = $key;
} else {
if (is_string($value)) {
$attributes[] = $value;
} else {
throw new InvalidConfigException('Wrong configured attribute');
}
}
}
return $attributes;
}
}