Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

RssFeed::generate()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 10
nop 0
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 5
rs 9.4555
c 0
b 0
f 0
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
use SimpleXMLElement;
31
32
/**
33
 * RSS Feed generator for the Feed helper.
34
 */
35
class RssFeed extends Generator
36
{
37 3
    public function generate() : string
38
    {
39 3
        $rss = new SimpleXMLElement('<rss></rss>');
40 3
        $rss['version'] = '2.0';
41 3
        $rss->addChild('channel');
42 3
        foreach($this->properties as $property => $value)
43
        {
44 3
            $property = $this->convertProperty($property);
45 3
            $rss->channel->addChild($property, $this->formatProperty($property, $value));
46
        }
47 3
        foreach($this->items as $item)
48
        {
49 3
            $itemElement = $rss->channel->addChild('item');
50 3
            foreach($item as $field => $value)
51
            {
52 3
                $value = utf8_encode($value);
53 3
                $property = $this->convertProperty($field);
54 3
                $itemElement->addChild($property, $this->formatProperty($property, $value));
55
            }
56 3
            if(!isset($item['id']))
57
            {
58 3
                $this->getGUID($itemElement, $item);
59
            }
60
        }
61 3
        return $rss->asXML();
62
    }
63
    
64 3
    private function getGUID($itemElement, $item)
65
    {
66 3
        if(isset($item['url']))
67
        {
68 2
            $itemElement->addChild('guid', $item['url']);
69 2
            $itemElement->guid['isPermaLink'] = 'true';
70
        }
71
        else
72
        {
73 1
            $itemElement->addChild('guid', md5(serialize($item)));
74 1
            $itemElement->guid['isPermaLink'] = 'false';
75
        }        
76 3
    }
77
    
78
    /**
79
     * Convert generic feed properties to the ones specified in the RSS 
80
     * standard.
81
     * 
82
     * @param string $property
83
     * @return string
84
     */
85 3
    private function convertProperty($property)
86
    {
87
        $properties = array(
88 3
            'title' => 'title',
89
            'url' => 'link',
90
            'updated' => 'lastBuildDate',
91
            'summary' => 'description',
92
            'author' => 'author',
93
            'category' => 'category',
94
            'date' => 'pubDate',
95
            'id' => 'guid',
96
            'description' => 'description'
97
        );
98
        
99 3
        return $properties[$property];
100
    }
101
102
    /**
103
     * @param $property
104
     * @param $value
105
     * @return string
106
     */
107 3
    private function formatProperty($property, $value)
108
    {
109 3
        switch($property)
110
        {
111 3
            case 'lastBuildDate':
112 3
            case 'pubDate':
113 3
                return date("r", strtotime($value));
114
            default:
115 3
                return $value;
116
        }
117
    }
118
}
119