|
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
|
80 |
|
public function __construct(bool $turbo = true) |
|
45
|
|
|
{ |
|
46
|
80 |
|
$this->turbo = $turbo; |
|
47
|
80 |
|
} |
|
48
|
|
|
|
|
49
|
24 |
|
public function title(string $title): ItemInterface |
|
50
|
|
|
{ |
|
51
|
24 |
|
$title = (mb_strlen($title) > 240) ? mb_substr($title, 0, 239) . '…' : $title; |
|
52
|
24 |
|
$this->title = $title; |
|
53
|
24 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
20 |
|
public function link(string $link): ItemInterface |
|
57
|
|
|
{ |
|
58
|
20 |
|
$this->link = $link; |
|
59
|
20 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
20 |
|
public function category(string $category): ItemInterface |
|
63
|
|
|
{ |
|
64
|
20 |
|
$this->category = $category; |
|
65
|
20 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
24 |
|
public function pubDate(int $pubDate): ItemInterface |
|
69
|
|
|
{ |
|
70
|
24 |
|
$this->pubDate = $pubDate; |
|
71
|
24 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
8 |
|
public function turboSource(string $turboSource): ItemInterface |
|
75
|
|
|
{ |
|
76
|
8 |
|
$this->turboSource = $turboSource; |
|
77
|
8 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
8 |
|
public function turboTopic(string $turboTopic): ItemInterface |
|
81
|
|
|
{ |
|
82
|
8 |
|
$this->turboTopic = $turboTopic; |
|
83
|
8 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
24 |
|
public function turboContent(string $turboContent): ItemInterface |
|
87
|
|
|
{ |
|
88
|
24 |
|
$this->turboContent = $turboContent; |
|
89
|
24 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
20 |
|
public function author(string $author): ItemInterface |
|
93
|
|
|
{ |
|
94
|
20 |
|
$this->author = $author; |
|
95
|
20 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
8 |
|
public function fullText(string $fullText): ItemInterface |
|
99
|
|
|
{ |
|
100
|
8 |
|
$this->fullText = $fullText; |
|
101
|
8 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
public function appendTo(ChannelInterface $channel): ItemInterface |
|
105
|
|
|
{ |
|
106
|
4 |
|
$channel->addItem($this); |
|
107
|
4 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
12 |
|
public function addRelatedItemsList(RelatedItemsListInterface $relatedItemsList): ItemInterface |
|
111
|
|
|
{ |
|
112
|
12 |
|
$this->relatedItemsList = $relatedItemsList; |
|
113
|
12 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
20 |
|
public function asXML(): SimpleXMLElement |
|
117
|
|
|
{ |
|
118
|
20 |
|
$isTurboEnabled = $this->turbo ? 'true' : 'false'; |
|
119
|
|
|
|
|
120
|
20 |
|
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><item turbo="' . $isTurboEnabled |
|
121
|
20 |
|
. '"></item>', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL); |
|
122
|
|
|
|
|
123
|
20 |
|
$xml->addChild('title', $this->title); |
|
124
|
20 |
|
$xml->addChild('link', $this->link); |
|
125
|
20 |
|
$xml->addCdataChild('turbo:content', $this->turboContent, 'http://turbo.yandex.ru'); |
|
126
|
|
|
|
|
127
|
20 |
|
$xml->addChildWithValueChecking('pubDate', date(DATE_RSS, $this->pubDate)); |
|
128
|
20 |
|
$xml->addChildWithValueChecking('category', $this->category); |
|
129
|
20 |
|
$xml->addChildWithValueChecking('author', $this->author); |
|
130
|
20 |
|
$xml->addChildWithValueChecking('yandex:full-text', $this->fullText, 'http://news.yandex.ru'); |
|
131
|
20 |
|
$xml->addChildWithValueChecking('turbo:topic', $this->turboTopic, 'http://turbo.yandex.ru'); |
|
132
|
20 |
|
$xml->addChildWithValueChecking('turbo:source', $this->turboSource, 'http://turbo.yandex.ru'); |
|
133
|
|
|
|
|
134
|
20 |
|
if ($this->relatedItemsList) { |
|
135
|
4 |
|
$toDom = dom_import_simplexml($xml); |
|
136
|
4 |
|
$fromDom = dom_import_simplexml($this->relatedItemsList->asXML()); |
|
137
|
4 |
|
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
20 |
|
return $xml; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|