/** * @group utility */ function testGetContentWithLocalization() { // GIVEN RESTClient::connect("sandbox.epages.com", "EpagesDevD20150929T075829R63"); // WHEN RESTClient::sendWithLocalization("legal", "de_DE"); // THEN $this->assertTrue(RESTClient::isResponseOk()); $this->assertNotNull(RESTClient::getContent()); $this->assertNotNull(RESTClient::getJSONContent()); }
/** * 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. * @since 0.2.1 Implement REST client fixes. */ 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)) { $error = InputValidator::isEmpty(self::RESTPATH) ? "TI-1" : "RESTC-9"; self::errorSet($error); return; } RESTClient::sendWithLocalization(self::RESTPATH, Locales::getLocale()); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { self::errorSet("TI-2"); 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; }
/** * 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. * @since 0.1.2 Add error reporting. * @since 0.2.1 Implement REST client fixes. */ private static function load() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { self::errorSet("RESTC-9"); return; } RESTClient::send(self::RESTPATH); $content = RESTClient::getJSONContent(); // 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."); self::errorSet("C-1"); 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; }
/** * 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); }
/** * 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); } } } }
/** * 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; } RESTClient::send(); $content = RESTClient::getJSONContent(); // 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->logoURL = 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; }
/** * 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. * @since 0.1.1 Unstatic every attributes. * @since 0.1.2 Add error reporting. * @since 0.2.1 Implement REST client fixes. */ private function load() { // if request method is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { self::errorSet("RESTC-9"); return; } RESTClient::sendWithLocalization(self::RESTPATH, Locales::getLocale()); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { self::errorSet("CI-1"); return; } // reset values $this->resetValues(); if (!InputValidator::isEmptyArrayKey($content, "name")) { $this->NAME = $content["name"]; } if (!InputValidator::isEmptyArrayKey($content, "title")) { $this->TITLE = $content["title"]; } if (!InputValidator::isEmptyArrayKey($content, "navigationCaption")) { $this->NAVIGATIONCAPTION = $content["navigationCaption"]; } if (!InputValidator::isEmptyArrayKey($content, "shortDescription")) { $this->SHORTDESCRIPTION = $content["shortDescription"]; } if (!InputValidator::isEmptyArrayKey($content, "description")) { $this->DESCRIPTION = $content["description"]; } if (!InputValidator::isEmptyArrayKey($content, "company")) { $this->COMPANY = $content["company"]; } if (!InputValidator::isEmptyArrayKey($content, "contactPerson")) { $this->CONTACTPERSON = $content["contactPerson"]; } if (!InputValidator::isEmptyArrayKey($content, "contactPersonJobTitle")) { $this->CONTACTPERSONJOBTITLE = $content["contactPersonJobTitle"]; } if (!InputValidator::isEmptyArrayKey($content, "address")) { $this->ADDRESS = $content["address"]; } if (!InputValidator::isEmptyArrayKey($content, "phone")) { $this->PHONE = $content["phone"]; } if (!InputValidator::isEmptyArrayKey($content, "email")) { $this->EMAIL = $content["email"]; } // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); $this->NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::$NEXT_RESPONSE_WAIT_TIME; }
/** * Change the stock level with a step. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @since 0.1.1 Unstatic every attributes. * @since 0.1.2 Add error reporting. * @param float $step The step to change. */ private function changeStockLevel($step) { // if parameter is wrong or GET is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::PUT)) { self::errorSet("RESTC-9"); return; } if (!InputValidator::isFloat($step)) { self::errorSet("P-8"); Logger::error("The " . $step . " step to change the stocklevel is no float."); return; } $postfields = array("changeStocklevel" => $step); RESTClient::send(self::RESTPATH . "/" . $this->productID . "/" . self::RESTPATH_STOCKLEVEL, $postfields); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { self::errorSet("P-6"); return; } // if there are no items if (InputValidator::isEmptyArrayKey($content, "stocklevel")) { self::errorSet("P-7"); Logger::error("Respond for " . self::RESTPATH . "/" . $this->productID . "/" . self::RESTPATH_STOCKLEVEL . " can not be interpreted."); return; } $this->stockLevel = (double) $content["stocklevel"]; // 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; }
/** * 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; }