Apache Zeta Components Manual :: File Source for dublincore_module.php

Source for file dublincore_module.php

Documentation is available at dublincore_module.php

  1. <?php
  2. /**
  3.  * File containing the ezcFeedDublinCoreModule class.
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @package Feed
  23.  * @version //autogentag//
  24.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25.  * @filesource
  26.  */
  27.  
  28. /**
  29.  * Support for the DublinCore module: data container, generator, parser.
  30.  *
  31.  * Specifications: {@link http://dublincore.org/documents/dces/}.
  32.  *
  33.  * Each DublinCore property can appear multiple times both on the feed-level
  34.  * or the item-level.
  35.  *
  36.  * Each DublinCore property can have the language attribute, which appears in
  37.  * the generated XML file as a 'xml:lang' attribute.
  38.  *
  39.  * Create example:
  40.  *
  41.  * <code>
  42.  * <?php
  43.  * // $feed is an ezcFeed object
  44.  * $item = $feed->add( 'item' );
  45.  * $module = $item->addModule( 'DublinCore' );
  46.  * $creator = $module->add( 'creator' );
  47.  * $creator->name = 'Creator name';
  48.  * $creator->language = 'en'; // optional
  49.  * // more elements of the same type can be added
  50.  * ?>
  51.  * </code>
  52.  *
  53.  * Parse example:
  54.  *
  55.  * <code>
  56.  * <?php
  57.  * // $item is an ezcFeedEntryElement object
  58.  * foreach ( $item->DublinCore->creator as $creator )
  59.  * {
  60.  *     echo $creator->name;
  61.  *     echo $creator->language;
  62.  * }
  63.  * ?>
  64.  * </code>
  65.  *
  66.  * @property array(ezcFeedPersonElement) $contributor 
  67.  *                                        An entity responsible for making contributions to
  68.  *                                        the resource.
  69.  *                                        Usually the name of a person, organization or service.
  70.  * @property array(ezcFeedTextElement) $coverage 
  71.  *                                      The spatial or temporal topic of the resource, the
  72.  *                                      spatial applicability of the resource, or the
  73.  *                                      jurisdiction under which the resource is relevant.
  74.  *                                      A recommended practice is to use a controlled
  75.  *                                      vocabulary such as
  76.  *                                      {@link http://www.getty.edu/research/tools/vocabulary/tgn/index.html TGN}.
  77.  * @property array(ezcFeedPersonElement) $creator 
  78.  *                                        An entity responsible for making the resource.
  79.  *                                        Usually the name of a person or organization.
  80.  * @property array(ezcFeedDateElement) $date 
  81.  *                                      A point or period of time associated with an event
  82.  *                                      in the lifecycle of the resource. It is a Unix
  83.  *                                      timestamp, which will be converted to an
  84.  *                                      {@link http://www.w3.org/TR/NOTE-datetime ISO 8601}
  85.  *                                      date when generating the feed.
  86.  * @property array(ezcFeedTextElement) $description 
  87.  *                                      A description of the resource.
  88.  * @property array(ezcFeedTextElement) $format 
  89.  *                                      The file format, physical medium, or dimensions of
  90.  *                                      the resource.
  91.  *                                      Recommended best practices is to use a controlled
  92.  *                                      vocabulary such as the list of
  93.  *                                      {@link http://www.iana.org/assignments/media-types/ Internet Media Types}
  94.  *                                      (MIME).
  95.  * @property array(ezcFeedIdElement) $identifier 
  96.  *                                    An unambiguous reference to the resource within a
  97.  *                                    given context.
  98.  * @property array(ezcFeedTextElement) $language 
  99.  *                                      A language of the resource.
  100.  *                                      Recommended best practice is to use a controlled
  101.  *                                      vocabulary such as
  102.  *                                      {@link http://www.faqs.org/rfcs/rfc4646.html RFC 4646}.
  103.  * @property array(ezcFeedPersonElement) $publisher 
  104.  *                                        An entity responsible for making the resource available.
  105.  *                                        Usually the name of a person, organization or service.
  106.  * @property array(ezcFeedTextElement) $relation 
  107.  *                                      A related resource.
  108.  * @property array(ezcFeedTextElement) $rights 
  109.  *                                      Information about rights held in and over the resource.
  110.  * @property array(ezcFeedSourceElement) $source 
  111.  *                                        A related resource from which the described resource
  112.  *                                        is derived.
  113.  * @property array(ezcFeedTextElement) $subject 
  114.  *                                      The topic of the resource.
  115.  * @property array(ezcFeedTextElement) $title 
  116.  *                                      The name given to the resource.
  117.  * @property array(ezcFeedTextElement) $type 
  118.  *                                      The nature or genre of the resource.
  119.  *                                      Recommended best practice is to use a controlled
  120.  *                                      vocabulary such as the
  121.  *                                      {@link http://dublincore.org/documents/dcmi-type-vocabulary/ DCMI Type Vocabulary}
  122.  *
  123.  * @package Feed
  124.  * @version //autogentag//
  125.  */
  126. {
  127.     /**
  128.      * Constructs a new ezcFeedDublinCoreModule object.
  129.      *
  130.      * @param string $level The level of the data container ('feed' or 'item')
  131.      */
  132.     public function __construct$level 'feed' )
  133.     {
  134.         parent::__construct$level );
  135.     }
  136.  
  137.     /**
  138.      * Sets the property $name to $value.
  139.      *
  140.      * @throws ezcBasePropertyNotFoundException
  141.      *          if the property $name is not defined
  142.      *
  143.      * @param string $name The property name
  144.      * @param mixed $value The property value
  145.      * @ignore
  146.      */
  147.     public function __set$name$value )
  148.     {
  149.         if $this->isElementAllowed$name ) )
  150.         {
  151.             switch $name )
  152.             {
  153.                 case 'date':
  154.                     $node $this->add$name );
  155.                     $node->date $value;
  156.                     break;
  157.  
  158.                 case 'contributor':
  159.                 case 'creator':
  160.                 case 'publisher':
  161.                     $node $this->add$name );
  162.                     $node->name $value;
  163.                     break;
  164.  
  165.                 case 'identifier':
  166.                     $node $this->add$name );
  167.                     $node->id $value;
  168.                     break;
  169.  
  170.                 case 'source':
  171.                     $node $this->add$name );
  172.                     $node->source $value;
  173.                     break;
  174.  
  175.                 default:
  176.                     $node new ezcFeedTextElement();
  177.                     break;
  178.             }
  179.         }
  180.         else
  181.         {
  182.             parent::__set$name$value );
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Returns the value of property $name.
  188.      *
  189.      * @throws ezcBasePropertyNotFoundException
  190.      *          if the property $name is not defined
  191.      *
  192.      * @param string $name The property name
  193.      * @return mixed 
  194.      * @ignore
  195.      */
  196.     public function __get$name )
  197.     {
  198.         if $this->isElementAllowed$name ) )
  199.         {
  200.             return $this->properties[$name];
  201.         }
  202.         else
  203.         {
  204.             return parent::__get$name );
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Returns if the property $name is set.
  210.      *
  211.      * @param string $name The property name
  212.      * @return bool 
  213.      * @ignore
  214.      */
  215.     public function __isset$name )
  216.     {
  217.         if $this->isElementAllowed$name ) )
  218.         {
  219.             return isset$this->properties[$name);
  220.         }
  221.         else
  222.         {
  223.             return parent::__isset$name );
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Returns true if the element $name is allowed in the current module at the
  229.      * current level (feed or item), and false otherwise.
  230.      *
  231.      * @param string $name The element name to check if allowed in the current module and level (feed or item)
  232.      * @return bool 
  233.      */
  234.     public function isElementAllowed$name )
  235.     {
  236.         switch $this->level )
  237.         {
  238.             case 'feed':
  239.                 if in_array$namearray'contributor''coverage''creator',
  240.                                              'date''description''format',
  241.                                              'identifier''language''publisher',
  242.                                              'relation''rights''source',
  243.                                              'subject''title''type' ) ) )
  244.                 {
  245.                     return true;
  246.                 }
  247.                 break;
  248.  
  249.             case 'item':
  250.                 if in_array$namearray'contributor''coverage''creator',
  251.                                              'date''description''format',
  252.                                              'identifier''language''publisher',
  253.                                              'relation''rights''source',
  254.                                              'subject''title''type' ) ) )
  255.                 {
  256.                     return true;
  257.                 }
  258.                 break;
  259.         }
  260.         return false;
  261.     }
  262.  
  263.     /**
  264.      * Adds a new ezcFeedElement element with name $name to this module and
  265.      * returns it.
  266.      *
  267.      * @throws ezcFeedUnsupportedElementException
  268.      *          if trying to add an element which is not supported.
  269.      *
  270.      * @param string $name The element name
  271.      * @return ezcFeedElement 
  272.      */
  273.     public function add$name )
  274.     {
  275.         if $this->isElementAllowed$name ) )
  276.         {
  277.             switch $name )
  278.             {
  279.                 case 'date':
  280.                     $node new ezcFeedDateElement();
  281.                     break;
  282.  
  283.                 case 'contributor':
  284.                 case 'creator':
  285.                 case 'publisher':
  286.                     $node new ezcFeedPersonElement();
  287.                     break;
  288.  
  289.                 case 'identifier':
  290.                     $node new ezcFeedIdElement();
  291.                     break;
  292.  
  293.                 case 'source':
  294.                     $node new ezcFeedSourceElement();
  295.                     break;
  296.  
  297.                 default:
  298.                     $node new ezcFeedTextElement();
  299.                     break;
  300.             }
  301.  
  302.             $this->properties[$name][$node;
  303.             return $node;
  304.         }
  305.         else
  306.         {
  307.             throw new ezcFeedUnsupportedElementException$name );
  308.         }
  309.     }
  310.  
  311.     /**
  312.      * Adds the module elements to the $xml XML document, in the container $root.
  313.      *
  314.      * @param DOMDocument $xml The XML document in which to add the module elements
  315.      * @param DOMNode $root The parent node which will contain the module elements
  316.      */
  317.     public function generateDOMDocument $xmlDOMNode $root )
  318.     {
  319.         $elements array'contributor''coverage''creator',
  320.                            'date''description''format',
  321.                            'identifier''language''publisher',
  322.                            'relation''rights''source',
  323.                            'subject''title''type');
  324.  
  325.         foreach $elements as $element )
  326.         {
  327.             if isset$this->$element ) )
  328.             {
  329.                 foreach $this->$element as $values )
  330.                 {
  331.                     $elementTag $xml->createElement$this->getNamespacePrefix(':' $element );
  332.                     $root->appendChild$elementTag );
  333.  
  334.                     switch $element )
  335.                     {
  336.                         case 'date':
  337.                             $elementTag->nodeValue $values->date->format'c' );
  338.                             break;
  339.  
  340.                         default:
  341.                             $elementTag->nodeValue $values->__toString();
  342.                             break;
  343.                     }
  344.  
  345.                     if isset$values->language ) )
  346.                     {
  347.                         $this->addAttribute$xml$elementTag'xml:lang'$values->language );
  348.                     }
  349.                 }
  350.             }
  351.         }
  352.     }
  353.  
  354.     /**
  355.      * Parses the XML element $node and creates a feed element in the current
  356.      * module with name $name.
  357.      *
  358.      * @param string $name The name of the element belonging to the module
  359.      * @param DOMElement $node The XML child from which to take the values for $name
  360.      */
  361.     public function parse$nameDOMElement $node )
  362.     {
  363.         if $this->isElementAllowed$name ) )
  364.         {
  365.             $element $this->add$name );
  366.             $value $node->textContent;
  367.  
  368.             switch $name )
  369.             {
  370.                 case 'date':
  371.                     $element->date $value;
  372.                     break;
  373.  
  374.                 case 'contributor':
  375.                 case 'creator':
  376.                 case 'publisher':
  377.                     $element->name $value;
  378.                     break;
  379.  
  380.                 case 'identifier':
  381.                     $element->id $value;
  382.                     break;
  383.  
  384.                 case 'source':
  385.                     $element->source $value;
  386.                     break;
  387.  
  388.                 default:
  389.                     $element->text $value;
  390.             }
  391.  
  392.             if $node->hasAttributes() )
  393.             {
  394.                 foreach $node->attributes as $attribute )
  395.                 {
  396.                     switch $attribute->name )
  397.                     {
  398.                         case 'lang':
  399.                             $element->language $attribute->value;
  400.                             break;
  401.                     }
  402.                 }
  403.             }
  404.         }
  405.     }
  406.  
  407.     /**
  408.      * Returns the module name ('DublinCore').
  409.      *
  410.      * @return string 
  411.      */
  412.     public static function getModuleName()
  413.     {
  414.         return 'DublinCore';
  415.     }
  416.  
  417.     /**
  418.      * Returns the namespace for this module ('http://purl.org/dc/elements/1.1/').
  419.      *
  420.      * @return string 
  421.      */
  422.     public static function getNamespace()
  423.     {
  424.         return 'http://purl.org/dc/elements/1.1/';
  425.     }
  426.  
  427.     /**
  428.      * Returns the namespace prefix for this module ('dc').
  429.      *
  430.      * @return string 
  431.      */
  432.     public static function getNamespacePrefix()
  433.     {
  434.         return 'dc';
  435.     }
  436. }
  437. ?>
Documentation generated by phpDocumentor 1.4.3