- <?php
- /**
- * This file contains the AmazonItem class. This class contains the minimum information
- * that {@link http://www.amazon.com/gp/browse.html/?node=3434641 AWS} returns for
- * any item.
- *
- * @author Mauricio Diaz <madd0@users.sourceforge.net>
- * @copyright 2004 (c) Mauricio Diaz Orlich
- * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
- * @version 0.3RC2
- *
- * @package amazOOP
- */
-
- /**
- * The AmazonItem class stores the minimum information that {@link }
- * http://www.amazon.com/gp/browse.html/?node=3434641 AWS} returns for any product.
- *
- * This information includes:
- * - <i>ASIN</i> of a product ({@link getASIN()})
- * - <i>Product's name</i> ({@link getProductName()})
- * - <i>List Price</i> of a product ({@link getListPrice()})
- * - <i>Amazon's Price</i> for a product ({@link getOurPrice()})
- *
- * @author Mauricio Diaz <madd0@users.sourceforge.net>
- * @copyright 2004 (c) Mauricio Diaz Orlich
- * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
- * @version 0.3RC2
- *
- * @package amazOOP
- * @subpackage Details
- */
- class AmazonItem {
- /**#@+
- * @access private
- */
-
- /**
- * Locale from which this product information was obtained.
- * @var string
- */
- var $locale;
-
- /**
- * Amazon Standart Item Number.
- * Each product sold at Amazon is identified by a different ASIN. ASINs are usually
- * different in different Amazon sites.
- * @var string
- */
- var $asin;
-
- /**
- * Name or title of the product.
- * @var string
- */
- var $productName;
-
- /**
- * Usually represents the price of the product suggested by the manufacturer.
- * Sometimes, the list price can be lower than Amazon's price, or be set to 0 if Amazon's price
- * is the same as the list price. By using selective=true when calling {@link getListPrice()}
- * false will be returned if the list price is strictly lower than Amazon's price.
- * @var string
- */
- var $listPrice;
-
- /**
- * Amazon's price.
- * @var string
- */
- var $ourPrice;
- /**#@-*/
-
-
- /**
- * Fetches the product's ASIN, or Amazon Standart Item Number.
- * Each product sold at Amazon is identified by a different ASIN. ASINs are usually
- * different in different Amazon sites.
- * Although the product's ASIN in not usually changed when encoded, a decoded
- * version is returned.
- *
- * @return string|booleanThe product's ASIN. Or false if the ASIN is not set.
- */
- function getASIN(){
- if(!isset($this->asin))
- return false;
- else
- return rawurldecode($this->asin);
- }
-
- /**
- * Decodes the product's name.
- *
- * @param integer $words the number of words you want to get, or 0 if you want
- * the complete title.
- * @param string $end the character(s) to be appended at the end of the title
- * in case the whole text is not returned.
- *
- * @return string|booleanThe product's name, the request part, or false if the name is not set.
- */
- function getProductName($words = 0, $end = "..."){
- if(!isset($this->productName))
- return false;
- else{
- if($words == 0)
- return rawurldecode($this->productName);
- else{
- $wordArray = preg_split("/[\s]/", rawurldecode($this->productName));
-
- $res = "";
- $words = min($words, sizeof($wordArray));
- for($i = 0; $i < $words; $i++){
- $res .= $wordArray[$i];
- if($i != $words-1)
- $res .= " ";
- }
- if($words < sizeof($wordArray))
- $res .= $end;
- return $res;
- }
- }
- }
-
- /**
- * The listed price of the item including its currency symbol.
- *
- * The function can be selective, in this case it will only return the price
- * if it exists and is different than $ourPrice.
- *
- * @param bool $selective if true, the price is only returned if it exists and
- * is different than $ourPrice
- *
- * @return string|booleanProduct's list price. Or false if none is set or it
- * is equal to $ourPrice.
- */
- function getListPrice($selective = true){
- if(isset($this->listPrice)){
- if(!$selective)
- return rawurldecode($this->listPrice);
- else{
- $list = rawurldecode($this->listPrice);
- $our = rawurldecode($this->ourPrice);
-
- if($list != $our && $list != "EUR 0,00" && $list != "")
- return $list;
- else
- return false;
- }
- }
- else
- return false;
- }
-
- /**
- * The price offered by Amazon for this product including its currency symbol.
- *
- * @return string|booleanAmazon's price for the product. Or false if none is set.
- */
- function getOurPrice(){
- if(isset($this->ourPrice))
- return rawurldecode($this->ourPrice);
- else
- return false;
- }
-
- /**
- * Calculates the difference between Amazon's price and the list price.
- *
- * If the list price is not strictly greater than Amazon's price, savings
- * are not calculated (since there are none) and false is returned.
- *
- * If money is saved, the difference between Amazon's price and the list
- * price is returned including the currency symbol.
- *
- * @param boolean $selective if this is set to false, the difference is
- * calculated no matter what the value of the list price is. It is recommended
- * not to use this.
- *
- * @return string|booleanThe price difference including the currency symbol.
- * Or false if the listed price for this product is not strictly greater than
- * Amazon's price.
- *
- * @since version 0.2. Before this, $locale had to be provided as first argument.
- */
- function getSavings($selective = true){
- if(!$selective || $this->getListPrice()){
- $decimalSep = "";
- $cents = "";
- $thousandsSep = "";
- $res = "";
- $number = array();
-
- //echo "<!-- ";
-
- $symbol = preg_replace("/([^0-9]*)[0-9]*.*/", "$1", rawurldecode($this->listPrice));
- //echo "Symbol: " . $symbol . "<br>\n";
- $list = preg_replace("/([^0-9]*)/", "", rawurldecode($this->listPrice));
- //echo "List: " . $list . "<br>\n";
- $our = preg_replace("/([^0-9]*)/", "", rawurldecode($this->ourPrice));
- //echo "Our: " . $our . "<br>\n";
-
- $savings = $list-$our;
- //echo "Savings: " . $savings . "<br>\n";
- if($savings <= 0 && $selective)
- return false;
-
-
- switch($this->locale){
- case "de":
- $thousandsSep = ".";
- break;
- default:
- $thousandsSep = ",";
- break;
- }
-
- if($this->locale != "jp"){
- $cents = substr($savings,-2);
- $savings = floor($savings/100);
-
- switch($this->locale){
- case "de":
- $decimalSep = ",";
- break;
- default:
- $decimalSep = ".";
- break;
- }
- }
-
- while($savings != 0){
- array_push($number, substr($savings,-3));
- $savings = floor($savings/1000);
- }
-
- while($temp = array_pop($number)){
- if(sizeof($number) > 0)
- $res .= $temp . $thousandsSep;
- else
- $res .= $temp;
- }
-
- if($res == 0)
- $res = "0";
- if($cents == 0)
- $cents == "00";
-
- $res = $symbol . $res . $decimalSep . $cents;
-
- return $res;
- }
- else
- return false;
- }
- }
- ?>