1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Ntentan Framework |
4
|
|
|
* Copyright (c) 2008-2015 James Ekow Abaka Ainooson |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
7
|
|
|
* a copy of this software and associated documentation files (the |
8
|
|
|
* "Software"), to deal in the Software without restriction, including |
9
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
10
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
11
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
12
|
|
|
* the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be |
15
|
|
|
* included in all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\honam\engines\php\helpers\feed\generators; |
28
|
|
|
|
29
|
|
|
use ntentan\honam\engines\php\helpers\feed\Generator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* RSS Feed generator for the Feed helper. |
33
|
|
|
*/ |
34
|
|
|
class RssFeed extends Generator |
35
|
|
|
{ |
36
|
|
|
public function generate() |
37
|
|
|
{ |
38
|
|
|
$rss = new \SimpleXMLElement('<rss></rss>'); |
39
|
|
|
$rss['version'] = '2.0'; |
40
|
|
|
$rss->addChild('channel'); |
41
|
|
|
foreach($this->properties as $property => $value) |
42
|
|
|
{ |
43
|
|
|
$property = $this->convertProperty($property); |
44
|
|
|
$rss->channel->addChild($property, $this->formatProperty($property, $value)); |
45
|
|
|
} |
46
|
|
|
foreach($this->items as $item) |
47
|
|
|
{ |
48
|
|
|
$itemElement = $rss->channel->addChild('item'); |
49
|
|
|
foreach($item as $field => $value) |
50
|
|
|
{ |
51
|
|
|
$value = utf8_encode($value); |
52
|
|
|
$property = $this->convertProperty($field); |
53
|
|
|
$itemElement->addChild($property, $this->formatProperty($property, $value)); |
54
|
|
|
} |
55
|
|
|
if(!isset($item['id'])) |
56
|
|
|
{ |
57
|
|
|
$this->getGUID($itemElement, $item); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return $rss->asXML(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function getGUID($itemElement, $item) |
64
|
|
|
{ |
65
|
|
|
if(isset($item['url'])) |
66
|
|
|
{ |
67
|
|
|
$itemElement->addChild('guid', $item['url']); |
68
|
|
|
$itemElement->guid['isPermaLink'] = 'true'; |
69
|
|
|
} |
70
|
|
|
else |
71
|
|
|
{ |
72
|
|
|
$itemElement->addChild('guid', md5(serialize($item))); |
73
|
|
|
$itemElement->guid['isPermaLink'] = 'false'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Convert generic feed properties to the ones specified in the RSS |
79
|
|
|
* standard. |
80
|
|
|
* |
81
|
|
|
* @param string $property |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function convertProperty($property) |
85
|
|
|
{ |
86
|
|
|
$properties = array( |
87
|
|
|
'title' => 'title', |
88
|
|
|
'url' => 'link', |
89
|
|
|
'updated' => 'lastBuildDate', |
90
|
|
|
'summary' => 'description', |
91
|
|
|
'author' => 'author', |
92
|
|
|
'category' => 'category', |
93
|
|
|
'date' => 'pubDate', |
94
|
|
|
'id' => 'guid', |
95
|
|
|
'description' => 'description' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $properties[$property]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function formatProperty($property, $value) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
switch($property) |
104
|
|
|
{ |
105
|
|
|
case 'lastBuildDate': |
106
|
|
|
case 'pubDate': |
107
|
|
|
return date("r", strtotime($value)); |
108
|
|
|
default: |
109
|
|
|
return $value; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.