diff --git a/src/FetchApp/API/FetchApp.php b/src/FetchApp/API/FetchApp.php index f06d85f..6233869 100644 --- a/src/FetchApp/API/FetchApp.php +++ b/src/FetchApp/API/FetchApp.php @@ -222,23 +222,7 @@ public function getProducts($itemsPerPage = -1, $pageNumber = -1) $results = APIWrapper::makeRequest($requestURL, "GET"); if (is_a($results, "SimpleXMLElement")) { foreach ($results->product as $product) { - $tempProduct = new Product(); - $tempProduct->setProductID($product->id); - $tempProduct->setSKU($product->sku); - $tempProduct->setName($product->name); - $tempProduct->setDescription($product->description); - $tempProduct->setPrice($product->price); - $tempProduct->setCurrency(Currency::getValue($product->currency)); - $tempProduct->setOrderCount($product->order_count); - $tempProduct->setDownloadCount($product->download_count); - $tempProduct->setPaypalAddToCartLink($product->paypal_add_to_cart_link['href']); - $tempProduct->setPaypalBuyNowLink($product->paypal_buy_now_link['href']); - $tempProduct->setPaypalViewCartLink($product->paypal_view_cart_link['href']); - $tempProduct->setCreationDate(new \DateTime($product->created_at)); - $tempProduct->setFilesUri($product->files_uri); - $tempProduct->setDownloadsUri($product->downloads_uri); - - $products[] = $tempProduct; + $products[] = Product::fromXML($product); } } return $products; @@ -254,22 +238,9 @@ public function getProduct($productID) $requestURL = "https://app.fetchapp.com/api/v2/products/" . $productID; $product = APIWrapper::makeRequest($requestURL, "GET"); if (is_a($product, "SimpleXMLElement")) { - $tempProduct = new Product(); - $tempProduct->setProductID($product->id); - $tempProduct->setSKU($product->sku); - $tempProduct->setName($product->name); - $tempProduct->setPrice($product->price); - $tempProduct->setCurrency(Currency::getValue($product->currency)); - $tempProduct->setOrderCount($product->order_count); - $tempProduct->setDownloadCount($product->download_count); - $tempProduct->setPaypalAddToCartLink($product->paypal_add_to_cart_link['href']); - $tempProduct->setPaypalBuyNowLink($product->paypal_buy_now_link['href']); - $tempProduct->setPaypalViewCartLink($product->paypal_view_cart_link['href']); - $tempProduct->setCreationDate(new \DateTime($product->created_at)); - $tempProduct->setFilesUri($product->files_uri); - $tempProduct->setDownloadsUri($product->downloads_uri); + return Product::fromXML($product); } - return $tempProduct; + return false; } /** diff --git a/src/FetchApp/API/Product.php b/src/FetchApp/API/Product.php index bf81cbb..30a20a9 100644 --- a/src/FetchApp/API/Product.php +++ b/src/FetchApp/API/Product.php @@ -253,10 +253,11 @@ public function getCurrency() * @param array $files * @return mixed */ - public function create(array $files) + public function create(array $files=[], array $item_urls=[]) { APIWrapper::verifyReadiness(); $this->files = $files; + $this->item_urls = $item_urls; $url = "https://app.fetchapp.com/api/v2/products/create"; $data = $this->toXML(); @@ -264,18 +265,7 @@ public function create(array $files) $response = APIWrapper::makeRequest($url, "POST", $data); if (isset($response->id)) { - $this->setProductID($response->id); - $this->setSKU($response->sku); - $this->setName($response->name); - $this->setPrice($response->price); - $this->setOrderCount($response->order_count); - $this->setDownloadCount($response->download_count); - $this->setPaypalAddToCartLink($response->paypal_add_to_cart_link); - $this->setPaypalBuyNowLink($response->paypal_buy_now_link); - $this->setPaypalViewCartLink($response->paypal_view_cart_link); - $this->setCreationDate(new \DateTime($response->created_at)); - $this->setFilesUri($response->files_uri); - $this->setDownloadsUri($response->downloads_uri); + self::fromXML($response, $this); return true; } else { // It failed, let's return the error @@ -287,9 +277,10 @@ public function create(array $files) * @param array $files * @return mixed */ - public function update(array $item_urls) + public function update(array $files=[], array $item_urls=[]) { APIWrapper::verifyReadiness(); + $this->files = $files; $this->item_urls = $item_urls; $url = "https://app.fetchapp.com/api/v2/products/" . $this->ProductID . "/update"; @@ -297,18 +288,7 @@ public function update(array $item_urls) $response = APIWrapper::makeRequest($url, "PUT", $data); if (isset($response->id)) { - $this->setProductID($response->id); - $this->setSKU($response->sku); - $this->setName($response->name); - $this->setPrice($response->price); - $this->setOrderCount($response->order_count); - $this->setDownloadCount($response->download_count); - $this->setPaypalAddToCartLink($response->paypal_add_to_cart_link); - $this->setPaypalBuyNowLink($response->paypal_buy_now_link); - $this->setPaypalViewCartLink($response->paypal_view_cart_link); - $this->setCreationDate(new \DateTime($response->created_at)); - $this->setFilesUri($response->files_uri); - $this->setDownloadsUri($response->downloads_uri); + self::fromXML($response, $this); return true; } else { // It failed, let's return the error @@ -431,4 +411,34 @@ public function toXML($sendEmailFlag = true) return $productXML->asXML(); } + + public static function fromXML($productXML, Product $productToUpdate = null) + { + if(!$productXML instanceof \SimpleXMLElement){ + return false; + } + + if($productToUpdate){ + $product = $productToUpdate; + } else { + $product = new Product(); + } + + $product->setProductID((string)$productXML->id); + $product->setSKU((string)$productXML->sku); + $product->setName((string)$productXML->name); + $product->setDescription((string)$productXML->description); + $product->setPrice((float)$productXML->price); + $product->setCurrency(Currency::getValue($productXML->currency)); + $product->setOrderCount((int)$productXML->order_count); + $product->setDownloadCount((int)$productXML->download_count); + $product->setPaypalAddToCartLink((string)$productXML->paypal_add_to_cart_link['href']); + $product->setPaypalBuyNowLink((string)$productXML->paypal_buy_now_link['href']); + $product->setPaypalViewCartLink((string)$productXML->paypal_view_cart_link['href']); + $product->setCreationDate(new \DateTime($productXML->created_at)); + $product->setFilesUri((string)$productXML->files_uri); + $product->setDownloadsUri((string)$productXML->downloads_uri); + + return $product; + } }