amazOOP amazOOP
[ class tree: amazOOP ] [ index: amazOOP ] [ all elements ]

Source for file item.class.php

Documentation is available at item.class.php

  1. <?php
  2. /**
  3. * This file contains the AmazonItem class. This class contains the minimum information
  4. * that {@link http://www.amazon.com/gp/browse.html/?node=3434641 AWS} returns for
  5. * any item.
  6. *
  7. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  8. * @copyright 2004 (c) Mauricio Diaz Orlich
  9. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  10. * @version 0.3RC2
  11. *
  12. * @package amazOOP
  13. */
  14.  
  15. /**
  16. * The AmazonItem class stores the minimum information that {@link }
  17. * http://www.amazon.com/gp/browse.html/?node=3434641 AWS} returns for any product.
  18. *
  19. * This information includes:
  20. * - <i>ASIN</i> of a product ({@link getASIN()})
  21. * - <i>Product's name</i> ({@link getProductName()})
  22. * - <i>List Price</i> of a product ({@link getListPrice()})
  23. * - <i>Amazon's Price</i> for a product ({@link getOurPrice()})
  24. *
  25. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  26. * @copyright 2004 (c) Mauricio Diaz Orlich
  27. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  28. * @version 0.3RC2
  29. *
  30. * @package amazOOP
  31. * @subpackage Details
  32. */
  33. class AmazonItem {
  34. /**#@+
  35. * @access private
  36. */
  37.  
  38. /**
  39. * Locale from which this product information was obtained.
  40. * @var string
  41. */
  42. var $locale;
  43.  
  44. /**
  45. * Amazon Standart Item Number.
  46. * Each product sold at Amazon is identified by a different ASIN. ASINs are usually
  47. * different in different Amazon sites.
  48. * @var string
  49. */
  50. var $asin;
  51.  
  52. /**
  53. * Name or title of the product.
  54. * @var string
  55. */
  56. var $productName;
  57.  
  58. /**
  59. * Usually represents the price of the product suggested by the manufacturer.
  60. * Sometimes, the list price can be lower than Amazon's price, or be set to 0 if Amazon's price
  61. * is the same as the list price. By using selective=true when calling {@link getListPrice()}
  62. * false will be returned if the list price is strictly lower than Amazon's price.
  63. * @var string
  64. */
  65. var $listPrice;
  66.  
  67. /**
  68. * Amazon's price.
  69. * @var string
  70. */
  71. var $ourPrice;
  72. /**#@-*/
  73.  
  74.  
  75. /**
  76. * Fetches the product's ASIN, or Amazon Standart Item Number.
  77. * Each product sold at Amazon is identified by a different ASIN. ASINs are usually
  78. * different in different Amazon sites.
  79. * Although the product's ASIN in not usually changed when encoded, a decoded
  80. * version is returned.
  81. *
  82. * @return string|booleanThe product's ASIN. Or false if the ASIN is not set.
  83. */
  84. function getASIN(){
  85. if(!isset($this->asin))
  86. return false;
  87. else
  88. return rawurldecode($this->asin);
  89. }
  90.  
  91. /**
  92. * Decodes the product's name.
  93. *
  94. * @param integer $words the number of words you want to get, or 0 if you want
  95. * the complete title.
  96. * @param string $end the character(s) to be appended at the end of the title
  97. * in case the whole text is not returned.
  98. *
  99. * @return string|booleanThe product's name, the request part, or false if the name is not set.
  100. */
  101. function getProductName($words = 0, $end = "..."){
  102. if(!isset($this->productName))
  103. return false;
  104. else{
  105. if($words == 0)
  106. return rawurldecode($this->productName);
  107. else{
  108. $wordArray = preg_split("/[\s]/", rawurldecode($this->productName));
  109.  
  110. $res = "";
  111. $words = min($words, sizeof($wordArray));
  112. for($i = 0; $i < $words; $i++){
  113. $res .= $wordArray[$i];
  114. if($i != $words-1)
  115. $res .= " ";
  116. }
  117. if($words < sizeof($wordArray))
  118. $res .= $end;
  119. return $res;
  120. }
  121. }
  122. }
  123.  
  124. /**
  125. * The listed price of the item including its currency symbol.
  126. *
  127. * The function can be selective, in this case it will only return the price
  128. * if it exists and is different than $ourPrice.
  129. *
  130. * @param bool $selective if true, the price is only returned if it exists and
  131. * is different than $ourPrice
  132. *
  133. * @return string|booleanProduct's list price. Or false if none is set or it
  134. * is equal to $ourPrice.
  135. */
  136. function getListPrice($selective = true){
  137. if(isset($this->listPrice)){
  138. if(!$selective)
  139. return rawurldecode($this->listPrice);
  140. else{
  141. $list = rawurldecode($this->listPrice);
  142. $our = rawurldecode($this->ourPrice);
  143.  
  144. if($list != $our && $list != "EUR 0,00" && $list != "")
  145. return $list;
  146. else
  147. return false;
  148. }
  149. }
  150. else
  151. return false;
  152. }
  153.  
  154. /**
  155. * The price offered by Amazon for this product including its currency symbol.
  156. *
  157. * @return string|booleanAmazon's price for the product. Or false if none is set.
  158. */
  159. function getOurPrice(){
  160. if(isset($this->ourPrice))
  161. return rawurldecode($this->ourPrice);
  162. else
  163. return false;
  164. }
  165.  
  166. /**
  167. * Calculates the difference between Amazon's price and the list price.
  168. *
  169. * If the list price is not strictly greater than Amazon's price, savings
  170. * are not calculated (since there are none) and false is returned.
  171. *
  172. * If money is saved, the difference between Amazon's price and the list
  173. * price is returned including the currency symbol.
  174. *
  175. * @param boolean $selective if this is set to false, the difference is
  176. * calculated no matter what the value of the list price is. It is recommended
  177. * not to use this.
  178. *
  179. * @return string|booleanThe price difference including the currency symbol.
  180. * Or false if the listed price for this product is not strictly greater than
  181. * Amazon's price.
  182. *
  183. * @since version 0.2. Before this, $locale had to be provided as first argument.
  184. */
  185. function getSavings($selective = true){
  186. if(!$selective || $this->getListPrice()){
  187. $decimalSep = "";
  188. $cents = "";
  189. $thousandsSep = "";
  190. $res = "";
  191. $number = array();
  192.  
  193. //echo "<!-- ";
  194.  
  195. $symbol = preg_replace("/([^0-9]*)[0-9]*.*/", "$1", rawurldecode($this->listPrice));
  196. //echo "Symbol: " . $symbol . "<br>\n";
  197. $list = preg_replace("/([^0-9]*)/", "", rawurldecode($this->listPrice));
  198. //echo "List: " . $list . "<br>\n";
  199. $our = preg_replace("/([^0-9]*)/", "", rawurldecode($this->ourPrice));
  200. //echo "Our: " . $our . "<br>\n";
  201.  
  202. $savings = $list-$our;
  203. //echo "Savings: " . $savings . "<br>\n";
  204. if($savings <= 0 && $selective)
  205. return false;
  206.  
  207.  
  208. switch($this->locale){
  209. case "de":
  210. $thousandsSep = ".";
  211. break;
  212. default:
  213. $thousandsSep = ",";
  214. break;
  215. }
  216.  
  217. if($this->locale != "jp"){
  218. $cents = substr($savings,-2);
  219. $savings = floor($savings/100);
  220.  
  221. switch($this->locale){
  222. case "de":
  223. $decimalSep = ",";
  224. break;
  225. default:
  226. $decimalSep = ".";
  227. break;
  228. }
  229. }
  230.  
  231. while($savings != 0){
  232. array_push($number, substr($savings,-3));
  233. $savings = floor($savings/1000);
  234. }
  235.  
  236. while($temp = array_pop($number)){
  237. if(sizeof($number) > 0)
  238. $res .= $temp . $thousandsSep;
  239. else
  240. $res .= $temp;
  241. }
  242.  
  243. if($res == 0)
  244. $res = "0";
  245. if($cents == 0)
  246. $cents == "00";
  247.  
  248. $res = $symbol . $res . $decimalSep . $cents;
  249.  
  250. return $res;
  251. }
  252. else
  253. return false;
  254. }
  255. }
  256. ?>
 
Documentation generated on Sat, 21 Aug 2004 17:40:35 +0200 by phpDocumentor 1.3.0RC3
hosted by
SourceForge.net Logo