web/enmi/Zend/Service/Ebay/Finding/Abstract.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service
       
    17  * @subpackage Ebay
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Abstract.php 22791 2010-08-04 16:11:47Z renanbr $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_Ebay_Abstract
       
    25  */
       
    26 require_once 'Zend/Service/Ebay/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Service_Ebay_Finding
       
    30  */
       
    31 require_once 'Zend/Service/Ebay/Finding.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_Service
       
    36  * @subpackage Ebay
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 abstract class Zend_Service_Ebay_Finding_Abstract
       
    41 {
       
    42     /**
       
    43      * @var DOMElement
       
    44      */
       
    45     protected $_dom;
       
    46 
       
    47     /**
       
    48      * @var DOMXPath
       
    49      */
       
    50     protected $_xPath;
       
    51 
       
    52     /**
       
    53      * @var array
       
    54      */
       
    55     protected $_attributes = array();
       
    56 
       
    57     /**
       
    58      * @param  DOMElement $dom
       
    59      * @return void
       
    60      */
       
    61     public function __construct(DOMElement $dom)
       
    62     {
       
    63         $this->_dom = $dom;
       
    64         $this->_initXPath();
       
    65         $this->_init();
       
    66     }
       
    67 
       
    68     /**
       
    69      * @param  string $tag
       
    70      * @param  string $attribute
       
    71      * @return mixed
       
    72      */
       
    73     public function attributes($tag, $attribute = null)
       
    74     {
       
    75         if (null === $attribute) {
       
    76             // all attributes
       
    77             if (array_key_exists($tag, $this->_attributes)) {
       
    78                 return $this->_attributes[$tag];
       
    79             }
       
    80             return array();
       
    81         }
       
    82 
       
    83         // a specific attribute
       
    84         if (isset($this->_attributes[$tag][$attribute])) {
       
    85             return $this->_attributes[$tag][$attribute];
       
    86         }
       
    87         return null;
       
    88     }
       
    89 
       
    90     /**
       
    91      * Initialize object.
       
    92      *
       
    93      * Post construct logic, classes must read their members here. Called from
       
    94      * {@link __construct()} as final step of object initialization.
       
    95      *
       
    96      * @return void
       
    97      */
       
    98     protected function _init()
       
    99     {
       
   100     }
       
   101 
       
   102     /**
       
   103      * Load DOMXPath for current DOM object.
       
   104      *
       
   105      * @see    Zend_Service_Ebay_Finding::_parseResponse()
       
   106      * @return void
       
   107      */
       
   108     protected function _initXPath()
       
   109     {
       
   110         $document = $this->_dom->ownerDocument;
       
   111         if (!isset($document->ebayFindingXPath)) {
       
   112             $xpath = new DOMXPath($document);
       
   113             foreach (Zend_Service_Ebay_Finding::getXmlNamespaces() as $alias => $uri) {
       
   114                 $xpath->registerNamespace($alias, $uri);
       
   115             }
       
   116             $document->ebayFindingXPath = $xpath;
       
   117         }
       
   118         $this->_xPath = $document->ebayFindingXPath;
       
   119     }
       
   120 
       
   121     /**
       
   122      * @return DOMElement
       
   123      */
       
   124     public function getDom()
       
   125     {
       
   126         return $this->_dom;
       
   127     }
       
   128 
       
   129     /**
       
   130      * @return DOMXPath
       
   131      */
       
   132     public function getXPath()
       
   133     {
       
   134         return $this->_xPath;
       
   135     }
       
   136 
       
   137     /**
       
   138      * @param  string $path
       
   139      * @param  string $type
       
   140      * @param  string $array When true means it expects more than one node occurence
       
   141      * @return mixed
       
   142      */
       
   143     protected function _query($path, $type, $array = false)
       
   144     {
       
   145         // find values
       
   146         $values = array();
       
   147         $nodes  = $this->_xPath->query($path, $this->_dom);
       
   148         foreach ($nodes as $node) {
       
   149             $value    = (string) $node->nodeValue;
       
   150             $values[] = Zend_Service_Ebay_Abstract::toPhpValue($value, $type);
       
   151             if (!$array) {
       
   152                 break;
       
   153             }
       
   154         }
       
   155 
       
   156         // array
       
   157         if ($array) {
       
   158             return $values;
       
   159         }
       
   160 
       
   161         // single value
       
   162         if (count($values)) {
       
   163             return reset($values);
       
   164         }
       
   165 
       
   166         // no nodes fount
       
   167         return null;
       
   168     }
       
   169 }