summaryrefslogtreecommitdiff
path: root/vendor/miniflux/picofeed/lib/PicoFeed/Serialization/SubscriptionParser.php
blob: caff07c2d22c7c94dee7dacef91befb70be46e67 (plain)
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
<?php

namespace PicoFeed\Serialization;

use SimpleXMLElement;

/**
 * Class SubscriptionParser
 *
 * @package PicoFeed\Serialization
 * @author  Frederic Guillot
 */
class SubscriptionParser
{
    /**
     * @var Subscription
     */
    protected $subscription;

    /**
     * @var SimpleXMLElement
     */
    private $outlineElement;

    /**
     * @var SimpleXMLElement
     */
    private $parentElement;

    /**
     * Constructor
     *
     * @access public
     * @param  SimpleXMLElement  $parentElement
     * @param  SimpleXMLElement  $outlineElement
     */
    public function __construct(SimpleXMLElement $parentElement, SimpleXMLElement $outlineElement)
    {
        $this->parentElement = $parentElement;
        $this->outlineElement = $outlineElement;
        $this->subscription = new Subscription();
    }

    /**
     * Get object instance
     *
     * @static
     * @access public
     * @param  SimpleXMLElement $parentElement
     * @param  SimpleXMLElement $outlineElement
     * @return SubscriptionParser
     */
    public static function create(SimpleXMLElement $parentElement, SimpleXMLElement $outlineElement)
    {
        return new static($parentElement, $outlineElement);
    }

    /**
     * Parse subscription entry
     *
     * @access public
     * @return Subscription
     */
    public function parse()
    {
        $this->subscription->setCategory($this->findCategory());
        $this->subscription->setTitle($this->findTitle());
        $this->subscription->setFeedUrl($this->findFeedUrl());
        $this->subscription->setSiteUrl($this->findSiteUrl());
        $this->subscription->setType($this->findType());
        $this->subscription->setDescription($this->findDescription());

        return $this->subscription;
    }

    /**
     * Find category.
     *
     * @access protected
     * @return string
     */
    protected function findCategory()
    {
        return isset($this->parentElement['text']) ? (string) $this->parentElement['text'] : '';
    }

    /**
     * Find title.
     *
     * @access protected
     * @return string
     */
    protected function findTitle()
    {
        return isset($this->outlineElement['title']) ? (string) $this->outlineElement['title'] : (string) $this->outlineElement['text'];
    }

    /**
     * Find feed url.
     *
     * @access protected
     * @return string
     */
    protected function findFeedUrl()
    {
        return (string) $this->outlineElement['xmlUrl'];
    }

    /**
     * Find site url.
     *
     * @access protected
     * @return string
     */
    protected function findSiteUrl()
    {
        return isset($this->outlineElement['htmlUrl']) ? (string) $this->outlineElement['htmlUrl'] : $this->findFeedUrl();
    }

    /**
     * Find type.
     *
     * @access protected
     * @return string
     */
    protected function findType()
    {
        return isset($this->outlineElement['version']) ? (string) $this->outlineElement['version'] :
            isset($this->outlineElement['type']) ? (string) $this->outlineElement['type'] : 'rss';
    }

    /**
     * Find description.
     *
     * @access protected
     * @return string
     */
    protected function findDescription()
    {
        return isset($this->outlineElement['description']) ? (string) $this->outlineElement['description'] : $this->findTitle();
    }
}