XML_Feed_Parser
[ class tree: XML_Feed_Parser ] [ index: XML_Feed_Parser ] [ all elements ]

Source for file AtomElement.php

Documentation is available at AtomElement.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * AtomElement class for XML_Feed_Parser package
  6.  *
  7.  * PHP versions 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   XML
  16.  * @package    XML_Feed_Parser
  17.  * @author     James Stewart <james@jystewart.net>
  18.  * @copyright  2005 James Stewart <james@jystewart.net>
  19.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU LGPL 2.1
  20.  * @version    CVS: $Id: AtomElement.php,v 1.19 2007/03/26 12:43:11 jystewart Exp $
  21.  * @link       http://pear.php.net/package/XML_Feed_Parser/
  22.  */
  23.  
  24. /**
  25.  * This class provides support for atom entries. It will usually be called by
  26.  * XML_Feed_Parser_Atom with which it shares many methods.
  27.  *
  28.  * @author    James Stewart <james@jystewart.net>
  29.  * @version    Release: 1.0.3
  30.  * @package XML_Feed_Parser
  31.  */
  32. {
  33.     /**
  34.      * This will be a reference to the parent object for when we want
  35.      * to use a 'fallback' rule
  36.      * @var XML_Feed_Parser_Atom 
  37.      */
  38.     protected $parent;
  39.  
  40.     /**
  41.      * When performing XPath queries we will use this prefix
  42.      * @var string 
  43.      */
  44.     private $xpathPrefix '';
  45.     
  46.     /**
  47.      * xml:base values inherited by the element
  48.      * @var string 
  49.      */
  50.     protected $xmlBase;
  51.  
  52.     /**
  53.      * Here we provide a few mappings for those very special circumstances in
  54.      * which it makes sense to map back to the RSS2 spec or to manage other
  55.      * compatibilities (eg. with the Univeral Feed Parser). Key is the other version's
  56.      * name for the command, value is an array consisting of the equivalent in our atom
  57.      * api and any attributes needed to make the mapping.
  58.      * @var array 
  59.      */
  60.     protected $compatMap = array(
  61.         'guid' => array('id'),
  62.         'links' => array('link'),
  63.         'tags' => array('category'),
  64.         'contributors' => array('contributor'));
  65.         
  66.     /**
  67.      * Our specific element map
  68.      * @var array 
  69.      */
  70.     protected $map = array(
  71.         'author' => array('Person''fallback'),
  72.         'contributor' => array('Person'),
  73.         'id' => array('Text''fail'),
  74.         'published' => array('Date'),
  75.         'updated' => array('Date''fail'),
  76.         'title' => array('Text''fail'),
  77.         'rights' => array('Text''fallback'),
  78.         'summary' => array('Text'),
  79.         'content' => array('Content'),
  80.         'link' => array('Link'),
  81.         'enclosure' => array('Enclosure'),
  82.         'category' => array('Category'));
  83.  
  84.     /**
  85.      * Store useful information for later.
  86.      *
  87.      * @param   DOMElement  $element - this item as a DOM element
  88.      * @param   XML_Feed_Parser_Atom    $parent - the feed of which this is a member
  89.      */
  90.     function __construct(DOMElement $element$parent$xmlBase '')
  91.     {
  92.         $this->model = $element;
  93.         $this->parent = $parent;
  94.         $this->xmlBase = $xmlBase;
  95.         $this->xpathPrefix "//atom:entry[atom:id='" $this->id "']/";
  96.         $this->xpath = $this->parent->xpath;
  97.     }
  98.  
  99.     /**
  100.      * Provides access to specific aspects of the author data for an atom entry
  101.      *
  102.      * Author data at the entry level is more complex than at the feed level.
  103.      * If atom:author is not present for the entry we need to look for it in
  104.      * an atom:source child of the atom:entry. If it's not there either, then
  105.      * we look to the parent for data.
  106.      *
  107.      * @param   array 
  108.      * @return  string 
  109.      */
  110.     function getAuthor($arguments)
  111.     {
  112.         /* Find out which part of the author data we're looking for */
  113.         if (isset($arguments['param'])) {
  114.             $parameter $arguments['param'];
  115.         else {
  116.             $parameter 'name';
  117.         }
  118.         
  119.         $test $this->model->getElementsByTagName('author');
  120.         if ($test->length > 0{
  121.             $item $test->item(0);
  122.             return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
  123.         }
  124.         
  125.         $source $this->model->getElementsByTagName('source');
  126.         if ($source->length > 0{
  127.             $test $this->model->getElementsByTagName('author');
  128.             if ($test->length > 0{
  129.                 $item $test->item(0);
  130.                 return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
  131.             }
  132.         }
  133.         return $this->parent->getAuthor($arguments);
  134.     }
  135.  
  136.     /**
  137.      * Returns the content of the content element or info on a specific attribute
  138.      *
  139.      * This element may or may not be present. It cannot be present more than
  140.      * once. It may have a 'src' attribute, in which case there's no content
  141.      * If not present, then the entry must have link with rel="alternate".
  142.      * If there is content we return it, if not and there's a 'src' attribute
  143.      * we return the value of that instead. The method can take an 'attribute'
  144.      * argument, in which case we return the value of that attribute if present.
  145.      * eg. $item->content("type") will return the type of the content. It is
  146.      * recommended that all users check the type before getting the content to
  147.      * ensure that their script is capable of handling the type of returned data.
  148.      * (data carried in the content element can be either 'text', 'html', 'xhtml',
  149.      * or any standard MIME type).
  150.      *
  151.      * @return  string|false
  152.      */
  153.     protected function getContent($method$arguments = array())
  154.     {
  155.         $attribute = empty($arguments[0]? false : $arguments[0];
  156.         $tags $this->model->getElementsByTagName('content');
  157.  
  158.         if ($tags->length == 0{
  159.             return false;
  160.         }
  161.  
  162.         $content $tags->item(0);
  163.  
  164.         if ($content->hasAttribute('type')) {
  165.             $content->setAttribute('type''text');
  166.         }
  167.         if (empty($attribute)) {
  168.             return $content->getAttribute($attribute);
  169.         }
  170.  
  171.         $type $content->getAttribute('type');
  172.  
  173.         if (empty($attribute)) {
  174.             if ($content->hasAttribute($attribute))
  175.             {
  176.                 return $content->getAttribute($attribute);
  177.             }
  178.             return false;
  179.         }
  180.  
  181.         if ($content->hasAttribute('src')) {
  182.             return $content->getAttribute('src');
  183.         }
  184.  
  185.         return $this->parseTextConstruct($content);
  186.      }
  187.  
  188.     /**
  189.      * For compatibility, this method provides a mapping to access enclosures.
  190.      *
  191.      * The Atom spec doesn't provide for an enclosure element, but it is
  192.      * generally supported using the link element with rel='enclosure'.
  193.      *
  194.      * @param   string  $method - for compatibility with our __call usage
  195.      * @param   array   $arguments - for compatibility with our __call usage
  196.      * @return  array|false
  197.      */
  198.     function getEnclosure($method$arguments = array())
  199.     {
  200.         $offset = isset($arguments[0]$arguments[0: 0;
  201.         $query "//atom:entry[atom:id='" $this->getText('id'false
  202.             "']/atom:link[@rel='enclosure']";
  203.  
  204.         $encs $this->parent->xpath->query($query);
  205.         if ($encs->length > $offset{
  206.             try {
  207.                 if ($encs->item($offset)->hasAttribute('href')) {
  208.                     return false;
  209.                 }
  210.                 $attrs $encs->item($offset)->attributes;
  211.                 $length $encs->item($offset)->hasAttribute('length'
  212.                     $encs->item($offset)->getAttribute('length': false;
  213.                 return array(
  214.                     'url' => $attrs->getNamedItem('href')->value,
  215.                     'type' => $attrs->getNamedItem('type')->value,
  216.                     'length' => $length);
  217.             catch (Exception $e{
  218.                 return false;
  219.             }
  220.         }
  221.         return false;
  222.     }
  223.     
  224.     /**
  225.      * Get details of this entry's source, if available/relevant
  226.      *
  227.      * Where an atom:entry is taken from another feed then the aggregator
  228.      * is supposed to include an atom:source element which replicates at least
  229.      * the atom:id, atom:title, and atom:updated metadata from the original
  230.      * feed. Atom:source therefore has a very similar structure to atom:feed
  231.      * and if we find it we will return it as an XML_Feed_Parser_Atom object.
  232.      *
  233.      * @return  XML_Feed_Parser_Atom|false
  234.      */
  235.     function getSource()
  236.     {
  237.         $test $this->model->getElementsByTagName('source');
  238.         if ($test->length == 0{
  239.             return false;
  240.         }
  241.         $source = new XML_Feed_Parser_Atom($test->item(0));
  242.     }
  243.  
  244.     /**
  245.      * Get the entry as an XML string
  246.      *
  247.      * Return an XML serialization of the feed, should it be required. Most
  248.      * users however, will already have a serialization that they used when
  249.      * instantiating the object.
  250.      *
  251.      * @return    string    XML serialization of element
  252.      */    
  253.     function __toString()
  254.     {
  255.         $simple simplexml_import_dom($this->model);
  256.         return $simple->asXML();
  257.     }
  258. }
  259.  
  260. ?>

Documentation generated on Wed, 19 Nov 2008 08:30:07 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.