1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace sokolnikov911\YandexTurboPages; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Item |
7
|
|
|
* @package sokolnikov911\YandexTurboPages |
8
|
|
|
*/ |
9
|
|
|
class Item implements ItemInterface |
10
|
|
|
{ |
11
|
|
|
/** @var boolean */ |
12
|
|
|
protected $turbo; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $title; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
protected $link; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $category; |
22
|
|
|
|
23
|
|
|
/** @var int */ |
24
|
|
|
protected $pubDate; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $author; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
protected $fullText; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $turboSource; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
protected $turboTopic; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
protected $turboContent; |
40
|
|
|
|
41
|
|
|
/** @var RelatedItemsListInterface */ |
42
|
|
|
protected $relatedItemsList; |
43
|
|
|
|
44
|
140 |
|
public function __construct($turbo = true) |
45
|
|
|
{ |
46
|
140 |
|
$this->turbo = $turbo; |
47
|
140 |
|
} |
48
|
|
|
|
49
|
42 |
|
public function title($title) |
50
|
|
|
{ |
51
|
42 |
|
$title = (mb_strlen($title) > 240) ? mb_substr($title, 0, 239) . '…' : $title; |
52
|
42 |
|
$this->title = $title; |
53
|
42 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
35 |
|
public function link($link) |
57
|
|
|
{ |
58
|
35 |
|
$this->link = $link; |
59
|
35 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
35 |
|
public function category($category) |
63
|
|
|
{ |
64
|
35 |
|
$this->category = $category; |
65
|
35 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
42 |
|
public function pubDate($pubDate) |
69
|
|
|
{ |
70
|
42 |
|
$this->pubDate = $pubDate; |
71
|
42 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
14 |
|
public function turboSource($turboSource) |
75
|
|
|
{ |
76
|
14 |
|
$this->turboSource = $turboSource; |
77
|
14 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
14 |
|
public function turboTopic($turboTopic) |
81
|
|
|
{ |
82
|
14 |
|
$this->turboTopic = $turboTopic; |
83
|
14 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
42 |
|
public function turboContent($turboContent) |
87
|
|
|
{ |
88
|
42 |
|
$this->turboContent = $turboContent; |
89
|
42 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
35 |
|
public function author($author) |
93
|
|
|
{ |
94
|
35 |
|
$this->author = $author; |
95
|
35 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
14 |
|
public function fullText($fullText) |
99
|
|
|
{ |
100
|
14 |
|
$this->fullText = $fullText; |
101
|
14 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
7 |
|
public function appendTo(ChannelInterface $channel) |
105
|
|
|
{ |
106
|
7 |
|
$channel->addItem($this); |
107
|
7 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
21 |
|
public function addRelatedItemsList(RelatedItemsListInterface $relatedItemsList) |
111
|
|
|
{ |
112
|
21 |
|
$this->relatedItemsList = $relatedItemsList; |
113
|
21 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
35 |
|
public function asXML() |
117
|
|
|
{ |
118
|
35 |
|
$isTurboEnabled = $this->turbo ? 'true' : 'false'; |
119
|
|
|
|
120
|
35 |
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item turbo="' . $isTurboEnabled |
121
|
35 |
|
. '"></item>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); |
122
|
|
|
|
123
|
35 |
|
$xml->addChild('title', $this->title); |
124
|
35 |
|
$xml->addChild('link', $this->link); |
125
|
35 |
|
$xml->addCdataChild('turbo:content', $this->turboContent, 'http://turbo.yandex.ru'); |
126
|
35 |
|
$xml->addChildWithValueChecking('pubDate', date(DATE_RSS, $this->pubDate)); |
127
|
35 |
|
$xml->addChildWithValueChecking('category', $this->category); |
128
|
35 |
|
$xml->addChildWithValueChecking('author', $this->author); |
129
|
35 |
|
$xml->addChildWithValueChecking('yandex:full-text', $this->fullText, 'http://news.yandex.ru'); |
130
|
35 |
|
$xml->addChildWithValueChecking('turbo:topic', $this->turboTopic, 'http://turbo.yandex.ru'); |
131
|
35 |
|
$xml->addChildWithValueChecking('turbo:source', $this->turboSource, 'http://turbo.yandex.ru'); |
132
|
|
|
|
133
|
35 |
|
if ($this->relatedItemsList) { |
134
|
7 |
|
$toDom = dom_import_simplexml($xml); |
135
|
7 |
|
$fromDom = dom_import_simplexml($this->relatedItemsList->asXML()); |
136
|
7 |
|
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
137
|
3 |
|
} |
138
|
|
|
|
139
|
35 |
|
return $xml; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|