/** * Fill the product filter with a array. * * @author David Pauli <*****@*****.**> * @since 0.0.1 * @since 0.1.0 Use a default Locale and Currency. * @api * @param String[] $productFilterParameter The values of a product filter. */ public function setProductFilter($productFilterParameter) { if (!InputValidator::isArray($productFilterParameter) || InputValidator::isEmptyArray($productFilterParameter)) { return; } foreach ($productFilterParameter as $key => $parameter) { if ($key == "page") { $this->setPage($parameter); } else { if ($key == "resultsPerPage") { $this->setResultsPerPage($parameter); } else { if ($key == "direction") { $this->setDirection($parameter); } else { if ($key == "sort") { $this->setSort($parameter); } else { if ($key == "q") { $this->setQ($parameter); } else { if ($key == "categoryID") { $this->setCategoryID($parameter); } else { Logger::warning("Unknown attribute <i>" . $key . "</i> in product filter attribute."); } } } } } } } }
/** * Returns the product attributes. * * @author David Pauli <*****@*****.**> * @since 0.1.0 * @api * @return ProductAttributes[] Gets the product attributes in an array. */ public function getAttributes() { $timestamp = (int) (microtime(true) * 1000); // if the attribute is not loaded until now if (InputValidator::isEmptyArray($this->attributes) || self::$NEXT_REQUEST_TIMESTAMP < $timestamp) { $this->loadAttributes(); } return $this->attributes; }
/** * @group utility */ function testIsEmptyArray() { $this->assertFalse(InputValidator::isEmptyArray("String")); $this->assertFalse(InputValidator::isEmptyArray(array(1, 2, 3))); $this->assertTrue(InputValidator::isEmptyArray(array())); }
/** * 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; } }
/** * Fill the product filter with an array. * * @author David Pauli <*****@*****.**> * @param mixed[] $productFilterParameter The Product Filter Parameter as an array. * @since 0.0.1 * @since 0.1.0 Use a default Locale and Currency. * @since 0.1.2 Add error reporting. */ public function setProductFilter($productFilterParameter) { $this->errorReset(); if (!InputValidator::isArray($productFilterParameter) || InputValidator::isEmptyArray($productFilterParameter)) { $this->errorSet("PF-1"); Logger::warning("ep6\\ProductFilter\nProduct filter parameter " . $productFilterParameter . " to create product filter is invalid."); return; } foreach ($productFilterParameter as $key => $parameter) { if ($key == "page") { $this->setPage($parameter); } else { if ($key == "resultsPerPage") { $this->setResultsPerPage($parameter); } else { if ($key == "direction") { $this->setDirection($parameter); } else { if ($key == "sort") { $this->setSort($parameter); } else { if ($key == "q") { $this->setQ($parameter); } else { if ($key == "categoryID") { $this->setCategoryID($parameter); } else { $this->errorSet("PF-2"); Logger::warning("ep6\\ProductFilter\nUnknown attribute <i>" . $key . "</i> in product filter attribute."); } } } } } } } }