1<?php
2/**
3 * Portions Copyright 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
4 * Copyright 2007-2016 Horde LLC (http://www.horde.org/)
5 *
6 * @category Horde
7 * @package  Feed
8 */
9
10/**
11 * Atom feed class
12 *
13 * The Horde_Feed_Atom class is a concrete subclass of the general
14 * Horde_Feed_Base class, tailored for representing an Atom feed. It shares all
15 * of the same methods with its parent. The distinction is made in the format of
16 * data that Horde_Feed_Atom expects, and as a further pointer for users as to
17 * what kind of feed object they have been passed.
18 *
19 * @category Horde
20 * @package  Feed
21 */
22class Horde_Feed_Atom extends Horde_Feed_Base
23{
24    /**
25     * The classname for individual feed elements.
26     *
27     * @var string
28     */
29    protected $_listItemClassName = 'Horde_Feed_Entry_Atom';
30
31    /**
32     * The default namespace for Atom feeds.
33     *
34     * @var string
35     */
36    protected $_defaultNamespace = 'atom';
37
38    /**
39     * The XML string for an "empty" Atom feed.
40     *
41     * @var string
42     */
43    protected $_emptyXml = '<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"></feed>';
44
45    /**
46     * Cache the individual feed elements so they don't need to be
47     * searched for on every operation.
48     * @return array
49     */
50    protected function _buildListItemCache()
51    {
52        $entries = array();
53        foreach ($this->_element->childNodes as $child) {
54            if ($child->localName == 'entry') {
55                $entries[] = $child;
56            }
57        }
58
59        return $entries;
60    }
61
62    /**
63     * Easy access to <link> tags keyed by "rel" attributes.
64     * @TODO rationalize this with other __get/__call access
65     *
66     * If $elt->link() is called with no arguments, we will attempt to return
67     * the value of the <link> tag(s) like all other method-syntax attribute
68     * access. If an argument is passed to link(), however, then we will return
69     * the "href" value of the first <link> tag that has a "rel" attribute
70     * matching $rel:
71     *
72     * $elt->link(): returns the value of the link tag.
73     * $elt->link('self'): returns the href from the first <link rel="self"> in the entry.
74     *
75     * @param string $rel The "rel" attribute to look for.
76     * @return mixed
77     */
78    public function link($rel = null)
79    {
80        if ($rel === null) {
81            return parent::__call('link', null);
82        }
83
84        // Index link tags by their "rel" attribute.
85        $links = parent::__get('link');
86        if (!is_array($links)) {
87            if ($links instanceof Horde_Xml_Element) {
88                $links = array($links);
89            } else {
90                return $links;
91            }
92        }
93
94        foreach ($links as $link) {
95            if (empty($link['rel'])) {
96                continue;
97            }
98            if ($rel == $link['rel']) {
99                return $link['href'];
100            }
101        }
102
103        return null;
104    }
105}
106