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

Source for file XMLDocument.php

Documentation is available at XMLDocument.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Services_Yahoo_Maps_XMLDocument class
  6.  * 
  7.  * Copyright 2005 Bryan Dunlap
  8.  *
  9.  * Licensed under the Apache License, Version 2.0 (the "License");
  10.  * you may not use this file except in compliance with the License.
  11.  * 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, software
  16.  * distributed under the License is distributed on an "AS IS" BASIS,
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18.  * See the License for the specific language governing permissions and
  19.  * limitations under the License.
  20.  * 
  21.  * @category   Web Services
  22.  * @package    Services_Yahoo
  23.  * @author     Bryan Dunlap <bdunlap@bryandunlap.com>
  24.  * @copyright  2005 Bryan Dunlap
  25.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  26.  * @version    CVS: $Id: XMLDocument.php,v 1.2 2006/10/02 12:53:33 mj Exp $
  27.  */
  28.  
  29. require_once "Services/Yahoo/Exception.php";
  30.  
  31. /**
  32.  * Provides facilities for creating and modifying XML for use with the
  33.  * Yahoo! Maps API.
  34.  * 
  35.  * The Yahoo! Maps XML is based on geoRSS 2.0
  36.  *
  37.  * @category   Web Services
  38.  * @package    Services_Yahoo
  39.  * @author     Bryan Dunlap <bdunlap@bryandunlap.com>
  40.  * @copyright  2005 Bryan Dunlap
  41.  * @license    http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
  42.  * @version    Release: @package_version@
  43.  */
  44. {
  45.  
  46.     /**
  47.      * Location of the GeoRSS 2.0 schema
  48.      *
  49.      * @const
  50.      */
  51.     const URI_GEO   = "http://www.w3.org/2003/01/geo/wgs84_pos#";
  52.  
  53.     /**
  54.      * Location of the Yahoo! Maps schema
  55.      *
  56.      * @const
  57.      */
  58.     const URI_YMAPS = "http://api.maps.yahoo.com/Maps/V1/AnnotatedMaps.xsd";
  59.  
  60.     /**
  61.      * DOMDocument instance
  62.      *
  63.      * @var    object 
  64.      * @access private
  65.      */
  66.     private $domDoc;
  67.         
  68.     /**
  69.      * Constructor
  70.      *
  71.      * @param  string  $file  (optional) a string containing the path to a valid
  72.      *                                    XML document
  73.      * @throws Services_Yahoo_Exception
  74.      * @access public
  75.      */
  76.     public function __construct($file = false)
  77.     {
  78.         if ($file{
  79.             $domDoc = DOMDocument::load($file);
  80.         else {
  81.             $this->createNew();
  82.         }
  83.     }
  84.     
  85.     /**
  86.      * Adds a group sub-element to the groups element
  87.      *
  88.      * @param  array  $options  an array containing valid attributes for the
  89.      *                           group element
  90.      * @return void 
  91.      * @throws Services_Yahoo_Exception
  92.      * @access public
  93.      */
  94.     public function addGroup($options)
  95.     {
  96.         $validOptions = array("id""title",
  97.                               "ymaps:BaseIcon",
  98.                               "ymaps:HoverIcon",
  99.                               "ymaps:PopupIcon");
  100.         if (!($groups $this->domDoc->getElementsByTagName("ymaps:Groups")
  101.                                      ->item(0))) {
  102.             $groups   $this->domDoc->getElementsByTagName("channel")
  103.                                      ->item(0)
  104.                                      ->appendChild($this->domDoc->createElement("ymaps:Groups"));
  105.         }
  106.         $group $this->domDoc->createElement("group");
  107.         foreach ($options as $name => $value{
  108.             if (!in_array($name$validOptions)) {
  109.                 throw new Services_Yahoo_Exception("Attribute {$name} not recognized");
  110.             }
  111.             $group->appendChild($this->setElementValue($this->domDoc->createElement($name),
  112.                                                        $value));
  113.         }
  114.         $groups->appendChild($group);
  115.     }
  116.     
  117.     /**
  118.      * Adds an item sub-element to the channel element
  119.      *
  120.      * @param  array  $options  an array containing valid attributes for the
  121.      *                           item element
  122.      * @return void 
  123.      * @throws Services_Yahoo_Exception
  124.      * @access public
  125.      */
  126.     public function addItem($options)
  127.     {
  128.         $validOptions = array("title""link""description",
  129.                               "ymaps:Address""ymaps:CityState",
  130.                               "ymaps:Zip""ymaps:Country",
  131.                               "geo:lat""geo:long",
  132.                               "ymaps:PhoneNumber""ymaps:Group",
  133.                               "ymaps:BaseIcon""ymaps:HoverIcon",
  134.                               "ymaps:PopupIcon""ymaps:ExtraLink",
  135.                               "ymaps:ExtraImage""ymaps:ItemUrl");
  136.         $channel $this->domDoc->getElementsByTagName("channel")
  137.                                 ->item(0);
  138.         $item $this->domDoc->createElement("item");
  139.         foreach ($options as $name => $value{
  140.             if (!in_array($name$validOptions)) {
  141.                 throw new Services_Yahoo_Exception("Attribute {$name} not recognized");
  142.             }
  143.             if ($name == "ymaps:ExtraLink"{
  144.                 foreach ($value as $linkText => $href{
  145.                     $extraLink $this->domDoc->createElement("ymaps:ExtraLink"
  146.                                                               $linkText);
  147.                     $extraLink->setAttribute("href"$href);
  148.                     $item->appendChild($extraLink);
  149.                 }
  150.             elseif ($name == "ymaps:ExtraImage"{
  151.                 $extraImage $this->domDoc->createElement("ymaps:ExtraImage");
  152.                 foreach ($value as $elementName => $elementValue{
  153.                     $extraImage->appendChild($this->setElementValue($this->domDoc->createElement($elementName),
  154.                                                                     $elementValue));
  155.                 }
  156.                 $item->appendChild($extraImage);
  157.             else {
  158.                 $item->appendChild($this->setElementValue($this->domDoc->createElement($name),
  159.                                                           $value));                
  160.             }
  161.         }
  162.         $channel->appendChild($item);
  163.     }
  164.     
  165.     /**
  166.      * Sets the image sub-element of the channel element
  167.      *
  168.      * @param  string  $imageURL   a string containing a valid URL to an image
  169.      * @return void 
  170.      * @access public
  171.      */
  172.     public function setBrandingImage($options)
  173.     {
  174.         $validOptions = array("id""title",
  175.                               "ymaps:BaseIcon",
  176.                               "ymaps:HoverIcon",
  177.                               "ymaps:PopupIcon");
  178.         $image $this->setChannelElement("image"$imageUrl);        
  179.         foreach ($options as $name => $value{
  180.             if (!in_array($name$validOptions)) {
  181.                 throw new Services_Yahoo_Exception("Attribute {$name} not recognized");
  182.             }
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Sets the description sub-element of the channel element
  188.      *
  189.      * @param  string $description  a string containing the description
  190.      * @return void 
  191.      * @access public
  192.      */
  193.     public function setDescription($description)
  194.     {
  195.         $this->setChannelElement("description"$description);        
  196.     }
  197.     
  198.     /**
  199.      * Sets the "defaultViewNumbered" attribute of the ymaps:Groups element
  200.      *
  201.      * @param  boolean $defaultViewNumbered 
  202.      * @return void 
  203.      * @access public
  204.      */
  205.     public function setDefaultViewNumbered($defaultViewNumbered = true)
  206.     {
  207.         $this->domDoc->getElementsByTagName("ymaps:Groups")
  208.                      ->item(0)
  209.                      ->setAttribute("defaultViewNumbered"(boolean) $defaultViewNumbered);
  210.     }
  211.  
  212.     /**
  213.      * Sets the ymaps::IntlCode sub-element of the channel element
  214.      *
  215.      * @param  string $languageCode  a string containing the language code
  216.      * @return void 
  217.      * @access public
  218.      */
  219.     public function setLanguageCode($languageCode)
  220.     {
  221.         $this->setChannelElement("ymaps:IntlCode"$languageCode);        
  222.     }
  223.  
  224.     /**
  225.      * Sets the geo:lat sub-element of the channel element
  226.      *
  227.      * @param  float $latitude  a float representing latitude
  228.      * @return void 
  229.      * @access public
  230.      */
  231.     public function setLatitude($latitude)
  232.     {
  233.         $this->setChannelElement("geo:lat"(float) $latitude);        
  234.     }
  235.  
  236.     /**
  237.      * Sets the geo:long sub-element of the channel element
  238.      *
  239.      * @param  float $longitude  a float representing longitude
  240.      * @return void 
  241.      * @access public
  242.      */
  243.     public function setLongitude($longitude)
  244.     {
  245.         $this->setChannelElement("geo:long"(float) $longitude);        
  246.     }
  247.  
  248.     /**
  249.      * Sets the link sub-element of the channel element
  250.      *
  251.      * @param  string $link  a string containing a valid URL
  252.      * @return void 
  253.      * @access public
  254.      */
  255.     public function setLink($link)
  256.     {
  257.         $this->setChannelElement("link"$link);
  258.     }
  259.     
  260.     /**
  261.      * Sets the title sub-element of the channel element
  262.      *
  263.      * @param  string $title  a string containing a title
  264.      * @return void 
  265.      * @access public
  266.      */
  267.     public function setTitle($title)
  268.     {
  269.         $this->setChannelElement("title"$title);        
  270.     }
  271.     
  272.     /**
  273.      * Sets the ymaps:ZoomLevel sub-element of the channel element
  274.      *
  275.      * @param  integer $zoomLevel  an integer representing the map"s zoom level
  276.      * @return void 
  277.      * @access public
  278.      */
  279.     public function setZoomLevel($zoomLevel)
  280.     {
  281.         $this->setChannelElement("ymaps:ZoomLevel"$zoomLevel);                
  282.     }
  283.     
  284.     /**
  285.      * Exports the DOM representation to XML and saves to file
  286.      *
  287.      * @param  string $file   the string containing the path of the
  288.      *                         file to save to
  289.      * @return boolean        true if successful
  290.      * @throws Services_Yahoo_Exception
  291.      * @access public
  292.      */
  293.     public function toFile($file)
  294.     {
  295.         if (!$this->domDoc->save($file)) {
  296.             throw new Services_Yahoo_Exception("DOM export to XML file {$file} failed");
  297.         }
  298.         return true;
  299.     }
  300.  
  301.     /**
  302.      * Exports the DOM representation to an XML string
  303.      *
  304.      * @return string 
  305.      * @throws Services_Yahoo_Exception
  306.      * @access public
  307.      */
  308.     public function toXML()
  309.     {
  310.         if (!($xmlString $this->domDoc->saveXML())) {
  311.             throw new Services_Yahoo_Exception("DOM export to XML string failed");
  312.         }
  313.         return $xmlString;
  314.     }
  315.  
  316.     /**
  317.      * Creates a new Yahoo! Maps XML Document
  318.      *
  319.      * @return void 
  320.      * @access private
  321.      */
  322.     private function createNew()
  323.     {
  324.         $domDoc = new DOMDocument("1.0""UTF-8");
  325.         $rss $domDoc->appendChild($domDoc->createElement("rss"));
  326.         $rss->setAttribute("version""2.0");
  327.         $rss->setAttribute("xmlns:geo"self::URI_GEO);
  328.         $rss->setAttribute("xmlns:ymaps"self::URI_YMAPS);
  329.         $channel $rss->appendChild($domDoc->createElement("channel"));
  330.         $channel->appendChild($domDoc->createElement("link"));
  331.         $channel->appendChild($domDoc->createElement("title"));
  332.         $channel->appendChild($domDoc->createElement("description"));
  333.         $this->domDoc $domDoc;
  334.     }
  335.     
  336.     /**
  337.      * Sets a channel sub-element
  338.      *
  339.      * @param  string  $name   a string containing the
  340.      *                          sub-element name
  341.      * @param  string  $value  a string containing the
  342.      *                          sub-element value
  343.      * @return object          DOMElement instance
  344.      * @access private
  345.      */
  346.     private function setChannelElement($name$value)
  347.     {
  348.         if (!($element $this->domDoc->getElementsByTagName("channel")
  349.                                       ->item(0)
  350.                                       ->getElementsByTagName($name)
  351.                                       ->item(0))) {
  352.             $element   $this->domDoc->getElementsByTagName("channel")
  353.                                       ->item(0)
  354.                                       ->appendChild($this->domDoc->createElement($name));
  355.         }
  356.         $element $this->setElementValue($element$value);
  357.         return $element;
  358.     }
  359.     
  360.     /**
  361.      * Sets an element's value
  362.      *
  363.      * @param  object  $element a DOMElement instance
  364.      * @param  string  $value   a string containing the
  365.      *                           element value
  366.      * @return object           DOMElement instance
  367.      * @access private
  368.      */
  369.     private function setElementValue($element$value)
  370.     {
  371.         $cData = array("link""url""href""image",
  372.                        "ymaps:BaseIcon""ymaps:HoverIcon",
  373.                        "ymaps:PopupIcon""ymaps:ExtraLink",
  374.                        "ymaps:ExtraImage""ymaps:ItemUrl");
  375.         (in_array($element->nodeName$cData)) 
  376.             $element->appendChild($this->domDoc->createCDATASection($value)) :
  377.             $element->nodeValue = $value;
  378.  
  379.         return $element;
  380.     }
  381. }

Documentation generated on Fri, 20 Apr 2007 14:30:10 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.