Last active
January 26, 2016 02:05
-
-
Save kurozumi/a24ea4b0c6c450562949 to your computer and use it in GitHub Desktop.
【Amazon Product Adverstising API】ASINからパッケージ寸法を取得するクラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
class ItemLookup implements \IteratorAggregate | |
{ | |
private $items; | |
private $item; | |
public function __construct(\SimpleXMLElement $response) | |
{ | |
if(!isset($response->Items)){ | |
if(isset($response->Error)){ | |
throw new \InvalidArgumentException($response->Error->Message); | |
}else{ | |
throw new \InvalidArgumentException("Unexpected Error"); | |
} | |
} | |
$this->items = $response->Items; | |
} | |
public function getRequest() | |
{ | |
return $this->items->Request; | |
} | |
public function isValid() | |
{ | |
return ($this->getRequest()->IsValid == "True") ? true : false; | |
} | |
public function getError() | |
{ | |
return $this->getRequest()->Errors; | |
} | |
public function getIterator() | |
{ | |
foreach($this->items->Item as $item) | |
yield $this->item = $item; | |
} | |
public function getWeight() | |
{ | |
if(!isset($this->item->ItemAttributes->PackageDimensions->Weight)) | |
return 0; | |
return sprintf("%.2f", $this->item->ItemAttributes->PackageDimensions->Weight / 100 * 0.4536 * 1000); | |
} | |
public function getWidth() | |
{ | |
if(!isset($this->item->ItemAttributes->PackageDimensions->Width)) | |
return 0; | |
return sprintf("%.2f", $this->item->ItemAttributes->PackageDimensions->Width / 100 * 2.54); | |
} | |
public function getHeight() | |
{ | |
if(!isset($this->item->ItemAttributes->PackageDimensions->Height)) | |
return 0; | |
return sprintf("%.2f", $this->item->ItemAttributes->PackageDimensions->Height / 100 * 2.54); | |
} | |
public function getLength() | |
{ | |
if(!isset($this->item->ItemAttributes->PackageDimensions->Length)) | |
return 0; | |
return sprintf("%.2f", $this->item->ItemAttributes->PackageDimensions->Length / 100 * 2.54); | |
} | |
public function getAsin() | |
{ | |
return (string) $this->item->ASIN; | |
} | |
public function getBrowsenodes() | |
{ | |
return $this->item->BrowseNodes->BrowseNode; | |
} | |
public function getTitle() | |
{ | |
return (string) $this->item->ItemAttributes->Title; | |
} | |
public function getSalesRank() | |
{ | |
return (string) $this->item->SalesRank; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment