/** * @group utility */ function testIsEmpty() { $this->assertFalse(InputValidator::isEmpty("String")); $this->assertFalse(InputValidator::isEmpty(123)); $this->assertTrue(InputValidator::isEmpty("")); $this->assertTrue(InputValidator::isEmpty(null)); }
/** * Sets an occured error. * * @author David Pauli <*****@*****.**> * @param String $errorNumber The error number to set. * @return True if the error number is valid, false if not. * @since 0.1.2 */ private static function errorSet($errorNumber) { // if the parameter is empty or not a defined error number. if (InputValidator::isEmpty($errorNumber) || InputValidator::isEmptyArrayKey(self::$ERRORMESSAGES, $errorNumber)) { return false; } self::$ERROR = $errorNumber; return true; }
/** * 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"]; }
/** * Loads the order. * * @author David Pauli <*****@*****.**> * @since 0.1.3 * @since 0.2.1 Implement REST client fixes. */ private function load() { // if parameter is wrong or GET is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { self::errorSet("RESTC-9"); return; } RESTClient::sendWithLocalization(self::RESTPATH . "/" . $this->orderId, Locales::getLocale()); $content = RESTClient::getJSONContent(); // if respond is empty if (InputValidator::isEmpty($content)) { self::errorSet("OF-1"); return; } $this->parseData($content); // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); $this->NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::$NEXT_RESPONSE_WAIT_TIME; }
/** * This send function sends a special command to the REST server with additional parameter. * * @author David Pauli <*****@*****.**> * @param String command The path which is requested in the REST client. * @param String[] $postParameter Add specific parameters to the REST server. * @since 0.0.0 * @since 0.0.1 Use HTTPRequestMethod enum. * @since 0.1.0 Allow empty message body if the status code is 204. * @since 0.1.2 Restructure the logging message and fix the PATCH call. * @since 0.1.2 Add error reporting. * @since 0.1.3 Remove isRESTCommand function. * @since 0.2.1 Refactor the complete send method. */ public static function send($command = "", $postParameter = array()) { self::errorReset(); if (!InputValidator::isArray($postParameter)) { Logger::warning("ep6\\RESTClient\\Post parameter (" . $postParameter . ") are not valid."); self::errorSet("RESTC-5"); return null; } if (!self::$ISCONNECTED) { Logger::warning("ep6\\RESTClient\nClient is not connected."); self::errorSet("RESTC-6"); return null; } $protocol = self::$ISSSL ? "https" : "http"; $url = $protocol . "://" . self::$HOST . "/" . self::PATHTOREST . "/" . self::$SHOP . "/" . $command; $headers = array("Accept: " . self::HTTP_ACCEPT, "Content-Type: " . self::HTTP_CONTENT_TYPE_JSON, "User-Agent: " . self::USER_AGENT); // add authentification if there is a token if (InputValidator::isAuthToken(self::$AUTHTOKEN)) { array_push($headers, "Authorization: Bearer " . self::$AUTHTOKEN); } # parse cookies if (!InputValidator::isEmptyArray(self::$COOKIES)) { $cookiesValues = array(); foreach (self::$COOKIES as $key => $value) { array_push($cookiesValues, $key . "=" . $value); } array_push($headers, "Cookie: " . implode("; ", $cookiesValues)); } $curl = curl_init($url); curl_setopt($curl, CURLOPT_FAILONERROR, 1); // show full errors curl_setopt($curl, CURLOPT_FORBID_REUSE, 0); // connection can be opened curl_setopt($curl, CURLOPT_FRESH_CONNECT, 0); // no new connection required curl_setopt($curl, CURLOPT_NOBODY, 0); // show body curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // get response as string curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); // no connection timeout curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 0); // no connection timeout curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); // cURL will choose the http version curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER); // understand ipv4 and ipv6 curl_setopt($curl, CURLINFO_HEADER_OUT, 1); // save the header in the log curl_setopt($curl, CURLOPT_HEADER, 1); // get the header if (self::$ISSSL) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // don't check the peer ssl cerrificate curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTPS); curl_setopt($curl, CURLOPT_SSLVERSION, 0); // default ssl version } else { curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP); curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP); } switch (self::$HTTP_REQUEST_METHOD) { case HTTPRequestMethod::GET: curl_setopt($curl, CURLOPT_HTTPGET, 1); break; case HTTPRequestMethod::POST: $JSONpostfield = JSONHandler::createJSON($postParameter); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTREDIR, 0); // don't post on redirects curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; case HTTPRequestMethod::PUT: $JSONpostfield = JSONHandler::createJSON($postParameter); array_push($headers, "Content-Length: " . strlen($JSONpostfield)); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); break; case HTTPRequestMethod::DELETE: $JSONpostfield = JSONHandler::createJSON($postParameter); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; case HTTPRequestMethod::PATCH: $JSONpostfield = "[" . JSONHandler::createJSON($postParameter) . "]"; curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($curl, CURLOPT_POSTFIELDS, $JSONpostfield); break; } curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($curl); $info = curl_getinfo($curl); $error = curl_error($curl); curl_close($curl); # get header and body list($header, $body) = self::explodeResponse($response); $header = trim($header); $content = trim($body); $logMessage = "Request:\n" . "Parameters: " . http_build_query($postParameter) . "\n" . $info["request_header"] . "Response:\n" . "Size (Header/Request): " . $info["header_size"] . "/" . $info["request_size"] . " Bytes\n" . "Time (Total/Namelookup/Connect/Pretransfer/Starttransfer/Redirect): " . $info["total_time"] . " / " . $info["namelookup_time"] . " / " . $info["connect_time"] . " / " . $info["pretransfer_time"] . " / " . $info["starttransfer_time"] . " / " . $info["redirect_time"] . " seconds\n" . $response . "\n"; Logger::notify("ep6\\RESTClient:\n" . $logMessage); # parse header, response code and body self::explodeHeader($header); self::$HTTP_RESPONSE_CODE = (int) $info["http_code"]; if (!InputValidator::isEmpty($content)) { self::$CONTENT = $content; } }
/** * Gets the email. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a reload function. * @since 0.1.0 Use the default Locale. * @api * @return String|null The email or null if the email address is not set. */ public function getEmail() { self::reload(); return InputValidator::isEmpty(self::$EMAIL) ? null : self::$EMAIL; }
/** * Gets the description. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a reload function. * @since 0.1.0 Use the default Locale. * @api * @return String|null The localized description or null if the description is unset. */ public function getDescription() { self::reload($locale); return InputValidator::isEmpty(self::$DESCRIPTION) ? null : self::$DESCRIPTION; }
/** * This function checks whether a reload is needed. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @api */ private static function reload() { $timestamp = (int) (microtime(true) * 1000); // if the value is empty if (!InputValidator::isEmpty(self::$DEFAULT) && !InputValidator::isEmpty(self::$ITEMS) && self::$NEXT_REQUEST_TIMESTAMP > $timestamp) { return; } self::load(); }
/** * This function checks whether a reload is needed. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @since 0.1.1 Unstatic every attributes. */ private function reload() { $timestamp = (int) (microtime(true) * 1000); // if the value is empty if (!InputValidator::isEmpty($this->NAME) && !InputValidator::isEmpty($this->NAVIGATIONCAPTION) && !InputValidator::isEmpty($this->DESCRIPTION) && $this->NEXT_REQUEST_TIMESTAMP > $timestamp) { return; } $this->load(); }
/** * This function returns the parameter as string. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a default Locale and Currency. * @api * @return String The parameter build with this product filter. */ private function getParameter() { $parameter = array(); array_push($parameter, "locale=" . Locales::getLocale()); array_push($parameter, "currency=" . Currencies::getCurrency()); if (!InputValidator::isEmpty($this->page)) { array_push($parameter, "page=" . $this->page); } if (!InputValidator::isEmpty($this->resultsPerPage)) { array_push($parameter, "resultsPerPage=" . $this->resultsPerPage); } if (!InputValidator::isEmpty($this->direction)) { array_push($parameter, "direction=" . $this->direction); } if (!InputValidator::isEmpty($this->sort)) { array_push($parameter, "sort=" . $this->sort); } if (!InputValidator::isEmpty($this->q)) { array_push($parameter, "q=" . $this->q); } if (!InputValidator::isEmpty($this->categoryID)) { array_push($parameter, "categoryId=" . $this->categoryID); } foreach ($this->IDs as $number => $id) { array_push($parameter, "id=" . $id); } return implode("&", $parameter); }
/** * This function checks whether a reload is needed. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @since 0.1.1 Unstatic every attributes. */ private function reload() { $timestamp = (int) (microtime(true) * 1000); // if the value is empty if (!InputValidator::isEmpty($this->NAME) && !InputValidator::isEmpty($this->NAVIGATIONCAPTION) && !InputValidator::isEmpty($this->DESCRIPTION) && !InputValidator::isEmpty($this->TITLE) && !InputValidator::isEmpty($this->SHORTDESCRIPTION) && !InputValidator::isEmpty($this->COMPANY) && !InputValidator::isEmpty($this->CONTACTPERSON) && !InputValidator::isEmpty($this->CONTACTPERSONJOBTITLE) && !InputValidator::isEmpty($this->ADDRESS) && !InputValidator::isEmpty($this->PHONE) && !InputValidator::isEmpty($this->EMAIL) && $this->NEXT_REQUEST_TIMESTAMP > $timestamp) { return; } $this->load(); }
/** * This function prints warnings. * * @author David Pauli <*****@*****.**> * @param String $message The message to print. * @since 0.0.0 * @since 0.0.1 Use LogLevel * @since 0.1.2 Call the printMessage function on another way. */ public static function warning($message) { if (InputValidator::isEmpty($message) || self::$LOGLEVEL == LogLevel::ERROR || self::$LOGLEVEL == LogLevel::NONE) { return; } self::printMessage($message, true); }
/** * Gets the description. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a reload function. * @since 0.1.0 Use the default Locale. * @since 0.1.1 Unstatic every attributes. * @api * @return String|null The localized description or null if the description is unset. */ public function getDescription() { $this->reload($locale); return InputValidator::isEmpty($this->DESCRIPTION) ? null : $this->DESCRIPTION; }
/** * Loads the stock level. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @since 0.1.1 Unstatic every attributes. * @since 0.1.2 Add error reporting. */ private function loadStockLevel() { // if parameter is wrong or GET is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::GET)) { self::errorSet("RESTC-9"); return; } $content = RESTClient::send(self::RESTPATH . "/" . $this->productID . "/" . self::RESTPATH_STOCKLEVEL); // 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 finally prints the message. * * @param String message The message to print. * @param String level The message level. */ private static function printMessage($message, $level = "FORCE") { if (InputValidator::isEmpty($message)) { return; } switch (self::$OUT) { case "SCREEN": echo "********************" . $level . "********************<br/>\n"; if ($level == "ERROR") { echo "<strong>AN ERROR OCCURED:</strong><br/>\n"; } if ($level == "WARNING") { echo "<strong><u>WARNING:</u></strong> "; } if (is_array($message)) { echo "<pre>\n"; var_dump($message); echo "</pre><br/>\n"; } else { echo $message . "<br/>\n"; } break; } }
/** * Use this shop from now. * * @author David Pauli <*****@*****.**> * @since 0.1.1 * @api */ function useShop() { if (InputValidator::isEmpty($this->host) || InputValidator::isEmpty($this->shop)) { RESTClient::disconnect($this->host, $this->shop, $this->authToken, $this->isssl); } }
/** * Gets the email. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.1.0 Use a reload function. * @since 0.1.0 Use the default Locale. * @since 0.1.1 Unstatic every attributes. * @api * @return String|null The email or null if the email address is not set. */ public function getEmail() { $this->reload(); return InputValidator::isEmpty($this->EMAIL) ? null : $this->EMAIL; }
/** * 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 definitly prints the message. * * @author David Pauli <*****@*****.**> * @since 0.0.0 * @since 0.0.1 Use LogLevel * @api * @param String $message The message to print. */ public static function force($message) { if (InputValidator::isEmpty($message)) { return; } self::printMessage($message, LogLevel::FORCE); }
/** * Gets the number of available images. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @api * @return int The number of images. */ public function getCountImages() { if (InputValidator::isEmpty($this->images)) { return 0; } return sizeof($this->images); }
/** * This function checks whether a reload is needed. * * @author David Pauli <*****@*****.**> * @since 0.1.3 */ private function reload() { $timestamp = (int) (microtime(true) * 1000); // if the value is empty if (!InputValidator::isEmpty($this->name) && !InputValidator::isEmpty($this->slogan) && !InputValidator::isEmpty($this->logo) && !InputValidator::isEmpty($this->storefrontURL) && !InputValidator::isEmpty($this->backofficeURL) && $this->NEXT_REQUEST_TIMESTAMP > $timestamp) { return; } $this->load(); }
/** * Loads the stock level. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @param float $step The step to change. */ private function changeStockLevel($step) { // if parameter is wrong or GET is blocked if (!RESTClient::setRequestMethod(HTTPRequestMethod::PUT) || !InputValidator::isFloat($step)) { return; } $postfields = array("changeStocklevel" => $step); $content = RESTClient::send(self::$RESTPATH . "/" . $this->productID . "/" . self::$RESTPATH_STOCKLEVEL, $postfields); // if respond is empty if (InputValidator::isEmpty($content) || InputValidator::isEmptyArrayKey($content, "stocklevel")) { return; } $this->stockLevel = (double) $content["stocklevel"]; // update timestamp when make the next request $timestamp = (int) (microtime(true) * 1000); self::$NEXT_REQUEST_TIMESTAMP = $timestamp + RESTClient::NEXT_RESPONSE_WAIT_TIME; }
/** * This function returns the parameter as string. * * @author David Pauli <*****@*****.**> * @return String The parameter build with this order filter. * @since 0.1.3 * @since 0.2.1 Update build of parameter */ private function getParameter() { $parameter = array(); array_push($parameter, "locale=" . Locales::getLocale()); array_push($parameter, "currency=" . Currencies::getCurrency()); if (!InputValidator::isEmpty($this->createdAfterDate)) { array_push($parameter, "createdAfter=" . $this->createdAfterDate->asReadable()); } if (!InputValidator::isEmpty($this->createdBeforeDate)) { array_push($parameter, "createdBefore=" . $this->createdBeforeDate->asReadable()); } if (!InputValidator::isEmpty($this->customerId)) { array_push($parameter, "customerId=" . $this->customerId); } if (!InputValidator::isEmpty($this->isArchived)) { array_push($parameter, "archivedOn=" . $this->isArchived); } if (!InputValidator::isEmpty($this->isClosed)) { array_push($parameter, "closedOn=" . $this->isClosed); } if (!InputValidator::isEmpty($this->isDelivered)) { array_push($parameter, "deliveredOn=" . $this->isDelivered); } if (!InputValidator::isEmpty($this->isDispatched)) { array_push($parameter, "dispatchedOn=" . $this->isDispatched); } if (!InputValidator::isEmpty($this->isInvoiced)) { array_push($parameter, "invoicedOn=" . $this->isInvoiced); } if (!InputValidator::isEmpty($this->isPaid)) { array_push($parameter, "paidOn=" . $this->isPaid); } if (!InputValidator::isEmpty($this->isPending)) { array_push($parameter, "pendingOn=" . $this->isPending); } if (!InputValidator::isEmpty($this->isRejected)) { array_push($parameter, "rejectedOn=" . $this->isRejected); } if (!InputValidator::isEmpty($this->isReturned)) { array_push($parameter, "returnedOn=" . $this->isReturned); } if (!InputValidator::isEmpty($this->isViewed)) { array_push($parameter, "viewedOn=" . $this->isViewed); } if (!InputValidator::isEmpty($this->updatedFromDate)) { array_push($parameter, "updatedFrom=" . $this->updatedFromDate->asReadable()); } if (!InputValidator::isEmpty($this->page)) { array_push($parameter, "page=" . $this->page); } if (!InputValidator::isEmpty($this->productId)) { array_push($parameter, "productId=" . $this->productId); } if (!InputValidator::isEmpty($this->resultsPerPage)) { array_push($parameter, "resultsPerPage=" . $this->resultsPerPage); } if (!InputValidator::isEmpty($this->sortLastUpdate)) { array_push($parameter, "sortLastUpdate=" . $this->sortLastUpdate); } return implode("&", $parameter); }