web/lib/Zend/Service/Amazon/Item.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Service
       
    18  * @subpackage Amazon
       
    19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  * @version    $Id: Item.php 21883 2010-04-16 14:57:07Z dragonbe $
       
    22  */
       
    23 
       
    24 
       
    25 /**
       
    26  * @category   Zend
       
    27  * @package    Zend_Service
       
    28  * @subpackage Amazon
       
    29  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    30  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    31  */
       
    32 class Zend_Service_Amazon_Item
       
    33 {
       
    34     /**
       
    35      * @var string
       
    36      */
       
    37     public $ASIN;
       
    38 
       
    39     /**
       
    40      * @var string
       
    41      */
       
    42     public $DetailPageURL;
       
    43 
       
    44     /**
       
    45      * @var int
       
    46      */
       
    47     public $SalesRank;
       
    48 
       
    49     /**
       
    50      * @var int
       
    51      */
       
    52     public $TotalReviews;
       
    53 
       
    54     /**
       
    55      * @var int
       
    56      */
       
    57     public $AverageRating;
       
    58 
       
    59     /**
       
    60      * @var string
       
    61      */
       
    62     public $SmallImage;
       
    63 
       
    64     /**
       
    65      * @var string
       
    66      */
       
    67     public $MediumImage;
       
    68 
       
    69     /**
       
    70      * @var string
       
    71      */
       
    72     public $LargeImage;
       
    73 
       
    74     /**
       
    75      * @var string
       
    76      */
       
    77     public $Subjects;
       
    78 
       
    79     /**
       
    80      * @var Zend_Service_Amazon_OfferSet
       
    81      */
       
    82     public $Offers;
       
    83 
       
    84     /**
       
    85      * @var Zend_Service_Amazon_CustomerReview[]
       
    86      */
       
    87     public $CustomerReviews = array();
       
    88 
       
    89     /**
       
    90      * @var Zend_Service_Amazon_SimilarProducts[]
       
    91      */
       
    92     public $SimilarProducts = array();
       
    93 
       
    94     /**
       
    95      * @var Zend_Service_Amazon_Accessories[]
       
    96      */
       
    97     public $Accessories = array();
       
    98 
       
    99     /**
       
   100      * @var array
       
   101      */
       
   102     public $Tracks = array();
       
   103 
       
   104     /**
       
   105      * @var Zend_Service_Amazon_ListmaniaLists[]
       
   106      */
       
   107     public $ListmaniaLists = array();
       
   108 
       
   109     protected $_dom;
       
   110 
       
   111 
       
   112     /**
       
   113      * Parse the given <Item> element
       
   114      *
       
   115      * @param  null|DOMElement $dom
       
   116      * @return void
       
   117      * @throws	Zend_Service_Amazon_Exception
       
   118      * 
       
   119      * @group ZF-9547
       
   120      */
       
   121     public function __construct($dom)
       
   122     {
       
   123     	if (null === $dom) {
       
   124     		require_once 'Zend/Service/Amazon/Exception.php';
       
   125     		throw new Zend_Service_Amazon_Exception('Item element is empty');
       
   126     	}
       
   127     	if (!$dom instanceof DOMElement) {
       
   128     		require_once 'Zend/Service/Amazon/Exception.php';
       
   129     		throw new Zend_Service_Amazon_Exception('Item is not a valid DOM element');
       
   130     	}
       
   131         $xpath = new DOMXPath($dom->ownerDocument);
       
   132         $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
       
   133         $this->ASIN = $xpath->query('./az:ASIN/text()', $dom)->item(0)->data;
       
   134 
       
   135         $result = $xpath->query('./az:DetailPageURL/text()', $dom);
       
   136         if ($result->length == 1) {
       
   137             $this->DetailPageURL = $result->item(0)->data;
       
   138         }
       
   139 
       
   140         if ($xpath->query('./az:ItemAttributes/az:ListPrice', $dom)->length >= 1) {
       
   141             $this->CurrencyCode = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:CurrencyCode/text()', $dom)->item(0)->data;
       
   142             $this->Amount = (int) $xpath->query('./az:ItemAttributes/az:ListPrice/az:Amount/text()', $dom)->item(0)->data;
       
   143             $this->FormattedPrice = (string) $xpath->query('./az:ItemAttributes/az:ListPrice/az:FormattedPrice/text()', $dom)->item(0)->data;
       
   144         }
       
   145 
       
   146         $result = $xpath->query('./az:ItemAttributes/az:*/text()', $dom);
       
   147         if ($result->length >= 1) {
       
   148             foreach ($result as $v) {
       
   149                 if (isset($this->{$v->parentNode->tagName})) {
       
   150                     if (is_array($this->{$v->parentNode->tagName})) {
       
   151                         array_push($this->{$v->parentNode->tagName}, (string) $v->data);
       
   152                     } else {
       
   153                         $this->{$v->parentNode->tagName} = array($this->{$v->parentNode->tagName}, (string) $v->data);
       
   154                     }
       
   155                 } else {
       
   156                     $this->{$v->parentNode->tagName} = (string) $v->data;
       
   157                 }
       
   158             }
       
   159         }
       
   160 
       
   161         foreach (array('SmallImage', 'MediumImage', 'LargeImage') as $im) {
       
   162             $result = $xpath->query("./az:ImageSets/az:ImageSet[position() = 1]/az:$im", $dom);
       
   163             if ($result->length == 1) {
       
   164                 /**
       
   165                  * @see Zend_Service_Amazon_Image
       
   166                  */
       
   167                 require_once 'Zend/Service/Amazon/Image.php';
       
   168                 $this->$im = new Zend_Service_Amazon_Image($result->item(0));
       
   169             }
       
   170         }
       
   171 
       
   172         $result = $xpath->query('./az:SalesRank/text()', $dom);
       
   173         if ($result->length == 1) {
       
   174             $this->SalesRank = (int) $result->item(0)->data;
       
   175         }
       
   176 
       
   177         $result = $xpath->query('./az:CustomerReviews/az:Review', $dom);
       
   178         if ($result->length >= 1) {
       
   179             /**
       
   180              * @see Zend_Service_Amazon_CustomerReview
       
   181              */
       
   182             require_once 'Zend/Service/Amazon/CustomerReview.php';
       
   183             foreach ($result as $review) {
       
   184                 $this->CustomerReviews[] = new Zend_Service_Amazon_CustomerReview($review);
       
   185             }
       
   186             $this->AverageRating = (float) $xpath->query('./az:CustomerReviews/az:AverageRating/text()', $dom)->item(0)->data;
       
   187             $this->TotalReviews = (int) $xpath->query('./az:CustomerReviews/az:TotalReviews/text()', $dom)->item(0)->data;
       
   188         }
       
   189 
       
   190         $result = $xpath->query('./az:EditorialReviews/az:*', $dom);
       
   191         if ($result->length >= 1) {
       
   192             /**
       
   193              * @see Zend_Service_Amazon_EditorialReview
       
   194              */
       
   195             require_once 'Zend/Service/Amazon/EditorialReview.php';
       
   196             foreach ($result as $r) {
       
   197                 $this->EditorialReviews[] = new Zend_Service_Amazon_EditorialReview($r);
       
   198             }
       
   199         }
       
   200 
       
   201         $result = $xpath->query('./az:SimilarProducts/az:*', $dom);
       
   202         if ($result->length >= 1) {
       
   203             /**
       
   204              * @see Zend_Service_Amazon_SimilarProduct
       
   205              */
       
   206             require_once 'Zend/Service/Amazon/SimilarProduct.php';
       
   207             foreach ($result as $r) {
       
   208                 $this->SimilarProducts[] = new Zend_Service_Amazon_SimilarProduct($r);
       
   209             }
       
   210         }
       
   211 
       
   212         $result = $xpath->query('./az:ListmaniaLists/*', $dom);
       
   213         if ($result->length >= 1) {
       
   214             /**
       
   215              * @see Zend_Service_Amazon_ListmaniaList
       
   216              */
       
   217             require_once 'Zend/Service/Amazon/ListmaniaList.php';
       
   218             foreach ($result as $r) {
       
   219                 $this->ListmaniaLists[] = new Zend_Service_Amazon_ListmaniaList($r);
       
   220             }
       
   221         }
       
   222 
       
   223         $result = $xpath->query('./az:Tracks/az:Disc', $dom);
       
   224         if ($result->length > 1) {
       
   225             foreach ($result as $disk) {
       
   226                 foreach ($xpath->query('./*/text()', $disk) as $t) {
       
   227                     // TODO: For consistency in a bugfix all tracks are appended to one single array
       
   228                     // Erroreous line: $this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
       
   229                     $this->Tracks[] = (string) $t->data;
       
   230                 }
       
   231             }
       
   232         } else if ($result->length == 1) {
       
   233             foreach ($xpath->query('./*/text()', $result->item(0)) as $t) {
       
   234                 $this->Tracks[] = (string) $t->data;
       
   235             }
       
   236         }
       
   237 
       
   238         $result = $xpath->query('./az:Offers', $dom);
       
   239         $resultSummary = $xpath->query('./az:OfferSummary', $dom);
       
   240         if ($result->length > 1 || $resultSummary->length == 1) {
       
   241             /**
       
   242              * @see Zend_Service_Amazon_OfferSet
       
   243              */
       
   244             require_once 'Zend/Service/Amazon/OfferSet.php';
       
   245             $this->Offers = new Zend_Service_Amazon_OfferSet($dom);
       
   246         }
       
   247 
       
   248         $result = $xpath->query('./az:Accessories/*', $dom);
       
   249         if ($result->length > 1) {
       
   250             /**
       
   251              * @see Zend_Service_Amazon_Accessories
       
   252              */
       
   253             require_once 'Zend/Service/Amazon/Accessories.php';
       
   254             foreach ($result as $r) {
       
   255                 $this->Accessories[] = new Zend_Service_Amazon_Accessories($r);
       
   256             }
       
   257         }
       
   258 
       
   259         $this->_dom = $dom;
       
   260     }
       
   261 
       
   262 
       
   263     /**
       
   264      * Returns the item's original XML
       
   265      *
       
   266      * @return string
       
   267      */
       
   268     public function asXml()
       
   269     {
       
   270         return $this->_dom->ownerDocument->saveXML($this->_dom);
       
   271     }
       
   272 }