/** * Gets the default and possible currencies of the shop. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use HTTPRequestMethod enum * @since 0.1.0 Save timestamp of the last request. * @since 0.1.0 Add configured used Currency. * @api */ private static function load() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::send(self::RESTPATH); // if respond is empty or there are no default AND items element if (InputValidator::isEmptyArrayKey($content, "default") || InputValidator::isEmptyArrayKey($content, "items")) { Logger::error("Respond for " . self::RESTPATH . " can not be interpreted."); return; } // reset values self::resetValues(); // save the default currency self::$DEFAULT = $content["default"]; // parse the possible currencies self::$ITEMS = $content["items"]; // set the configured shop Locale if it is empty. if (InputValidator::isEmpty(self::$USED)) { self::$USED = $content["default"]; } // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); self::$NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::$NEXT_RESPONSE_WAIT_TIME; }
/** * Reload the REST information. * This is only a empty placeholder. The child class can override it. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.0.1 Use HTTPRequestMethod enum. * @since 0.1.0 Use a default Locale. * @since 0.1.1 Unstatic every attributes. */ private function load() { // if the REST path empty -> this is the not the implementation or can't get something else if (InputValidator::isEmpty(self::RESTPATH) || !RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::sendWithLocalization(self::RESTPATH, Locales::getLocale()); // if respond is empty if (InputValidator::isEmpty($content)) { return; } // reset values $this->resetValues(); if (!InputValidator::isEmptyArrayKey($content, "name")) { $this->NAME = $content["name"]; } if (!InputValidator::isEmptyArrayKey($content, "navigationCaption")) { $this->NAVIGATIONCAPTION = $content["navigationCaption"]; } if (!InputValidator::isEmptyArrayKey($content, "description")) { $this->DESCRIPTION = $content["description"]; } // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); $this->NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::$NEXT_RESPONSE_WAIT_TIME; }
/** * This function gets the product images. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @since 0.1.1 Fix bug with nonsetted product URL and delete reload functionality. * @since 0.1.1 Use unstatic variables. * @api * @param String $productID The product ID to get images. */ private function load($productID) { // if parameter is wrong or GET is blocked if (!InputValidator::isProductId($productID) || !RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::send("products/" . $productID . "/" . self::RESTPATH); // if respond is empty if (InputValidator::isEmpty($content)) { return; } // if there is items if (InputValidator::isEmptyArrayKey($content, "items")) { Logger::error("Respond for product/" . $productID . "/" . self::RESTPATH . " can not be interpreted."); return; } // is there any images found: load the images. foreach ($content['items'] as $number => $image) { // parse every image size if (!InputValidator::isEmptyArrayKey($image, "sizes")) { $object = null; foreach ($image["sizes"] as $size) { // if there is "url" and "classifier" set in the image if (!InputValidator::isEmptyArrayKey($size, "url") && !InputValidator::isEmptyArrayKey($size, "classifier")) { $object[$size["classifier"]] = $size["url"]; } } // if all needed sizes are available, save it if (!InputValidator::isEmptyArrayKey($object, "Thumbnail") && !InputValidator::isEmptyArrayKey($object, "Small") && !InputValidator::isEmptyArrayKey($object, "HotDeal") && !InputValidator::isEmptyArrayKey($object, "MediumSmall") && !InputValidator::isEmptyArrayKey($object, "Medium") && !InputValidator::isEmptyArrayKey($object, "MediumLarge") && !InputValidator::isEmptyArrayKey($object, "Large")) { array_push($this->images, $object); } } } }
/** * @group utility */ function testSend() { @RESTClient::setRequestMethod(); @RESTClient::connect("www.google.de", "Shopname", "AuthToken"); $this->assertNull(@RESTClient::send("locale")); @RESTClient::setRequestMethod("NOTVALID"); $this->assertNull(@RESTClient::send("locale")); @RESTClient::disconnect(); @RESTClient::setRequestMethod("GET"); $this->assertNull(@RESTClient::send("locale")); @RESTClient::setRequestMethod("NOTVALID"); $this->assertNull(@RESTClient::send("locale")); }
/** * Reload the REST information. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.0.1 Use HTTPRequestMethod enum. * @since 0.1.0 Use a default Locale. */ private static function load() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::sendWithLocalization(self::$RESTPATH, Locales::getLocale()); // if respond is empty if (InputValidator::isEmpty($content)) { return; } // reset values self::resetValues(); if (!InputValidator::isEmptyArrayKey($content, "name")) { self::$NAME = $content["name"]; } if (!InputValidator::isEmptyArrayKey($content, "title")) { self::$TITLE = $content["title"]; } if (!InputValidator::isEmptyArrayKey($content, "navigationCaption")) { self::$NAVIGATIONCAPTION = $content["navigationCaption"]; } if (!InputValidator::isEmptyArrayKey($content, "shortDescription")) { self::$SHORTDESCRIPTION = $content["shortDescription"]; } if (!InputValidator::isEmptyArrayKey($content, "description")) { self::$DESCRIPTION = $content["description"]; } if (!InputValidator::isEmptyArrayKey($content, "company")) { self::$COMPANY = $content["company"]; } if (!InputValidator::isEmptyArrayKey($content, "contactPerson")) { self::$CONTACTPERSON = $content["contactPerson"]; } if (!InputValidator::isEmptyArrayKey($content, "contactPersonJobTitle")) { self::$CONTACTPERSONJOBTITLE = $content["contactPersonJobTitle"]; } if (!InputValidator::isEmptyArrayKey($content, "address")) { self::$ADDRESS = $content["address"]; } if (!InputValidator::isEmptyArrayKey($content, "phone")) { self::$PHONE = $content["phone"]; } if (!InputValidator::isEmptyArrayKey($content, "email")) { self::$EMAIL = $content["email"]; } // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); self::$NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::NEXT_RESPONSE_WAIT_TIME; }
/** * Gets the default and possible locales of the shop. */ private static function load() { // if request method is blocked if (!RESTClient::setRequestMethod("GET")) { return; } $content = JSONParser::parseJSON(RESTClient::send(self::RESTPATH)); // if respond is empty if (InputValidator::isEmpty($content)) { return; } // if there is no default AND items element if (!array_key_exists("default", $content) || !array_key_exists("items", $content)) { Logger::error("Respond for " . self::RESTPATH . " can not be interpreted."); return; } // reset values self::resetValues(); // save the default localization self::$DEFAULT = $content["default"]; // parse the possible localizations self::$ITEMS = $content["items"]; }
/** * @group utility */ function testSendWithLocalization() { RESTClient::connect("sandbox.epages.com", "EpagesDevD20150929T075829R63", "icgToyl45PKhmkz6E2PUQOriaCoE5Wzq", true); RESTClient::setRequestMethod("GET"); $this->assertNull(RESTClient::sendWithLocalization("locale", "NoLocale")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-3", RESTClient::errorNumber()); $this->assertNull(RESTClient::sendWithLocalization("locales", "NoLocale", "NoArray")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-3", RESTClient::errorNumber()); $this->assertNull(RESTClient::sendWithLocalization("locales", "en_GB", "NoArray")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-5", RESTClient::errorNumber()); RESTClient::disconnect(); $this->assertNull(RESTClient::sendWithLocalization("locales", "NoLocale")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-3", RESTClient::errorNumber()); RESTClient::disconnect(); $this->assertNull(RESTClient::sendWithLocalization("locales", "en_GB")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-6", RESTClient::errorNumber()); RESTClient::connect("sandbox.epages.com", "EpagesDevD20150929T075829R63", "icgToyl45PKhmkz6E2PUQOriaCoE5Wzq", true); RESTClient::setRequestMethod("GET"); $this->assertNull(RESTClient::sendWithLocalization("NoValidRessource", "en_GB")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-7", RESTClient::errorNumber()); $this->assertNull(RESTClient::sendWithLocalization("NoValidRessource", "NoLocale")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-3", RESTClient::errorNumber()); $this->assertNull(RESTClient::sendWithLocalization("locales", "de_DE", "noArray")); $this->assertTrue(RESTClient::error()); $this->assertEquals("RESTC-5", RESTClient::errorNumber()); $this->assertNotNull(RESTClient::sendWithLocalization("locales", "de_DE")); $this->assertFalse(RESTClient::error()); $this->assertNull(RESTClient::errorNumber()); }
/** * Deletes itself. * * Dont use this function. To delete a product its better to use $shop->deleteProduct($product). * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @return boolean True if the deletion was successful, false if not. */ public function delete() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::DELETE)) { return false; } RESTClient::send(self::$RESTPATH . "/" . $this->productID); return true; }
/** * This function gets the product images. * * @author David Pauli <*****@*****.**> * @param String $productID The product ID to get images. * @since 0.1.0 * @since 0.1.1 Fix bug with nonsetted product URL and delete reload functionality. * @since 0.1.1 Use unstatic variables. * @since 0.1.2 Add error reporting. * @since 0.2.1 Implement REST client fixes. */ private function load($productID) { // if parameter is wrong if (!InputValidator::isProductId($productID)) { $this->errorSet("PS-1"); Logger::warning("ep6\\ProductSlideshow\nInvalid product ID " . $productId . " to load slideshow."); return; } // if GET is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { $this->errorSet("RESTC-9"); return; } RESTClient::send("products/" . $productID . "/" . self::RESTPATH); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { $this->errorSet("PS-2"); Logger::warning("ep6\\ProductSlideshow\nEmpty response while getting product slideshow."); return; } // if there is items if (InputValidator::isEmptyArrayKey($content, "items")) { $this->errorSet("PS-3"); Logger::error("Respond for product slidehows can not be interpreted."); return; } // is there any images found: load the images. foreach ($content['items'] as $number => $image) { // parse every image size if (!InputValidator::isEmptyArrayKey($image, "sizes")) { $object = null; foreach ($image["sizes"] as $size) { // if there is "url" and "classifier" set in the image if (!InputValidator::isEmptyArrayKey($size, "url") && !InputValidator::isEmptyArrayKey($size, "classifier")) { $object[$size["classifier"]] = $size["url"]; } } // if all needed sizes are available, save it if (!InputValidator::isEmptyArrayKey($object, "Thumbnail") && !InputValidator::isEmptyArrayKey($object, "Small") && !InputValidator::isEmptyArrayKey($object, "HotDeal") && !InputValidator::isEmptyArrayKey($object, "MediumSmall") && !InputValidator::isEmptyArrayKey($object, "Medium") && !InputValidator::isEmptyArrayKey($object, "MediumLarge") && !InputValidator::isEmptyArrayKey($object, "Large")) { array_push($this->images, $object); } } } }
/** * This function returns the products by using the product filter. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a default Locale. * @since 0.1.1 Unstatic every attributes. * @api * @return Products[] Returns an array of products. */ public function getProducts() { $parameter = $this->getParameter(); // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::send(self::RESTPATH . "?" . $parameter); // if respond is empty if (InputValidator::isEmpty($content)) { return; } // if there is no results, page AND resultsPerPage element if (InputValidator::isEmptyArrayKey($content, "results") || InputValidator::isEmptyArrayKey($content, "page") || InputValidator::isEmptyArrayKey($content, "resultsPerPage")) { Logger::error("Respond for " . self::RESTPATH . " can not be interpreted."); return; } $products = array(); // is there any product found: load the products. if (!InputValidator::isEmptyArrayKey($content, "items") && sizeof($content['items']) != 0) { foreach ($content['items'] as $item) { $product = new Product($item); array_push($products, $product); } } return $products; }
/** * Unsets an attribute value of the order. * * @author David Pauli <*****@*****.**> * @param String $path The path to this attribute. * @since 0.2.0 * @since 0.2.1 Implement REST client fixes. */ private function unsetAttribute($path) { // if PATCH does not work if (!RESTClient::setRequestMethod("PATCH")) { self::errorSet("RESTC-9"); return; } $parameter = array("op" => "remove", "path" => $path); RESTClient::send(self::RESTPATH, $parameter); $orderParameter = RESTClient::getJSONContent(); // update the product $this->parseData($orderParameter); }
/** * Loads the shop data. * * @author David Pauli <*****@*****.**> * @param Array $product The product in an array. * @since 0.1.3 */ private function load() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { return; } $content = RESTClient::send(); // if respond has no name, slogan, logoUrl, sfUrl and mboUrl if (InputValidator::isExistsArrayKey($content, "name") || InputValidator::isExistsArrayKey($content, "slogan") || InputValidator::isExistsArrayKey($content, "logoUrl") || InputValidator::isExistsArrayKey($content, "sfUrl") || InputValidator::isExistsArrayKey($content, "mboUrl")) { Logger::error("Respond for " . $this->shop . " can not be interpreted."); self::errorSet("C-1"); return; } // reset values $this->resetValues(); // save the attributes $this->name = $content['name']; $this->slogan = $content['slogan']; $this->logo = new Image($content['logoUrl']); $this->storefrontURL = new URL($content['sfUrl']); $this->backofficeURL = new URL($content['mboUrl']); // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); $this->NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::$NEXT_RESPONSE_WAIT_TIME; }
/** * This function returns the products by using the product filter. * * @author David Pauli <*****@*****.**> * @return Product[] Returns an array of products. * @since 0.0.0 * @since 0.1.0 Use a default Locale. * @since 0.1.1 Unstatic every attributes. * @since 0.1.2 Add error reporting. * @since 0.1.3 Get all results. * @since 0.2.0 Set error message for empty responses to notify. * @since 0.2.1 Implement REST client fixes. */ public function getProducts() { $this->errorReset(); $parameter = $this->getParameter(); // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { $this->errorSet("RESTC-9"); return; } RESTClient::send(self::RESTPATH . "?" . $parameter); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { $this->errorSet("PF-8"); Logger::notify("ep6\\ProductFilter\nREST respomd for getting products is empty."); return; } // if there is no results, page AND resultsPerPage element if (InputValidator::isEmptyArrayKey($content, "results") || InputValidator::isEmptyArrayKey($content, "page") || InputValidator::isEmptyArrayKey($content, "resultsPerPage")) { $this->errorSet("PF-9"); Logger::error("ep6\\ProductFilter\nRespond for " . self::RESTPATH . " can not be interpreted."); return; } $this->results = $content['results']; $products = array(); // is there any product found: load the products. if (!InputValidator::isEmptyArrayKey($content, "items") && sizeof($content['items']) != 0) { foreach ($content['items'] as $item) { $product = new Product($item); // go to every filter foreach ($this->filters as $filter) { switch ($filter->getAttribute()) { case 'stocklevel': $value = array(); $value["stocklevel"] = $product->getStocklevel(); break; case 'price': $value = array(); $value["price"] = $product->getPrice()->getAmount(); break; case 'category': $value = array(); $value["category"] = $product->getCategories(); break; default: $value = $item; break; } if (!InputValidator::isEmptyArrayKey($value, $filter->getAttribute()) || $filter->getOperator() == FilterOperation::UNDEF) { if (!InputValidator::isArray($value[$filter->getAttribute()])) { if (!$filter->isElementInFilter($value)) { continue 2; } } } else { continue 2; } } array_push($products, $product); } } return $products; }
/** * Unsets the amount of a product price. * * @author David Pauli <*****@*****.**> * @since 0.1.2 */ public function unsetAmount() { $this->errorReset(); $allowedTypes = array(ProductPriceTypes::PRICE, ProductPriceTypes::MANUFACTURER, ProductPriceTypes::ECOPARTICIPATION, ProductPriceTypes::DEPOSIT); // if PATCH does not work if (!RESTClient::setRequestMethod("PATCH")) { $this->errorSet("RESTC-9"); return; } // if this operation is not allowed for this price type if (InputValidator::isEmptyArrayKey($allowedTypes, $this->type)) { $this->errorSet("PP-3"); Logger::warning("ep6\\ProductPrice\nChanging product price is not allowed for this " . $this->type . " product price method."); return; } $parameter = array("op" => "remove", "path" => "/priceInfo/" . $this->type . "/amount"); RESTClient::send("product/" . $this->productID, $parameter); }
/** * This function returns the orders by using the order filter. * * @author David Pauli <*****@*****.**> * @return Order[] Returns an array of orders. * @since 0.1.3 * @since 0.2.0 Set error message for empty responses to notify. * @since 0.2.1 Implement REST client fixes. */ public function getOrders() { $this->errorReset(); $parameter = $this->getParameter(); // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { $this->errorSet("RESTC-9"); return; } RESTClient::send(self::RESTPATH . "?" . $parameter); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { $this->errorSet("OF-1"); Logger::notify("ep6\\OrderFilter\nREST respond for getting orders is empty."); return; } // if there is no results, page AND resultsPerPage element if (InputValidator::isEmptyArrayKey($content, "results") || InputValidator::isEmptyArrayKey($content, "page") || InputValidator::isEmptyArrayKey($content, "resultsPerPage")) { $this->errorSet("OF-2"); Logger::error("ep6\\OrderFilter\nRespond for " . self::RESTPATH . " can not be interpreted."); return; } $this->results = $content['results']; $orders = array(); // is there any order found: load the products. if (!InputValidator::isEmptyArrayKey($content, "items") && sizeof($content['items']) != 0) { foreach ($content['items'] as $item) { $order = new Order($item); // go to every filter foreach ($this->filters as $filter) { if (!InputValidator::isEmptyArrayKey($item, $filter->getAttribute()) || $filter->getOperator() == FilterOperation::UNDEF) { if (!InputValidator::isArray($item[$filter->getAttribute()])) { if (!$filter->isElementInFilter($item)) { continue 2; } } } else { continue 2; } } array_push($orders, $order); } } return $orders; }