File indexing completed on 2024-12-29 05:28:01

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage Ebay
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id: Abstract.php 22791 2010-08-04 16:11:47Z renanbr $
0021  */
0022 
0023 /**
0024  * @see Zend_Service_Ebay_Abstract
0025  */
0026 // require_once 'Zend/Service/Ebay/Abstract.php';
0027 
0028 /**
0029  * @see Zend_Service_Ebay_Finding
0030  */
0031 // require_once 'Zend/Service/Ebay/Finding.php';
0032 
0033 /**
0034  * @category   Zend
0035  * @package    Zend_Service
0036  * @subpackage Ebay
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 abstract class Zend_Service_Ebay_Finding_Abstract
0041 {
0042     /**
0043      * @var DOMElement
0044      */
0045     protected $_dom;
0046 
0047     /**
0048      * @var DOMXPath
0049      */
0050     protected $_xPath;
0051 
0052     /**
0053      * @var array
0054      */
0055     protected $_attributes = array();
0056 
0057     /**
0058      * @param  DOMElement $dom
0059      * @return void
0060      */
0061     public function __construct(DOMElement $dom)
0062     {
0063         $this->_dom = $dom;
0064         $this->_initXPath();
0065         $this->_init();
0066     }
0067 
0068     /**
0069      * @param  string $tag
0070      * @param  string $attribute
0071      * @return mixed
0072      */
0073     public function attributes($tag, $attribute = null)
0074     {
0075         if (null === $attribute) {
0076             // all attributes
0077             if (array_key_exists($tag, $this->_attributes)) {
0078                 return $this->_attributes[$tag];
0079             }
0080             return array();
0081         }
0082 
0083         // a specific attribute
0084         if (isset($this->_attributes[$tag][$attribute])) {
0085             return $this->_attributes[$tag][$attribute];
0086         }
0087         return null;
0088     }
0089 
0090     /**
0091      * Initialize object.
0092      *
0093      * Post construct logic, classes must read their members here. Called from
0094      * {@link __construct()} as final step of object initialization.
0095      *
0096      * @return void
0097      */
0098     protected function _init()
0099     {
0100     }
0101 
0102     /**
0103      * Load DOMXPath for current DOM object.
0104      *
0105      * @see    Zend_Service_Ebay_Finding::_parseResponse()
0106      * @return void
0107      */
0108     protected function _initXPath()
0109     {
0110         $document = $this->_dom->ownerDocument;
0111         if (!isset($document->ebayFindingXPath)) {
0112             $xpath = new DOMXPath($document);
0113             foreach (Zend_Service_Ebay_Finding::getXmlNamespaces() as $alias => $uri) {
0114                 $xpath->registerNamespace($alias, $uri);
0115             }
0116             $document->ebayFindingXPath = $xpath;
0117         }
0118         $this->_xPath = $document->ebayFindingXPath;
0119     }
0120 
0121     /**
0122      * @return DOMElement
0123      */
0124     public function getDom()
0125     {
0126         return $this->_dom;
0127     }
0128 
0129     /**
0130      * @return DOMXPath
0131      */
0132     public function getXPath()
0133     {
0134         return $this->_xPath;
0135     }
0136 
0137     /**
0138      * @param  string $path
0139      * @param  string $type
0140      * @param  string $array When true means it expects more than one node occurence
0141      * @return mixed
0142      */
0143     protected function _query($path, $type, $array = false)
0144     {
0145         // find values
0146         $values = array();
0147         $nodes  = $this->_xPath->query($path, $this->_dom);
0148         foreach ($nodes as $node) {
0149             $value    = (string) $node->nodeValue;
0150             $values[] = Zend_Service_Ebay_Abstract::toPhpValue($value, $type);
0151             if (!$array) {
0152                 break;
0153             }
0154         }
0155 
0156         // array
0157         if ($array) {
0158             return $values;
0159         }
0160 
0161         // single value
0162         if (count($values)) {
0163             return reset($values);
0164         }
0165 
0166         // no nodes fount
0167         return null;
0168     }
0169 }