From 47601ab14684a58b4ab100fa50a44478a28005cf Mon Sep 17 00:00:00 2001 From: Cornel Bratu Date: Wed, 13 Mar 2019 14:31:13 +0200 Subject: [PATCH 01/24] Added Encryption and Decryption Classes for Customers API --- composer.json | 3 +- lib/Api/Customers.php | 18 +++++ lib/Api/readme.md | 51 ++++++++++++++ lib/Exceptions/DecryptException.php | 18 +++++ lib/Helpers/Decryption.php | 102 ++++++++++++++++++++++++++++ lib/Helpers/Encryption.php | 93 +++++++++++++++++++++++++ lib/Helpers/Nonce.php | 87 ++++++++++++++++++++++++ lib/Helpers/Token.php | 43 ++++++++++++ tests/Unit/ApiCustomersTest.php | 37 ++++++++++ 9 files changed, 451 insertions(+), 1 deletion(-) create mode 100644 lib/Api/Customers.php create mode 100644 lib/Api/readme.md create mode 100644 lib/Exceptions/DecryptException.php create mode 100644 lib/Helpers/Decryption.php create mode 100644 lib/Helpers/Encryption.php create mode 100644 lib/Helpers/Nonce.php create mode 100644 lib/Helpers/Token.php create mode 100644 tests/Unit/ApiCustomersTest.php diff --git a/composer.json b/composer.json index b8e91f4..50e0e35 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ ], "require": { "php": ">=7.0.0", - "ext-json": "*" + "ext-json": "*", + "ext-openssl": "*" }, "autoload": { "psr-4": { diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php new file mode 100644 index 0000000..4108ef0 --- /dev/null +++ b/lib/Api/Customers.php @@ -0,0 +1,18 @@ +hash = hash(self::HASH_ALGORITHM, self::TOKEN); + } + + /** + * @param $token + * @param $nonce + * @return string + * @throws DecryptException + */ + public function decrypt($token, $nonce = null) + { + $data = $this->tokenDecrypt($token, $this->hash, self::METHOD); + + if ($nonce && !$this->verifyNonce($nonce)) { + throw new DecryptException('Invalid Nonce!'); + } + + return $data; + } + + private function tokenDecrypt($data, $key, $method) + { + $data = base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); + $ivSize = openssl_cipher_iv_length($method); + $iv = substr($data, 0, $ivSize); + $data = openssl_decrypt(substr($data, $ivSize), $method, $key, OPENSSL_RAW_DATA, $iv); + + return $data; + } + + /** + * @param $nonce + * @param int $clockSkew + * @return bool + */ + private function verifyNonce($nonce, $clockSkew = 60) + { + $nonce = base64_decode( + str_pad( + strtr($nonce, '-_', '+/'), + strlen($nonce) % 4, + '=', + STR_PAD_RIGHT + ) + ); + + if (strlen($nonce) > 255) { + return false; + } + + $result = preg_match('/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/', + $nonce, + $matches); + if ($result != 1 || count($matches) != 8) { + return false; + } + + $stamp = gmmktime($matches[4], + $matches[5], + $matches[6], + $matches[2], + $matches[3], + $matches[1]); + + $time = time(); + if ($stamp < ($time - $clockSkew) + || $stamp > ($time + $clockSkew)) { + + return false; + } + + return true; + } + +} diff --git a/lib/Helpers/Encryption.php b/lib/Helpers/Encryption.php new file mode 100644 index 0000000..d97b085 --- /dev/null +++ b/lib/Helpers/Encryption.php @@ -0,0 +1,93 @@ +customer = $customer; + $this->token = $token; + } + + /** + * @param $data + * @return string + */ + public static function encrypt($data) + { + $ivSize = openssl_cipher_iv_length(self::METHOD); + $iv = openssl_random_pseudo_bytes($ivSize); + + $encrypted = openssl_encrypt($data, self::METHOD, self::createKey(), OPENSSL_RAW_DATA, $iv); + + + return rtrim(strtr(base64_encode($iv . $encrypted), '+/', '-_'), '='); + } + + /** + * @return string + */ + private static function createKey() + { + return hash('sha512', self::TOKEN); + } + + /** + * @return mixed + */ + public function decodeCustomer() + { + return json_decode($this->customer); + } + + /** + * @param int $length + * @param null $time + * @return string + */ + private function generateNonce($length = 12, $time = null) + { + $time = ($time === null) ? time() : $time; + $nonce = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); + + if ($length < 1) { + return $nonce; + } + + $length = (int)$length; + $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; + + $unique = ''; + for ($i = 0; $i < $length; $i++) { + $unique .= substr($chars, (rand() % (strlen($chars))), 1); + } + + return rtrim(strtr(base64_encode($nonce . $unique), '+/', '-_'), '='); + } + +} diff --git a/lib/Helpers/Nonce.php b/lib/Helpers/Nonce.php new file mode 100644 index 0000000..049ed04 --- /dev/null +++ b/lib/Helpers/Nonce.php @@ -0,0 +1,87 @@ + 255) { + return false; + } + + $result = preg_match('/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/', + $nonce, + $matches); + if ($result != 1 || count($matches) != 8) { + return false; + } + + $stamp = gmmktime($matches[4], + $matches[5], + $matches[6], + $matches[2], + $matches[3], + $matches[1]); + + $time = time(); + if ($stamp < ($time - $clockSkew) + || $stamp > ($time + $clockSkew)) { + + return false; + } + + return true; + } + +} diff --git a/lib/Helpers/Token.php b/lib/Helpers/Token.php new file mode 100644 index 0000000..61352a1 --- /dev/null +++ b/lib/Helpers/Token.php @@ -0,0 +1,43 @@ + 'John', + 'lastName' => 'Doe', + 'email' => 'john.doe@example.com', + 'phone' => '0770000000', + 'status' => true + ]; + + public function setUp(): void + { + parent::setUp(); // TODO: Change the autogenerated stub + + } + + //@TODO: write tests + + +} From 8eb4e44d924745e751502c1127a8db1afda3d023 Mon Sep 17 00:00:00 2001 From: Cornel Bratu Date: Wed, 13 Mar 2019 14:39:06 +0200 Subject: [PATCH 02/24] examples --- app.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 app.php diff --git a/app.php b/app.php new file mode 100644 index 0000000..df271e7 --- /dev/null +++ b/app.php @@ -0,0 +1,34 @@ + "Jane", + "lastName" => "Doe", + "email" => "jane.doe@example.com", + "phone" => "", + "status" => false +]; + +$data = json_encode($test); + +$enc = \Retargeting\Helpers\Encryption::encrypt($data); + +var_dump($enc); + + +//try { +// $dec = (new Retargeting\Helpers\Decryption())->decrypt($enc); +//} catch (\Retargeting\Exceptions\DecryptException $e) { +//} +// +//var_dump($dec); +// +// +//var_dump(\Retargeting\Helpers\Token::createRandomToken()); \ No newline at end of file From d00c1dcd3b99f492b2454e9c804eca5f9a936204 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Thu, 14 Mar 2019 18:36:03 +0200 Subject: [PATCH 03/24] Changes for sdk 1. Added RTG Api Client 2. Made some changes related to product class 3. Add a function to parse correctly strings --- lib/AbstractRetargetingSDK.php | 18 ++- lib/Api/RTGApiClient.php | 201 +++++++++++++++++++++++++++++++++ lib/Helpers/Decryption.php | 7 +- lib/Helpers/Encryption.php | 3 +- lib/Product.php | 116 ++++++++++++++----- 5 files changed, 312 insertions(+), 33 deletions(-) create mode 100644 lib/Api/RTGApiClient.php diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index fde50cb..ca6fea7 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -6,12 +6,12 @@ * Time: 07:48 */ namespace Retargeting; + /** * Class AbstractRetargetingSDK */ abstract class AbstractRetargetingSDK { - /** * @param array $data * @return string @@ -21,4 +21,20 @@ public function toJSON(array $data): string return json_encode($data, JSON_PRETTY_PRINT); } + /** + * Get proper formatted string + * @param $text + * @return string + */ + public function getProperFormattedString($text) + { + if ((bool)$text) { + return trim(strip_tags(html_entity_decode( + html_entity_decode($text), + ENT_QUOTES | ENT_COMPAT, + 'UTF-8'))); + } else { + return ''; + } + } } \ No newline at end of file diff --git a/lib/Api/RTGApiClient.php b/lib/Api/RTGApiClient.php new file mode 100644 index 0000000..c1ed715 --- /dev/null +++ b/lib/Api/RTGApiClient.php @@ -0,0 +1,201 @@ +api_key = $api_key; + } else { + $this->_throwException("checkApiKey"); + } + } + + /** + * Method: set a new API uri + * @param string $api_uri + */ + public function setApiUri($api_uri) { + if (is_string($api_uri) && !empty($api_uri)) { + $this->api_uri = $api_uri; + } else { + $this->_throwException("apiUriType"); + } + } + + /** + * Method: set a new API version + * @param string $api_version + */ + public function setApiVersion($api_version) + { + if (is_string($api_version) && !empty($api_version)) { + $this->api_version = $api_version; + } else { + $this->_throwException("apiVersionType"); + } + } + + /** + * Method: set a new API response format: json or serial (php serialize) + * @param string $response_format + */ + public function setResponseFormat($response_format = "json") + { + if (in_array($response_format, array("json", "serial"))) { + $this->response_format = $response_format; + } else { + $this->_throwException("responseFormat"); + } + } + + /** + * Method: set encoding for API response + * @param bool $mode + */ + public function setDecoding($mode = true) + { + if (is_bool($mode)) { + $this->decoding = $mode; + } else { + $this->_throwException("decodingMode"); + } + } + + /** + * Overloading method: is utilized for reading data from inaccessible properties + * @param string $name + * @return Retargeting_REST_API_Client + */ + public function __get($name) + { + $this->api_path[] = $name; + return $this; + } + + /** + * Overloading method: is triggered when invoking inaccessible methods in an object context + * @param string $name + * @param array $arguments + * @see _processRequest() + * @return array + */ + public function __call($name, $arguments) + { + $this->api_path[] = $name; + $this->api_parameters = $arguments; + return $this->_processRequest(); + } + + /** + * Method: use PHP cURL library to connect with Retargeting REST API and send the request + * @see http://php.net/manual/ro/book.curl.php + * @return array + */ + private function _processRequest() + { + if (empty($this->api_path)) { + $this->_throwException("emptyApiPath"); + } + + $api_uri = $this->api_uri."/".$this->api_version."/".implode("/", $this->api_path).".".$this->response_format; + + $this->api_path = array(); + $api_parameters = array( + "api_key" => $this->api_key + ); + + $api_parameters = http_build_query(array_merge($api_parameters, $this->api_parameters)); + $this->api_parameters = array(); + + $curl_request = curl_init(); + curl_setopt($curl_request, CURLOPT_URL, $api_uri); + curl_setopt($curl_request, CURLOPT_TIMEOUT, 30); + curl_setopt($curl_request, CURLOPT_POST, true); + curl_setopt($curl_request, CURLOPT_POSTFIELDS, $api_parameters); + curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, false); + + if ($this->decoding) { + if ($this->response_format == "json") { + return json_decode(curl_exec($curl_request), true); + } elseif ($this->response_format == "serial") { + return unserialize(curl_exec($curl_request)); + } + } + + return curl_exec($curl_request); + } + + /** + * Method: throw new exception with custom message + * @param string $message + * @throws Exception + */ + private function _throwException($message) + { + $messages = array( + "checkApiKey" => "You need an API KEY to use Retargeting API. Please go to your Retargeting Administration Panel to set up or check your API KEY.", + "apiUriType" => "The API uri must be string", + "apiVersionType" => "The API version must be a string", + "responseFormat" => "The response format can only be json or serial (php serialize)", + "decodingMode" => "Decoding must be boolean", + "emptyApiPath" => "You API request" + ); + + throw new Exception($messages[$message]); + } +} \ No newline at end of file diff --git a/lib/Helpers/Decryption.php b/lib/Helpers/Decryption.php index c8d3e68..3da8249 100644 --- a/lib/Helpers/Decryption.php +++ b/lib/Helpers/Decryption.php @@ -78,16 +78,19 @@ private function verifyNonce($nonce, $clockSkew = 60) $result = preg_match('/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/', $nonce, $matches); + if ($result != 1 || count($matches) != 8) { return false; } - $stamp = gmmktime($matches[4], + $stamp = gmmktime( + $matches[4], $matches[5], $matches[6], $matches[2], $matches[3], - $matches[1]); + $matches[1] + ); $time = time(); if ($stamp < ($time - $clockSkew) diff --git a/lib/Helpers/Encryption.php b/lib/Helpers/Encryption.php index d97b085..947e313 100644 --- a/lib/Helpers/Encryption.php +++ b/lib/Helpers/Encryption.php @@ -14,7 +14,6 @@ */ class Encryption { - const TOKEN = "df2ce5cba06265db9bffeb6caf8d9fcf46a5a1712f774bca67535a82bdcf1955"; const METHOD = "AES-256-CBC"; @@ -31,7 +30,7 @@ class Encryption public function __construct($customer, $token) { $this->customer = $customer; - $this->token = $token; + $this->token = $token; } /** diff --git a/lib/Product.php b/lib/Product.php index bf61ff6..f974309 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -5,12 +5,10 @@ * Date: 2019-02-19 * Time: 07:48 */ - namespace Retargeting; class Product extends AbstractRetargetingSDK { - protected $id; protected $name; protected $url; @@ -20,13 +18,33 @@ class Product extends AbstractRetargetingSDK protected $brand = null; protected $category = []; protected $inventory = []; + protected $additionalImages = []; /** - * @param mixed $id + * Product constructor. + * @param $id + * @param $name + * @param $url + * @param $img + * @param $price + * @param $promo + * @param $brand + * @param $category + * @param $inventory + * @param $additionalImages */ - public function setId($id) + public function __construct($id, $name, $url, $img, $price, $promo, $brand, $category, $inventory, $additionalImages) { $this->id = $id; + $this->name = $name; + $this->url = $url; + $this->img = $img; + $this->price = $price; + $this->promo = $promo; + $this->brand = $brand; + $this->category = $category; + $this->inventory = $inventory; + $this->additionalImages = $additionalImages; } /** @@ -37,20 +55,32 @@ public function getId() return $this->id; } + /** + * @param mixed $id + * @return Product + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + /** * @return mixed */ public function getName() { - return $this->name; + return $this->getProperFormattedString($this->name); } /** * @param mixed $name + * @return Product */ - public function setName($name): void + public function setName($name) { $this->name = $name; + return $this; } /** @@ -63,11 +93,12 @@ public function getUrl() /** * @param mixed $url + * @return Product */ - public function setUrl($url): void + public function setUrl($url) { - //@todo: verifica daca url-ul incepe cu http sau https.... $this->url = $url; + return $this; } /** @@ -80,10 +111,12 @@ public function getImg() /** * @param mixed $img + * @return Product */ - public function setImg($img): void + public function setImg($img) { $this->img = $img; + return $this; } /** @@ -96,26 +129,30 @@ public function getPrice() /** * @param mixed $price + * @return Product */ - public function setPrice(int $price): void + public function setPrice($price) { $this->price = $price; + return $this; } /** - * @return float + * @return int */ - public function getPromo(): float + public function getPromo(): int { return $this->promo; } /** - * @param float $promo + * @param int $promo + * @return Product */ - public function setPromo(float $promo): void + public function setPromo(int $promo): Product { $this->promo = $promo; + return $this; } /** @@ -128,10 +165,12 @@ public function getBrand() /** * @param null $brand + * @return Product */ - public function setBrand($brand): void + public function setBrand($brand) { $this->brand = $brand; + return $this; } /** @@ -144,10 +183,12 @@ public function getCategory(): array /** * @param array $category + * @return Product */ - public function setCategory(array $category): void + public function setCategory(array $category): Product { $this->category = $category; + return $this; } /** @@ -160,10 +201,30 @@ public function getInventory(): array /** * @param array $inventory + * @return Product */ - public function setInventory(array $inventory): void + public function setInventory(array $inventory): Product { $this->inventory = $inventory; + return $this; + } + + /** + * @return array + */ + public function getAdditionalImages(): array + { + return $this->additionalImages; + } + + /** + * @param array $additionalImages + * @return Product + */ + public function setAdditionalImages(array $additionalImages): Product + { + $this->additionalImages = $additionalImages; + return $this; } /** @@ -171,18 +232,17 @@ public function setInventory(array $inventory): void */ public function prepareProductInformation() { - return $this->toJSON([ - 'id' => $this->id, - 'name' => $this->name, - 'url' => $this->url, - 'img' => $this->img, - 'price' => $this->price, - 'promo' => $this->promo, - 'brand' => $this->brand, - 'category' => $this->category, - 'inventory' => $this->inventory + 'id' => $this->getId(), + 'name' => $this->getName(), + 'url' => $this->getUrl(), + 'img' => $this->getImg(), + 'price' => $this->getPrice(), + 'promo' => $this->getPromo(), + 'brand' => $this->getBrand(), + 'category' => $this->getCategory(), + 'inventory' => $this->getInventory(), + 'additionalImages' => $this->getAdditionalImages() ]); - } } \ No newline at end of file From 37424f5f4d85dabb9c1c611c20fd3bcd6238f002 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 15 Mar 2019 18:17:13 +0200 Subject: [PATCH 04/24] Changes for sdk --- lib/Brand.php | 44 ++++- lib/Category.php | 80 +++++++++ lib/Checkout.php | 26 ++- lib/Email.php | 98 ++++++++++- lib/Order.php | 223 +++++++++++++++++++++++++- lib/OrderProducts.php | 92 +++++++++++ lib/Product.php | 94 ++--------- lib/ProductFeed.php | 16 ++ lib/Validations/AbstractValidator.php | 31 ++++ lib/Validations/UrlValidator.php | 36 +++++ lib/Validations/Validator.php | 22 +++ lib/Variation.php | 61 ++++++- tests/Unit/BrandTest.php | 39 +++++ tests/Unit/CategoryTest.php | 31 ++++ tests/Unit/EmailTest.php | 32 ++++ tests/Unit/OrderProductsTest.php | 39 +++++ tests/Unit/OrderTest.php | 32 ++++ tests/Unit/ProductTest.php | 12 +- 18 files changed, 919 insertions(+), 89 deletions(-) create mode 100644 lib/OrderProducts.php create mode 100644 lib/Validations/AbstractValidator.php create mode 100644 lib/Validations/UrlValidator.php create mode 100644 lib/Validations/Validator.php create mode 100644 tests/Unit/BrandTest.php create mode 100644 tests/Unit/CategoryTest.php create mode 100644 tests/Unit/EmailTest.php create mode 100644 tests/Unit/OrderProductsTest.php create mode 100644 tests/Unit/OrderTest.php diff --git a/lib/Brand.php b/lib/Brand.php index 1c2aa82..0822933 100644 --- a/lib/Brand.php +++ b/lib/Brand.php @@ -8,8 +8,48 @@ namespace Retargeting; - -class Brand +class Brand extends AbstractRetargetingSDK { + protected $id; + protected $name = ''; + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name) + { + $this->name = $name; + } + public function prepareBrandInformation() + { + return $this->toJSON([ + 'id' => $this->getId(), + 'name' => $this->getName() + ]); + } } \ No newline at end of file diff --git a/lib/Category.php b/lib/Category.php index b1f23d3..a2488bb 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -14,5 +14,85 @@ */ class Category extends AbstractRetargetingSDK { + protected $id; + protected $name; + protected $parent = 0; + protected $breadcrumb = []; + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return int + */ + public function getParent(): int + { + return $this->parent; + } + + /** + * @param int $parent + */ + public function setParent(int $parent) + { + $this->parent = $parent; + } + + /** + * @return array + */ + public function getBreadcrumb(): array + { + return $this->breadcrumb; + } + + /** + * @param array $breadcrumb + */ + public function setBreadcrumb(array $breadcrumb) + { + $this->breadcrumb = $breadcrumb; + } + + /** + * @return string + */ + public function prepareCategoryData() + { + return $this->toJSON([ + 'id' => $this->getId(), + 'name' => $this->getName(), + 'parent' => $this->getParent(), + 'breadcrumb' => $this->getBreadcrumb() + ]); + } } \ No newline at end of file diff --git a/lib/Checkout.php b/lib/Checkout.php index f229767..99b2274 100644 --- a/lib/Checkout.php +++ b/lib/Checkout.php @@ -8,8 +8,30 @@ namespace Retargeting; - -class Checkout +class Checkout extends AbstractRetargetingSDK { + protected $productIds = []; + + /** + * @return array + */ + public function getProductIds(): array + { + return $this->productIds; + } + + /** + * @param array $productIds + */ + public function setProductIds(array $productIds) + { + $this->productIds = $productIds; + } + public function prepareCheckoutIds() + { + return $this->toJSON([ + 'product_id' => $this->getProductIds() + ]); + } } \ No newline at end of file diff --git a/lib/Email.php b/lib/Email.php index 5f7f19c..9d38fb1 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -8,8 +8,102 @@ namespace Retargeting; - -class Email +class Email extends AbstractRetargetingSDK { + protected $email; + protected $name = ''; + protected $phone = ''; + protected $city = ''; + protected $sex = ''; + + /** + * @return mixed + */ + public function getEmail() + { + return $this->email; + } + + /** + * @param mixed $email + */ + public function setEmail($email) + { + $this->email = $email; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name) + { + $this->name = $name; + } + + /** + * @return string + */ + public function getPhone(): string + { + return $this->phone; + } + + /** + * @param string $phone + */ + public function setPhone(string $phone) + { + $this->phone = $phone; + } + + /** + * @return string + */ + public function getCity(): string + { + return $this->city; + } + + /** + * @param string $city + */ + public function setCity(string $city) + { + $this->city = $city; + } + + /** + * @return string + */ + public function getSex(): string + { + return $this->sex; + } + + /** + * @param string $sex + */ + public function setSex(string $sex) + { + $this->sex = $sex; + } + public function prepareEmailData() + { + return $this->toJSON([ + 'email' => $this->getEmail(), + 'name' => $this->getName(), + 'phone' => $this->getPhone(), + 'city' => $this->getCity(), + 'sex' => $this->getSex() + ]); + } } \ No newline at end of file diff --git a/lib/Order.php b/lib/Order.php index aab1acb..67d78b3 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -8,7 +8,228 @@ namespace Retargeting; -class Order +class Order extends AbstractRetargetingSDK { + protected $orderNo; + protected $lastName; + protected $firstName; + protected $email; + protected $phone; + protected $state; + protected $city; + protected $address; + protected $discount; + protected $discountCode; + protected $shipping; + protected $total; + /** + * @return mixed + */ + public function getOrderNo() + { + return $this->orderNo; + } + + /** + * @param mixed $orderNo + */ + public function setOrderNo($orderNo) + { + $this->orderNo = $orderNo; + } + + /** + * @return mixed + */ + public function getLastName() + { + return $this->lastName; + } + + /** + * @param mixed $lastName + */ + public function setLastName($lastName) + { + $this->lastName = $lastName; + } + + /** + * @return mixed + */ + public function getFirstName() + { + return $this->firstName; + } + + /** + * @param mixed $firstName + */ + public function setFirstName($firstName) + { + $this->firstName = $firstName; + } + + /** + * @return mixed + */ + public function getEmail() + { + return $this->email; + } + + /** + * @param mixed $email + */ + public function setEmail($email) + { + $this->email = $email; + } + + /** + * @return mixed + */ + public function getPhone() + { + return $this->phone; + } + + /** + * @param mixed $phone + */ + public function setPhone($phone) + { + $this->phone = $phone; + } + + /** + * @return mixed + */ + public function getState() + { + return $this->state; + } + + /** + * @param mixed $state + */ + public function setState($state) + { + $this->state = $state; + } + + /** + * @return mixed + */ + public function getCity() + { + return $this->city; + } + + /** + * @param mixed $city + */ + public function setCity($city) + { + $this->city = $city; + } + + /** + * @return mixed + */ + public function getAddress() + { + return $this->address; + } + + /** + * @param mixed $address + */ + public function setAddress($address) + { + $this->address = $address; + } + + /** + * @return mixed + */ + public function getDiscount() + { + return $this->discount; + } + + /** + * @param mixed $discount + */ + public function setDiscount($discount) + { + $this->discount = $discount; + } + + /** + * @return mixed + */ + public function getDiscountCode() + { + return $this->discountCode; + } + + /** + * @param mixed $discountCode + */ + public function setDiscountCode($discountCode) + { + $this->discountCode = $discountCode; + } + + /** + * @return mixed + */ + public function getShipping() + { + return $this->shipping; + } + + /** + * @param mixed $shipping + */ + public function setShipping($shipping) + { + $this->shipping = $shipping; + } + + /** + * @return mixed + */ + public function getTotal() + { + return $this->total; + } + + /** + * @param mixed $total + */ + public function setTotal($total) + { + $this->total = $total; + } + + public function prepareOrderInformation() + { + return $this->toJSON([ + 'order_no' => $this->getOrderNo(), + 'lastname' => $this->getLastName(), + 'firstname' => $this->getFirstName(), + 'email' => $this->getEmail(), + 'phone' => $this->getPhone(), + 'state' => $this->getState(), + 'city' => $this->getCity(), + 'address' => $this->getAddress(), + 'discount' => $this->getDiscount(), + 'discount_code' => $this->getDiscountCode(), + 'shipping' => $this->getShipping(), + 'total' => $this->getTotal() + ]); + } } \ No newline at end of file diff --git a/lib/OrderProducts.php b/lib/OrderProducts.php new file mode 100644 index 0000000..f6cd795 --- /dev/null +++ b/lib/OrderProducts.php @@ -0,0 +1,92 @@ +id; + } + + /** + * @param mixed $id + */ + public function setId($id) + { + $this->id = $id; + } + + /** + * @return boolean + */ + public function getQuantity() + { + return $this->quantity; + } + + /** + * @param boolean $quantity + */ + public function setQuantity($quantity) + { + $this->quantity = $quantity; + } + + /** + * @return mixed + */ + public function getPrice() + { + return $this->price; + } + + /** + * @param mixed $price + */ + public function setPrice($price) + { + $this->price = $price; + } + + /** + * @return mixed + */ + public function getVariationCode() + { + return $this->variationCode; + } + + /** + * @param mixed $variationCode + */ + public function setVariationCode($variationCode) + { + $this->variationCode = $variationCode; + } + + public function prepareOrderProductsInfo() + { + return $this->toJSON([ + 'id' => $this->getId(), + 'quantity' => $this->getQuantity(), + 'price' => $this->getPrice(), + 'variation_code' => $this->getVariationCode() + ]); + } +} \ No newline at end of file diff --git a/lib/Product.php b/lib/Product.php index f974309..44ed586 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -7,45 +7,19 @@ */ namespace Retargeting; +use Retargeting\Validations\UrlValidator; + class Product extends AbstractRetargetingSDK { - protected $id; + protected $id = ''; protected $name; - protected $url; + protected $url = ''; protected $img; protected $price; protected $promo = 0; - protected $brand = null; + protected $brand = []; protected $category = []; protected $inventory = []; - protected $additionalImages = []; - - /** - * Product constructor. - * @param $id - * @param $name - * @param $url - * @param $img - * @param $price - * @param $promo - * @param $brand - * @param $category - * @param $inventory - * @param $additionalImages - */ - public function __construct($id, $name, $url, $img, $price, $promo, $brand, $category, $inventory, $additionalImages) - { - $this->id = $id; - $this->name = $name; - $this->url = $url; - $this->img = $img; - $this->price = $price; - $this->promo = $promo; - $this->brand = $brand; - $this->category = $category; - $this->inventory = $inventory; - $this->additionalImages = $additionalImages; - } /** * @return mixed @@ -57,12 +31,10 @@ public function getId() /** * @param mixed $id - * @return Product */ public function setId($id) { $this->id = $id; - return $this; } /** @@ -75,12 +47,10 @@ public function getName() /** * @param mixed $name - * @return Product */ public function setName($name) { $this->name = $name; - return $this; } /** @@ -93,12 +63,13 @@ public function getUrl() /** * @param mixed $url - * @return Product */ public function setUrl($url) { - $this->url = $url; - return $this; + if(UrlValidator::validate($url)) + { + $this->url = $url; + } } /** @@ -111,12 +82,10 @@ public function getImg() /** * @param mixed $img - * @return Product */ public function setImg($img) { $this->img = $img; - return $this; } /** @@ -129,12 +98,10 @@ public function getPrice() /** * @param mixed $price - * @return Product */ public function setPrice($price) { $this->price = $price; - return $this; } /** @@ -147,30 +114,26 @@ public function getPromo(): int /** * @param int $promo - * @return Product */ - public function setPromo(int $promo): Product + public function setPromo(int $promo) { $this->promo = $promo; - return $this; } /** - * @return null + * @return array */ - public function getBrand() + public function getBrand(): array { return $this->brand; } /** - * @param null $brand - * @return Product + * @param array $brand */ - public function setBrand($brand) + public function setBrand(array $brand) { $this->brand = $brand; - return $this; } /** @@ -183,12 +146,10 @@ public function getCategory(): array /** * @param array $category - * @return Product */ - public function setCategory(array $category): Product + public function setCategory(array $category) { $this->category = $category; - return $this; } /** @@ -201,30 +162,10 @@ public function getInventory(): array /** * @param array $inventory - * @return Product */ - public function setInventory(array $inventory): Product + public function setInventory(array $inventory) { $this->inventory = $inventory; - return $this; - } - - /** - * @return array - */ - public function getAdditionalImages(): array - { - return $this->additionalImages; - } - - /** - * @param array $additionalImages - * @return Product - */ - public function setAdditionalImages(array $additionalImages): Product - { - $this->additionalImages = $additionalImages; - return $this; } /** @@ -241,8 +182,7 @@ public function prepareProductInformation() 'promo' => $this->getPromo(), 'brand' => $this->getBrand(), 'category' => $this->getCategory(), - 'inventory' => $this->getInventory(), - 'additionalImages' => $this->getAdditionalImages() + 'inventory' => $this->getInventory() ]); } } \ No newline at end of file diff --git a/lib/ProductFeed.php b/lib/ProductFeed.php index ec11174..03a7e29 100644 --- a/lib/ProductFeed.php +++ b/lib/ProductFeed.php @@ -11,5 +11,21 @@ class ProductFeed { + protected $productFeed = []; + /** + * @return array + */ + public function getProductFeed(): array + { + return $this->productFeed; + } + + /** + * @param array $productFeed + */ + public function setProductFeed(array $productFeed) + { + $this->productFeed = $productFeed; + } } \ No newline at end of file diff --git a/lib/Validations/AbstractValidator.php b/lib/Validations/AbstractValidator.php new file mode 100644 index 0000000..3360238 --- /dev/null +++ b/lib/Validations/AbstractValidator.php @@ -0,0 +1,31 @@ + 'Url must be absolute and start with https:// or http://' + ]; + + /** + * Get error message + * @param $key + * @return bool|mixed + */ + public static function getMessage($key) + { + if(array_key_exists($key, self::MESSAGES)) + { + return self::MESSAGES[$key]; + } + + return false; + } +} \ No newline at end of file diff --git a/lib/Validations/UrlValidator.php b/lib/Validations/UrlValidator.php new file mode 100644 index 0000000..c6426b8 --- /dev/null +++ b/lib/Validations/UrlValidator.php @@ -0,0 +1,36 @@ +code; + } + + /** + * @param mixed $code + */ + public function setCode($code) + { + $this->code = $code; + } + + /** + * @return int + */ + public function getStock(): int + { + return $this->stock; + } + + /** + * @param int $stock + */ + public function setStock(int $stock) + { + $this->stock = $stock; + } + + /** + * @return array + */ + public function getDetails(): array + { + return $this->details; + } + + /** + * @param array $details + */ + public function setDetails(array $details) + { + $this->details = $details; + } + + public function prepareVariationInfo() + { + return $this->toJSON([ + 'code' => $this->getCode(), + 'stock' => $this->getStock(), + 'details' => $this->getDetails() + ]); + } } \ No newline at end of file diff --git a/tests/Unit/BrandTest.php b/tests/Unit/BrandTest.php new file mode 100644 index 0000000..6ceb77f --- /dev/null +++ b/tests/Unit/BrandTest.php @@ -0,0 +1,39 @@ +brand = new Brand(); + } + + public function testIfBrandHasId() + { + $this->brand->setId(33); + $this->assertNotNull($this->brand->getId()); + } + + public function testIfBrandHasName() + { + $this->brand->setName('Nike'); + $this->assertNotNull($this->brand->getName()); + } +} diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php new file mode 100644 index 0000000..0c093a5 --- /dev/null +++ b/tests/Unit/CategoryTest.php @@ -0,0 +1,31 @@ +category = new Category(); + } + + public function testIfCategoryHasId() + { + $this->category->setId(89); + $this->assertNotNull($this->category->getId()); + } +} \ No newline at end of file diff --git a/tests/Unit/EmailTest.php b/tests/Unit/EmailTest.php new file mode 100644 index 0000000..ee980b3 --- /dev/null +++ b/tests/Unit/EmailTest.php @@ -0,0 +1,32 @@ +email = new Email(); + } + + public function test_if_email_is_not_empty() + { + $this->email->setEmail('test@google.com'); + + $this->assertNotEmpty($this->email->getEmail()); + } +} \ No newline at end of file diff --git a/tests/Unit/OrderProductsTest.php b/tests/Unit/OrderProductsTest.php new file mode 100644 index 0000000..bf5d018 --- /dev/null +++ b/tests/Unit/OrderProductsTest.php @@ -0,0 +1,39 @@ +orderProducts = new OrderProducts(); + } + + public function testIfOrderProductsHasId() + { + $this->orderProducts->setId(111); + $this->assertNotNull($this->orderProducts->getId()); + } + + public function testIfOrderProductsHasQuantity() + { + $this->orderProducts->setQuantity(1); + $this->assertNotNull($this->orderProducts->getQuantity()); + } +} \ No newline at end of file diff --git a/tests/Unit/OrderTest.php b/tests/Unit/OrderTest.php new file mode 100644 index 0000000..a391b9b --- /dev/null +++ b/tests/Unit/OrderTest.php @@ -0,0 +1,32 @@ +order = new Order(); + } + + public function test_if_order_has_no() + { + $this->order->setOrderNo(28); + + $this->assertNotNull($this->order->getOrderNo()); + } +} \ No newline at end of file diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index a494bdd..71277f0 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -8,7 +8,6 @@ namespace Retargeting; - use PHPUnit\Framework\TestCase; /** @@ -16,12 +15,13 @@ */ class ProductTest extends TestCase { - public function setUp(): void { parent::setUp(); // TODO: Change the autogenerated stub $this->product = new Product(); + $this->product->setUrl('http://google.ro'); + $this->product->setImg('https://www.google.com/img.png'); } public function test_if_product_has_id() @@ -32,13 +32,17 @@ public function test_if_product_has_id() public function test_if_product_has_name() { - $this->product->setName('Fooo'); $this->assertNotNull($this->product->getName()); } - public function test_if_product_url_is_set(){ + public function test_if_product_url_is_set() + { $this->assertEquals($this->product->getUrl(), 'http://google.ro'); } + public function test_if_product_has_image() + { + $this->assertEquals($this->product->getImg(), 'https://www.google.com/img.png'); + } } From e24035668be4d7ab6b80c8ad65e4184ef7a4b1ba Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Mon, 18 Mar 2019 19:17:25 +0200 Subject: [PATCH 05/24] Changes for sdk --- lib/AbstractRetargetingSDK.php | 27 +++++++- lib/Api/Customers.php | 5 +- lib/Brand.php | 2 +- lib/Category.php | 16 +++-- lib/Checkout.php | 12 +++- lib/Email.php | 25 +++++-- lib/Helpers/Decryption.php | 6 ++ lib/Order.php | 67 ++++++++++++------- lib/OrderProducts.php | 20 +++--- lib/Product.php | 57 +++++++++++----- lib/ProductFeed.php | 12 +++- lib/Validations/AbstractValidator.php | 31 --------- lib/Validations/UrlValidator.php | 36 ---------- lib/Validators/AbstractValidator.php | 48 +++++++++++++ lib/Validators/Email/EmailValidator.php | 27 ++++++++ lib/Validators/Product/BrandValidator.php | 47 +++++++++++++ lib/Validators/Product/CategoryValidator.php | 62 +++++++++++++++++ lib/Validators/Product/UrlValidator.php | 51 ++++++++++++++ .../Product/VariationsValidator.php | 43 ++++++++++++ .../ProductFeed/ProductFeedValidator.php | 64 ++++++++++++++++++ lib/{Validations => Validators}/Validator.php | 2 +- lib/Validators/Variation/CodeValidator.php | 41 ++++++++++++ .../Variation/DetailsValidation.php | 15 +++++ lib/Variation.php | 14 +++- 24 files changed, 591 insertions(+), 139 deletions(-) delete mode 100644 lib/Validations/AbstractValidator.php delete mode 100644 lib/Validations/UrlValidator.php create mode 100644 lib/Validators/AbstractValidator.php create mode 100644 lib/Validators/Email/EmailValidator.php create mode 100644 lib/Validators/Product/BrandValidator.php create mode 100644 lib/Validators/Product/CategoryValidator.php create mode 100644 lib/Validators/Product/UrlValidator.php create mode 100644 lib/Validators/Product/VariationsValidator.php create mode 100644 lib/Validators/ProductFeed/ProductFeedValidator.php rename lib/{Validations => Validators}/Validator.php (90%) create mode 100644 lib/Validators/Variation/CodeValidator.php create mode 100644 lib/Validators/Variation/DetailsValidation.php diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index ca6fea7..0b9caf9 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -7,10 +7,12 @@ */ namespace Retargeting; +use Retargeting\Validators\AbstractValidator; + /** * Class AbstractRetargetingSDK */ -abstract class AbstractRetargetingSDK +abstract class AbstractRetargetingSDK extends AbstractValidator { /** * @param array $data @@ -37,4 +39,27 @@ public function getProperFormattedString($text) return ''; } } + + /** + * Parse correct format of given data + * @param $value + * @return float|int|string + */ + public function formatIntFloatString($value) + { + $value = strip_tags(trim($value)); + + if(is_numeric($value) && !is_float($value)) + { + return (int)$value; + } + else if(is_numeric($value) && is_float($value)) + { + return (float)$value; + } + else + { + return $this->getProperFormattedString($value); + } + } } \ No newline at end of file diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php index 4108ef0..c58acb4 100644 --- a/lib/Api/Customers.php +++ b/lib/Api/Customers.php @@ -14,5 +14,8 @@ */ class Customers { - + public function __construct() + { + $var = new Client(); + } } diff --git a/lib/Brand.php b/lib/Brand.php index 0822933..4585379 100644 --- a/lib/Brand.php +++ b/lib/Brand.php @@ -49,7 +49,7 @@ public function prepareBrandInformation() { return $this->toJSON([ 'id' => $this->getId(), - 'name' => $this->getName() + 'name' => $this->getProperFormattedString($this->getName()) ]); } } \ No newline at end of file diff --git a/lib/Category.php b/lib/Category.php index a2488bb..d0b0cd0 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -14,8 +14,8 @@ */ class Category extends AbstractRetargetingSDK { - protected $id; - protected $name; + protected $id = 0; + protected $name = ''; protected $parent = 0; protected $breadcrumb = []; @@ -84,14 +84,20 @@ public function setBreadcrumb(array $breadcrumb) } /** + * Prepare category data * @return string */ public function prepareCategoryData() { + $id = $this->getProperFormattedString($this->getId()); + $name = $this->getProperFormattedString($this->getName()); + + $parent = is_bool($this->getParent()) && !$this->getParent() ? false : $this->getParent(); + return $this->toJSON([ - 'id' => $this->getId(), - 'name' => $this->getName(), - 'parent' => $this->getParent(), + 'id' => $id, + 'name' => $name, + 'parent' => $parent, 'breadcrumb' => $this->getBreadcrumb() ]); } diff --git a/lib/Checkout.php b/lib/Checkout.php index 99b2274..5bdc792 100644 --- a/lib/Checkout.php +++ b/lib/Checkout.php @@ -28,10 +28,16 @@ public function setProductIds(array $productIds) $this->productIds = $productIds; } + /** + * Prepare checkout ids + * @return string + */ public function prepareCheckoutIds() { - return $this->toJSON([ - 'product_id' => $this->getProductIds() - ]); + $productIds = is_array($this->getProductIds()) ? $this->getProductIds() : (array)$this->getProductIds(); + + return $this->toJSON( + $productIds + ); } } \ No newline at end of file diff --git a/lib/Email.php b/lib/Email.php index 9d38fb1..f1824e2 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -8,9 +8,11 @@ namespace Retargeting; +use Retargeting\Validators\Email\EmailValidator; + class Email extends AbstractRetargetingSDK { - protected $email; + protected $email = ''; protected $name = ''; protected $phone = ''; protected $city = ''; @@ -96,14 +98,25 @@ public function setSex(string $sex) $this->sex = $sex; } + /** + * Prepare email data + * @return string + */ public function prepareEmailData() { + $email = EmailValidator::validate($this->getEmail()); + $name = $this->getProperFormattedString($this->getName()); + $phone = $this->formatIntFloatString($this->getPhone()); + $city = $this->getProperFormattedString($this->getCity()); + + $sex = is_numeric($this->getSex()) ? $this->getSex() : (int)$this->getSex(); + return $this->toJSON([ - 'email' => $this->getEmail(), - 'name' => $this->getName(), - 'phone' => $this->getPhone(), - 'city' => $this->getCity(), - 'sex' => $this->getSex() + 'email' => $email, + 'name' => $name, + 'phone' => $phone, + 'city' => $city, + 'sex' => $sex ]); } } \ No newline at end of file diff --git a/lib/Helpers/Decryption.php b/lib/Helpers/Decryption.php index 3da8249..4dae2da 100644 --- a/lib/Helpers/Decryption.php +++ b/lib/Helpers/Decryption.php @@ -45,6 +45,12 @@ public function decrypt($token, $nonce = null) return $data; } + /** + * @param $data + * @param $key + * @param $method + * @return bool|string + */ private function tokenDecrypt($data, $key, $method) { $data = base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); diff --git a/lib/Order.php b/lib/Order.php index 67d78b3..5327b42 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -8,20 +8,22 @@ namespace Retargeting; +use Retargeting\Validators\Email\EmailValidator; + class Order extends AbstractRetargetingSDK { - protected $orderNo; - protected $lastName; - protected $firstName; - protected $email; - protected $phone; - protected $state; - protected $city; - protected $address; - protected $discount; - protected $discountCode; - protected $shipping; - protected $total; + protected $orderNo = 0; + protected $lastName = ''; + protected $firstName = ''; + protected $email = ''; + protected $phone = 0; + protected $state = ''; + protected $city = ''; + protected $address = ''; + protected $discount = ''; + protected $discountCode = '0'; + protected $shipping = ''; + protected $total = 0; /** * @return mixed @@ -215,21 +217,38 @@ public function setTotal($total) $this->total = $total; } + /** + * Prepare order information + * @return string + */ public function prepareOrderInformation() { + $orderNo = $this->formatIntFloatString($this->getOrderNo()); + $lastName = $this->getProperFormattedString($this->getLastName()); + $firstName = $this->getProperFormattedString($this->getFirstName()); + $email = EmailValidator::sanitize($this->getEmail(), 'email'); + $phone = $this->getProperFormattedString($this->getPhone()); + $state = $this->getProperFormattedString($this->getState()); + $city = $this->getProperFormattedString($this->getCity()); + $address = $this->getProperFormattedString($this->getAddress()); + $discount = $this->formatIntFloatString($this->getDiscount()); + $discountCode = $this->formatIntFloatString($this->getDiscountCode()); + $shipping = $this->formatIntFloatString($this->getShipping()); + $total = $this->formatIntFloatString($this->getTotal()); + return $this->toJSON([ - 'order_no' => $this->getOrderNo(), - 'lastname' => $this->getLastName(), - 'firstname' => $this->getFirstName(), - 'email' => $this->getEmail(), - 'phone' => $this->getPhone(), - 'state' => $this->getState(), - 'city' => $this->getCity(), - 'address' => $this->getAddress(), - 'discount' => $this->getDiscount(), - 'discount_code' => $this->getDiscountCode(), - 'shipping' => $this->getShipping(), - 'total' => $this->getTotal() + 'order_no' => $orderNo, + 'lastname' => $lastName, + 'firstname' => $firstName, + 'email' => $email, + 'phone' => $phone, + 'state' => $state, + 'city' => $city, + 'address' => $address, + 'discount' => $discount, + 'discount_code' => $discountCode, + 'shipping' => $shipping, + 'total' => $total ]); } } \ No newline at end of file diff --git a/lib/OrderProducts.php b/lib/OrderProducts.php index f6cd795..7b01cc7 100644 --- a/lib/OrderProducts.php +++ b/lib/OrderProducts.php @@ -11,10 +11,10 @@ class OrderProducts extends AbstractRetargetingSDK { - protected $id; - protected $quantity = false; - protected $price; - protected $variationCode; + protected $id = ''; + protected $quantity = 0; + protected $price = 0; + protected $variationCode = ''; /** * @return mixed @@ -80,13 +80,17 @@ public function setVariationCode($variationCode) $this->variationCode = $variationCode; } + /** + * Prepare order products for save order + * @return string + */ public function prepareOrderProductsInfo() { return $this->toJSON([ - 'id' => $this->getId(), - 'quantity' => $this->getQuantity(), - 'price' => $this->getPrice(), - 'variation_code' => $this->getVariationCode() + 'id' => $this->formatIntFloatString($this->getId()), + 'quantity' => $this->formatIntFloatString($this->getQuantity()), + 'price' => $this->formatIntFloatString($this->getPrice()), + 'variation_code' => $this->getProperFormattedString($this->getVariationCode()) ]); } } \ No newline at end of file diff --git a/lib/Product.php b/lib/Product.php index 44ed586..9b5c3b3 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -7,15 +7,18 @@ */ namespace Retargeting; -use Retargeting\Validations\UrlValidator; +use Retargeting\Validators\Product\BrandValidator; +use Retargeting\Validators\Product\CategoryValidator; +use Retargeting\Validators\Product\UrlValidator; +use Retargeting\Validators\Product\VariationsValidator; class Product extends AbstractRetargetingSDK { - protected $id = ''; - protected $name; + protected $id = 0; + protected $name = ''; protected $url = ''; - protected $img; - protected $price; + protected $img = ''; + protected $price = ''; protected $promo = 0; protected $brand = []; protected $category = []; @@ -62,14 +65,12 @@ public function getUrl() } /** - * @param mixed $url + * @param $url + * @return array|bool|mixed */ public function setUrl($url) { - if(UrlValidator::validate($url)) - { - $this->url = $url; - } + $this->url = $url; } /** @@ -169,20 +170,40 @@ public function setInventory(array $inventory) } /** + * Prepare product information * @return string */ public function prepareProductInformation() { + $id = $this->formatIntFloatString($this->getId()); + $name = $this->getProperFormattedString($this->getName()); + $url = UrlValidator::validate($this->getUrl()); + + $price = $this->formatIntFloatString($this->getPrice()); + + if($this->getPromo() > 0) + { + $promo = $this->formatIntFloatString($this->getPromo()); + } + else + { + $promo = 0; + } + + $brand = BrandValidator::validate($this->getBrand()); + $category = CategoryValidator::validate($this->getCategory()); + $inventory = VariationsValidator::validate($this->getInventory()); + return $this->toJSON([ - 'id' => $this->getId(), - 'name' => $this->getName(), - 'url' => $this->getUrl(), + 'id' => $id, + 'name' => $name, + 'url' => $url, 'img' => $this->getImg(), - 'price' => $this->getPrice(), - 'promo' => $this->getPromo(), - 'brand' => $this->getBrand(), - 'category' => $this->getCategory(), - 'inventory' => $this->getInventory() + 'price' => $price, + 'promo' => $promo, + 'brand' => $brand, + 'category' => $category, + 'inventory' => $inventory ]); } } \ No newline at end of file diff --git a/lib/ProductFeed.php b/lib/ProductFeed.php index 03a7e29..37ed8ca 100644 --- a/lib/ProductFeed.php +++ b/lib/ProductFeed.php @@ -8,8 +8,9 @@ namespace Retargeting; +use Retargeting\Validators\ProductFeed\ProductFeedValidator; -class ProductFeed +class ProductFeed extends AbstractRetargetingSDK { protected $productFeed = []; @@ -28,4 +29,13 @@ public function setProductFeed(array $productFeed) { $this->productFeed = $productFeed; } + + /** + * Prepare product feed JSON + * @return array|mixed + */ + public function prepareProductFeed() + { + return ProductFeedValidator::validate($this->getProductFeed()); + } } \ No newline at end of file diff --git a/lib/Validations/AbstractValidator.php b/lib/Validations/AbstractValidator.php deleted file mode 100644 index 3360238..0000000 --- a/lib/Validations/AbstractValidator.php +++ /dev/null @@ -1,31 +0,0 @@ - 'Url must be absolute and start with https:// or http://' - ]; - - /** - * Get error message - * @param $key - * @return bool|mixed - */ - public static function getMessage($key) - { - if(array_key_exists($key, self::MESSAGES)) - { - return self::MESSAGES[$key]; - } - - return false; - } -} \ No newline at end of file diff --git a/lib/Validations/UrlValidator.php b/lib/Validations/UrlValidator.php deleted file mode 100644 index c6426b8..0000000 --- a/lib/Validations/UrlValidator.php +++ /dev/null @@ -1,36 +0,0 @@ -id; + $categoryArr['name'] = self::sanitize($categoryData->name, 'string'); + $categoryArr['parent'] = $category->parent; + $categoryArr['breadcrumb'] = isset($category['breadcrumb']['id']) ? $breadCrumbArr : []; + } + } + } + + return json_encode($categoryArr, JSON_PRETTY_PRINT); + } +} \ No newline at end of file diff --git a/lib/Validators/Product/UrlValidator.php b/lib/Validators/Product/UrlValidator.php new file mode 100644 index 0000000..7b19143 --- /dev/null +++ b/lib/Validators/Product/UrlValidator.php @@ -0,0 +1,51 @@ +stock; + $variationArr['variations'] = false; + } + else + { + $variationArr['variations'] = $variationData->variations; + $variationArr['stock'] = $variationData->stock; + } + } + + return json_encode($variationArr, JSON_PRETTY_PRINT); + } +} \ No newline at end of file diff --git a/lib/Validators/ProductFeed/ProductFeedValidator.php b/lib/Validators/ProductFeed/ProductFeedValidator.php new file mode 100644 index 0000000..e9a160f --- /dev/null +++ b/lib/Validators/ProductFeed/ProductFeedValidator.php @@ -0,0 +1,64 @@ += 1) + { + $code = explode('-', $code); + + if(is_numeric($code[0]) && is_string($code[1])) + { + $code = implode('-', $code); + } + else + { + $code = ''; + } + } + + return $code; + } +} \ No newline at end of file diff --git a/lib/Validators/Variation/DetailsValidation.php b/lib/Validators/Variation/DetailsValidation.php new file mode 100644 index 0000000..90b33ae --- /dev/null +++ b/lib/Validators/Variation/DetailsValidation.php @@ -0,0 +1,15 @@ +details = $details; } + /** + * Prepare variation data + * @return string + */ public function prepareVariationInfo() { + $code = CodeValidator::validate($this->getCode()); + $stock = (bool)$this->getStock(); + return $this->toJSON([ - 'code' => $this->getCode(), - 'stock' => $this->getStock(), + 'code' => $code, + 'stock' => $stock, 'details' => $this->getDetails() ]); } From c07fb184b052425fdecb741999a2a211a6fe250a Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 20 Mar 2019 09:56:04 +0200 Subject: [PATCH 06/24] Changes for sdk --- lib/AbstractRetargetingSDK.php | 4 +--- lib/Email.php | 6 ++--- lib/Exceptions/RTGException.php | 15 ++++++++++++ .../AbstractHelper.php} | 5 ++-- .../BrandHelper.php} | 7 ++---- .../CategoryHelper.php} | 8 ++----- .../CodeHelper.php} | 7 ++---- .../{Decryption.php => DecryptionHelper.php} | 12 ++++++---- .../EmailHelper.php} | 7 ++---- .../{Encryption.php => EncryptionHelper.php} | 24 ++++--------------- .../Validator.php => Helpers/Helper.php} | 6 ++--- lib/Helpers/{Nonce.php => NonceHelper.php} | 2 +- .../ProductFeedHelper.php} | 7 ++---- lib/Helpers/{Token.php => TokenHelper.php} | 2 +- .../UrlHelper.php} | 7 ++---- .../VariationsHelper.php} | 7 ++---- lib/Order.php | 4 ++-- lib/OrderProducts.php | 1 - lib/Product.php | 16 ++++++------- lib/ProductFeed.php | 4 ++-- .../Variation/DetailsValidation.php | 15 ------------ lib/Variation.php | 4 ++-- tests/Unit/ApiCustomersTest.php | 2 -- 23 files changed, 66 insertions(+), 106 deletions(-) create mode 100644 lib/Exceptions/RTGException.php rename lib/{Validators/AbstractValidator.php => Helpers/AbstractHelper.php} (94%) rename lib/{Validators/Product/BrandValidator.php => Helpers/BrandHelper.php} (81%) rename lib/{Validators/Product/CategoryValidator.php => Helpers/CategoryHelper.php} (89%) rename lib/{Validators/Variation/CodeValidator.php => Helpers/CodeHelper.php} (76%) rename lib/Helpers/{Decryption.php => DecryptionHelper.php} (91%) rename lib/{Validators/Email/EmailValidator.php => Helpers/EmailHelper.php} (63%) rename lib/Helpers/{Encryption.php => EncryptionHelper.php} (74%) rename lib/{Validators/Validator.php => Helpers/Helper.php} (69%) rename lib/Helpers/{Nonce.php => NonceHelper.php} (99%) rename lib/{Validators/ProductFeed/ProductFeedValidator.php => Helpers/ProductFeedHelper.php} (89%) rename lib/Helpers/{Token.php => TokenHelper.php} (97%) rename lib/{Validators/Product/UrlValidator.php => Helpers/UrlHelper.php} (83%) rename lib/{Validators/Product/VariationsValidator.php => Helpers/VariationsHelper.php} (82%) delete mode 100644 lib/Validators/Variation/DetailsValidation.php diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index 0b9caf9..f43e03b 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -7,12 +7,10 @@ */ namespace Retargeting; -use Retargeting\Validators\AbstractValidator; - /** * Class AbstractRetargetingSDK */ -abstract class AbstractRetargetingSDK extends AbstractValidator +abstract class AbstractRetargetingSDK { /** * @param array $data diff --git a/lib/Email.php b/lib/Email.php index f1824e2..0e4daac 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -8,11 +8,11 @@ namespace Retargeting; -use Retargeting\Validators\Email\EmailValidator; +use Retargeting\Helpers\EmailHelper; class Email extends AbstractRetargetingSDK { - protected $email = ''; + protected $email; protected $name = ''; protected $phone = ''; protected $city = ''; @@ -104,7 +104,7 @@ public function setSex(string $sex) */ public function prepareEmailData() { - $email = EmailValidator::validate($this->getEmail()); + $email = EmailHelper::validate($this->getEmail()); $name = $this->getProperFormattedString($this->getName()); $phone = $this->formatIntFloatString($this->getPhone()); $city = $this->getProperFormattedString($this->getCity()); diff --git a/lib/Exceptions/RTGException.php b/lib/Exceptions/RTGException.php new file mode 100644 index 0000000..7c744b7 --- /dev/null +++ b/lib/Exceptions/RTGException.php @@ -0,0 +1,15 @@ +hash = hash(self::HASH_ALGORITHM, self::TOKEN); + $this->hash = hash(self::HASH_ALGORITHM, $token); } /** diff --git a/lib/Validators/Email/EmailValidator.php b/lib/Helpers/EmailHelper.php similarity index 63% rename from lib/Validators/Email/EmailValidator.php rename to lib/Helpers/EmailHelper.php index 56b302b..471308a 100644 --- a/lib/Validators/Email/EmailValidator.php +++ b/lib/Helpers/EmailHelper.php @@ -6,12 +6,9 @@ * Time: 14:43 */ -namespace Retargeting\Validators\Email; +namespace Retargeting\Helpers; -use Retargeting\Validators\AbstractValidator; -use Retargeting\Validators\Validator; - -class EmailValidator extends AbstractValidator implements Validator +class EmailHelper extends AbstractHelper implements Helper { /** * Format email properly diff --git a/lib/Helpers/Encryption.php b/lib/Helpers/EncryptionHelper.php similarity index 74% rename from lib/Helpers/Encryption.php rename to lib/Helpers/EncryptionHelper.php index 947e313..a6dfb7d 100644 --- a/lib/Helpers/Encryption.php +++ b/lib/Helpers/EncryptionHelper.php @@ -12,25 +12,19 @@ * Class Encryption * @package Retargeting\Helpers */ -class Encryption +class EncryptionHelper { - const TOKEN = "df2ce5cba06265db9bffeb6caf8d9fcf46a5a1712f774bca67535a82bdcf1955"; - const METHOD = "AES-256-CBC"; - const HASH_ALGORITHM = 'sha512'; - private $customer; - private $token; + public static $token; /** * Encryption constructor. - * @param $customer * @param $token */ - public function __construct($customer, $token) + public function __construct($token) { - $this->customer = $customer; - $this->token = $token; + self::$token = $token; } /** @@ -53,15 +47,7 @@ public static function encrypt($data) */ private static function createKey() { - return hash('sha512', self::TOKEN); - } - - /** - * @return mixed - */ - public function decodeCustomer() - { - return json_decode($this->customer); + return hash('sha512', self::$token); } /** diff --git a/lib/Validators/Validator.php b/lib/Helpers/Helper.php similarity index 69% rename from lib/Validators/Validator.php rename to lib/Helpers/Helper.php index a1da5b9..ad3e976 100644 --- a/lib/Validators/Validator.php +++ b/lib/Helpers/Helper.php @@ -6,13 +6,13 @@ * Time: 11:44 */ -namespace Retargeting\Validators; +namespace Retargeting\Helpers; -interface Validator +interface Helper { /** - * Returns an array of error messages, or an empty array + * Returns an empty array * if $data is valid. * * @param mixed $data diff --git a/lib/Helpers/Nonce.php b/lib/Helpers/NonceHelper.php similarity index 99% rename from lib/Helpers/Nonce.php rename to lib/Helpers/NonceHelper.php index 049ed04..a7b8bdc 100644 --- a/lib/Helpers/Nonce.php +++ b/lib/Helpers/NonceHelper.php @@ -12,7 +12,7 @@ * Class Nonce * @package Retargeting\Helpers */ -class Nonce +class NonceHelper { /** diff --git a/lib/Validators/ProductFeed/ProductFeedValidator.php b/lib/Helpers/ProductFeedHelper.php similarity index 89% rename from lib/Validators/ProductFeed/ProductFeedValidator.php rename to lib/Helpers/ProductFeedHelper.php index e9a160f..afc1845 100644 --- a/lib/Validators/ProductFeed/ProductFeedValidator.php +++ b/lib/Helpers/ProductFeedHelper.php @@ -6,12 +6,9 @@ * Time: 17:56 */ -namespace Retargeting\Validators\ProductFeed; +namespace Retargeting\Helpers; -use Retargeting\AbstractRetargetingSDK; -use Retargeting\Validators\Validator; - -class ProductFeedValidator extends AbstractRetargetingSDK implements Validator +class ProductFeedHelper extends AbstractHelper implements Helper { /** * Check if product feed json is valid or not diff --git a/lib/Helpers/Token.php b/lib/Helpers/TokenHelper.php similarity index 97% rename from lib/Helpers/Token.php rename to lib/Helpers/TokenHelper.php index 61352a1..f714907 100644 --- a/lib/Helpers/Token.php +++ b/lib/Helpers/TokenHelper.php @@ -12,7 +12,7 @@ * Class Token * @package Retargeting\Helpers */ -class Token +class TokenHelper { /** diff --git a/lib/Validators/Product/UrlValidator.php b/lib/Helpers/UrlHelper.php similarity index 83% rename from lib/Validators/Product/UrlValidator.php rename to lib/Helpers/UrlHelper.php index 7b19143..a4150c9 100644 --- a/lib/Validators/Product/UrlValidator.php +++ b/lib/Helpers/UrlHelper.php @@ -6,12 +6,9 @@ * Time: 10:50 */ -namespace Retargeting\Validators\Product; +namespace Retargeting\Helpers; -use Retargeting\Validators\AbstractValidator; -use Retargeting\Validators\Validator; - -class UrlValidator extends AbstractValidator implements Validator +final class UrlHelper extends AbstractHelper implements Helper { const HTTPS_HTTP_VALUE = ['https', 'http']; const HTTPS_VALUE = 'https://'; diff --git a/lib/Validators/Product/VariationsValidator.php b/lib/Helpers/VariationsHelper.php similarity index 82% rename from lib/Validators/Product/VariationsValidator.php rename to lib/Helpers/VariationsHelper.php index d23cb8f..935264b 100644 --- a/lib/Validators/Product/VariationsValidator.php +++ b/lib/Helpers/VariationsHelper.php @@ -6,12 +6,9 @@ * Time: 12:46 */ -namespace Retargeting\Validators\Product; +namespace Retargeting\Helpers; -use Retargeting\Validators\AbstractValidator; -use Retargeting\Validators\Validator; - -class VariationsValidator extends AbstractValidator implements Validator +class VariationsHelper extends AbstractHelper implements Helper { /** * Format variations object diff --git a/lib/Order.php b/lib/Order.php index 5327b42..7ad7dad 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -8,7 +8,7 @@ namespace Retargeting; -use Retargeting\Validators\Email\EmailValidator; +use Retargeting\Helpers\EmailHelper; class Order extends AbstractRetargetingSDK { @@ -226,7 +226,7 @@ public function prepareOrderInformation() $orderNo = $this->formatIntFloatString($this->getOrderNo()); $lastName = $this->getProperFormattedString($this->getLastName()); $firstName = $this->getProperFormattedString($this->getFirstName()); - $email = EmailValidator::sanitize($this->getEmail(), 'email'); + $email = EmailHelper::sanitize($this->getEmail(), 'email'); $phone = $this->getProperFormattedString($this->getPhone()); $state = $this->getProperFormattedString($this->getState()); $city = $this->getProperFormattedString($this->getCity()); diff --git a/lib/OrderProducts.php b/lib/OrderProducts.php index 7b01cc7..544d107 100644 --- a/lib/OrderProducts.php +++ b/lib/OrderProducts.php @@ -8,7 +8,6 @@ namespace Retargeting; - class OrderProducts extends AbstractRetargetingSDK { protected $id = ''; diff --git a/lib/Product.php b/lib/Product.php index 9b5c3b3..1e81266 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -7,10 +7,10 @@ */ namespace Retargeting; -use Retargeting\Validators\Product\BrandValidator; -use Retargeting\Validators\Product\CategoryValidator; -use Retargeting\Validators\Product\UrlValidator; -use Retargeting\Validators\Product\VariationsValidator; +use Retargeting\Helpers\BrandHelper; +use Retargeting\Helpers\CategoryHelper; +use Retargeting\Helpers\UrlHelper; +use Retargeting\Helpers\VariationsHelper; class Product extends AbstractRetargetingSDK { @@ -177,7 +177,7 @@ public function prepareProductInformation() { $id = $this->formatIntFloatString($this->getId()); $name = $this->getProperFormattedString($this->getName()); - $url = UrlValidator::validate($this->getUrl()); + $url = UrlHelper::validate($this->getUrl()); $price = $this->formatIntFloatString($this->getPrice()); @@ -190,9 +190,9 @@ public function prepareProductInformation() $promo = 0; } - $brand = BrandValidator::validate($this->getBrand()); - $category = CategoryValidator::validate($this->getCategory()); - $inventory = VariationsValidator::validate($this->getInventory()); + $brand = BrandHelper::validate($this->getBrand()); + $category = CategoryHelper::validate($this->getCategory()); + $inventory = VariationsHelper::validate($this->getInventory()); return $this->toJSON([ 'id' => $id, diff --git a/lib/ProductFeed.php b/lib/ProductFeed.php index 37ed8ca..5c07380 100644 --- a/lib/ProductFeed.php +++ b/lib/ProductFeed.php @@ -8,7 +8,7 @@ namespace Retargeting; -use Retargeting\Validators\ProductFeed\ProductFeedValidator; +use Retargeting\Helpers\ProductFeedHelper; class ProductFeed extends AbstractRetargetingSDK { @@ -36,6 +36,6 @@ public function setProductFeed(array $productFeed) */ public function prepareProductFeed() { - return ProductFeedValidator::validate($this->getProductFeed()); + return ProductFeedHelper::validate($this->getProductFeed()); } } \ No newline at end of file diff --git a/lib/Validators/Variation/DetailsValidation.php b/lib/Validators/Variation/DetailsValidation.php deleted file mode 100644 index 90b33ae..0000000 --- a/lib/Validators/Variation/DetailsValidation.php +++ /dev/null @@ -1,15 +0,0 @@ -getCode()); + $code = CodeHelper::validate($this->getCode()); $stock = (bool)$this->getStock(); return $this->toJSON([ diff --git a/tests/Unit/ApiCustomersTest.php b/tests/Unit/ApiCustomersTest.php index c05f76c..a1b4cbf 100644 --- a/tests/Unit/ApiCustomersTest.php +++ b/tests/Unit/ApiCustomersTest.php @@ -32,6 +32,4 @@ public function setUp(): void } //@TODO: write tests - - } From f9c8ff8918268de86ca2b25fcbc896ed24913077 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Mon, 25 Mar 2019 11:48:20 +0200 Subject: [PATCH 07/24] Changes for sdk --- lib/AbstractRetargetingSDK.php | 16 ++- lib/Api/{RTGApiClient.php => Client.php} | 8 +- lib/Api/Customers.php | 134 +++++++++++++++++- lib/Api/StockManagement.php | 170 +++++++++++++++++++++++ lib/Brand.php | 10 +- lib/Category.php | 8 +- lib/Email.php | 16 +-- lib/Exceptions/RTGException.php | 5 +- lib/Helpers/AbstractHelper.php | 53 +++++++ lib/Helpers/BrandHelper.php | 31 ++--- lib/Helpers/CategoryHelper.php | 46 +++--- lib/Helpers/CodeHelper.php | 2 +- lib/Helpers/CustomersApiHelper.php | 68 +++++++++ lib/Helpers/DecryptionHelper.php | 2 + lib/Helpers/EmailHelper.php | 2 +- lib/Helpers/EncryptionHelper.php | 8 +- lib/Helpers/Helper.php | 8 +- lib/Helpers/ProductFeedHelper.php | 15 +- lib/Helpers/TokenHelper.php | 13 +- lib/Helpers/UrlHelper.php | 26 ++-- lib/Helpers/VariationsHelper.php | 30 ++-- lib/Order.php | 2 +- lib/Product.php | 79 ++++++++--- lib/ProductFeed.php | 7 +- 24 files changed, 621 insertions(+), 138 deletions(-) rename lib/Api/{RTGApiClient.php => Client.php} (97%) create mode 100644 lib/Api/StockManagement.php create mode 100644 lib/Helpers/CustomersApiHelper.php diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index f43e03b..ff5bc80 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -16,7 +16,7 @@ abstract class AbstractRetargetingSDK * @param array $data * @return string */ - public function toJSON(array $data): string + public function toJSON(array $data) { return json_encode($data, JSON_PRETTY_PRINT); } @@ -60,4 +60,18 @@ public function formatIntFloatString($value) return $this->getProperFormattedString($value); } } + + /** + * Format url from an array + * @param $array + * @return array + */ + public function validateArrayData($array) + { + $mappedArray = array_map(function($item){ + return $this->getProperFormattedString($item); + }, $array); + + return $mappedArray; + } } \ No newline at end of file diff --git a/lib/Api/RTGApiClient.php b/lib/Api/Client.php similarity index 97% rename from lib/Api/RTGApiClient.php rename to lib/Api/Client.php index c1ed715..628eda7 100644 --- a/lib/Api/RTGApiClient.php +++ b/lib/Api/Client.php @@ -180,10 +180,10 @@ private function _processRequest() return curl_exec($curl_request); } - /** + /**_throwException * Method: throw new exception with custom message * @param string $message - * @throws Exception + * @throws \Exception */ private function _throwException($message) { @@ -193,9 +193,9 @@ private function _throwException($message) "apiVersionType" => "The API version must be a string", "responseFormat" => "The response format can only be json or serial (php serialize)", "decodingMode" => "Decoding must be boolean", - "emptyApiPath" => "You API request" + "emptyApiPath" => "You API request is empty" ); - throw new Exception($messages[$message]); + throw new \Exception($messages[$message]); } } \ No newline at end of file diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php index c58acb4..61f1ef7 100644 --- a/lib/Api/Customers.php +++ b/lib/Api/Customers.php @@ -8,14 +8,142 @@ namespace Retargeting\Api; +use Retargeting\AbstractRetargetingSDK; +use Retargeting\Helpers\CustomersApiHelper; + /** * Class Customers * @package Retargeting\Api */ -class Customers +class Customers extends AbstractRetargetingSDK { - public function __construct() + protected $token; + protected $data = []; + protected $currentPage = 1; + protected $lastPage = 20; + protected $nextPage = ''; + protected $prevPage = ''; + + /** + * Customers constructor. + * @param $token + * @throws \Exception + */ + public function __construct($token) + { + $token = CustomersApiHelper::getToken($token); + + $this->token = $token; + } + + /** + * @return mixed + */ + public function getToken() + { + return $this->token; + } + + /** + * @param mixed $token + */ + public function setToken($token) + { + $this->token = $token; + } + + /** + * @return mixed + */ + public function getData() + { + return $this->data; + } + + /** + * @param mixed $data + */ + public function setData($data) + { + $this->data = $data; + } + + /** + * @return mixed + */ + public function getCurrentPage() + { + return $this->currentPage; + } + + /** + * @param mixed $currentPage + */ + public function setCurrentPage($currentPage) + { + $this->currentPage = $currentPage; + } + + /** + * @return mixed + */ + public function getLastPage() + { + return $this->lastPage; + } + + /** + * @param mixed $lastPage + */ + public function setLastPage($lastPage) + { + $this->lastPage = $lastPage; + } + + /** + * @return mixed + */ + public function getNextPage() + { + return $this->nextPage; + } + + /** + * @param mixed $nextPage + */ + public function setNextPage($nextPage) + { + $this->nextPage = $nextPage; + } + + /** + * @return mixed + */ + public function getPrevPage() + { + return $this->prevPage; + } + + /** + * @param mixed $prevPage + */ + public function setPrevPage($prevPage) + { + $this->prevPage = $prevPage; + } + + /** + * Prepare customers api information + * @throws \Exception + */ + public function prepareCustomersApiInfo() { - $var = new Client(); + return $this->toJSON([ + 'data' => $this->getData(), + "current_page" => $this->getCurrentPage(), + "last_page" => $this->getLastPage(), + "next_page" => $this->getNextPage(), + "prev_page" => $this->getPrevPage() + ]); } } diff --git a/lib/Api/StockManagement.php b/lib/Api/StockManagement.php new file mode 100644 index 0000000..59a7227 --- /dev/null +++ b/lib/Api/StockManagement.php @@ -0,0 +1,170 @@ +productId; + } + + /** + * @param mixed $productId + */ + public function setProductId($productId) + { + $this->productId = $productId; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } + + /** + * @param mixed $name + */ + public function setName($name) + { + $this->name = $name; + } + + /** + * @return mixed + */ + public function getPrice() + { + return $this->price; + } + + /** + * @param mixed $price + */ + public function setPrice($price) + { + $this->price = $price; + } + + /** + * @return mixed + */ + public function getPromo() + { + return $this->promo; + } + + /** + * @param mixed $promo + */ + public function setPromo($promo) + { + $this->promo = $promo; + } + + /** + * @return bool + */ + public function isStock() + { + return $this->stock; + } + + /** + * @param bool $stock + */ + public function setStock($stock) + { + $this->stock = $stock; + } + + /** + * @return mixed + */ + public function getImage() + { + return $this->image; + } + + /** + * @param mixed $image + */ + public function setImage($image) + { + $this->image = $image; + } + + /** + * @return mixed + */ + public function getUrl() + { + return $this->url; + } + + /** + * @param mixed $url + */ + public function setUrl($url) + { + $this->url = $url; + } + + /** + * Prepare stock information + * @return array + */ + public function prepareStockInfo() + { + return [ + 'productId' => $this->getProductId(), + 'name' => $this->getName(), + 'price' => $this->getPrice(), + 'promo' => $this->getPromo(), + 'image' => $this->getImage(), + 'url' => $this->getUrl(), + 'stock' => $this->isStock(), + ]; + } + + /** + * Update product stock + * @param $api + * @param $product + * @throws RTGException + */ + public function updateStock($api, $product) + { + try { + $rtgClient = new Client($api); + $rtgClient->setResponseFormat("json"); + $rtgClient->setDecoding(false); + $rtgClient->products->update($product); + } catch(RTGException $exception) { + throw new RTGException($exception->getMessage()); + } + } +} \ No newline at end of file diff --git a/lib/Brand.php b/lib/Brand.php index 4585379..0ef0ddf 100644 --- a/lib/Brand.php +++ b/lib/Brand.php @@ -8,6 +8,8 @@ namespace Retargeting; +use Retargeting\Helpers\BrandHelper; + class Brand extends AbstractRetargetingSDK { protected $id; @@ -32,7 +34,7 @@ public function setId($id) /** * @return string */ - public function getName(): string + public function getName() { return $this->name; } @@ -40,16 +42,16 @@ public function getName(): string /** * @param string $name */ - public function setName(string $name) + public function setName($name) { $this->name = $name; } public function prepareBrandInformation() { - return $this->toJSON([ + return $this->toJSON(BrandHelper::validate([ 'id' => $this->getId(), 'name' => $this->getProperFormattedString($this->getName()) - ]); + ])); } } \ No newline at end of file diff --git a/lib/Category.php b/lib/Category.php index d0b0cd0..ed12107 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -16,7 +16,7 @@ class Category extends AbstractRetargetingSDK { protected $id = 0; protected $name = ''; - protected $parent = 0; + protected $parent = false; protected $breadcrumb = []; /** @@ -54,7 +54,7 @@ public function setName($name) /** * @return int */ - public function getParent(): int + public function getParent() { return $this->parent; } @@ -62,7 +62,7 @@ public function getParent(): int /** * @param int $parent */ - public function setParent(int $parent) + public function setParent($parent) { $this->parent = $parent; } @@ -70,7 +70,7 @@ public function setParent(int $parent) /** * @return array */ - public function getBreadcrumb(): array + public function getBreadcrumb() { return $this->breadcrumb; } diff --git a/lib/Email.php b/lib/Email.php index 0e4daac..4e9f762 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -37,7 +37,7 @@ public function setEmail($email) /** * @return string */ - public function getName(): string + public function getName() { return $this->name; } @@ -45,7 +45,7 @@ public function getName(): string /** * @param string $name */ - public function setName(string $name) + public function setName($name) { $this->name = $name; } @@ -53,7 +53,7 @@ public function setName(string $name) /** * @return string */ - public function getPhone(): string + public function getPhone() { return $this->phone; } @@ -61,7 +61,7 @@ public function getPhone(): string /** * @param string $phone */ - public function setPhone(string $phone) + public function setPhone($phone) { $this->phone = $phone; } @@ -69,7 +69,7 @@ public function setPhone(string $phone) /** * @return string */ - public function getCity(): string + public function getCity() { return $this->city; } @@ -77,7 +77,7 @@ public function getCity(): string /** * @param string $city */ - public function setCity(string $city) + public function setCity($city) { $this->city = $city; } @@ -85,7 +85,7 @@ public function setCity(string $city) /** * @return string */ - public function getSex(): string + public function getSex() { return $this->sex; } @@ -93,7 +93,7 @@ public function getSex(): string /** * @param string $sex */ - public function setSex(string $sex) + public function setSex($sex) { $this->sex = $sex; } diff --git a/lib/Exceptions/RTGException.php b/lib/Exceptions/RTGException.php index 7c744b7..7a7c272 100644 --- a/lib/Exceptions/RTGException.php +++ b/lib/Exceptions/RTGException.php @@ -8,7 +8,10 @@ namespace Retargeting\Exceptions; - +/** + * Class RTGException + * @package Retargeting\Exceptions + */ class RTGException extends \Exception { diff --git a/lib/Helpers/AbstractHelper.php b/lib/Helpers/AbstractHelper.php index 41f6d80..01fad28 100644 --- a/lib/Helpers/AbstractHelper.php +++ b/lib/Helpers/AbstractHelper.php @@ -44,4 +44,57 @@ public static function sanitize($var, $type) return $result; } + + /** + * Format string + * @param $string + * @return string + */ + public static function formatString($string) + { + $string = stripslashes(htmlspecialchars_decode(trim(strip_tags((string)$string)))); + + $string = self::sanitize($string, 'string'); + + return $string; + } + + /** + * Filter an array by key + * @param $array + * @param $keyname + * @return array + */ + public static function filterArrayByKey($array, $keyname) + { + $new_array = []; + + foreach($array as $key => $value) { + + if(!isset($new_array[$value[$keyname]])) { + $new_array[$value[$keyname]] = $value; + } + } + + $new_array = array_values($new_array); + + return $new_array; + } + + /** + * Throw exceptions when validating data + * @param $message + * @throws \Exception + */ + public static function _throwException($message) + { + $messages = array( + "emptyURL" => "Url is required. Please don't leave it empty.", + "emptyCustomerData" => "Customer data is required. Please don't leave it empty.", + "emptyToken" => "Token is required. Please don't leave it empty.", + "wrongFormat" => "The array format you provided is wrong." + ); + + throw new \Exception($messages[$message]); + } } \ No newline at end of file diff --git a/lib/Helpers/BrandHelper.php b/lib/Helpers/BrandHelper.php index 11d5d80..452bf84 100644 --- a/lib/Helpers/BrandHelper.php +++ b/lib/Helpers/BrandHelper.php @@ -8,7 +8,7 @@ namespace Retargeting\Helpers; -class BrandHelper extends AbstractHelper implements Helper +final class BrandHelper extends AbstractHelper implements Helper { /** * Format brand object @@ -17,28 +17,27 @@ class BrandHelper extends AbstractHelper implements Helper */ public static function validate($brand) { - if(is_bool($brand) && !$brand) - { - return false; - } - else + if(is_array($brand)) { $brandArr = []; - if(is_array($brand)) + if(array_key_exists('id', $brand) && isset($brand['id'])) + { + $brandArr['id'] = $brand['id']; + } + else { - if(array_key_exists('id', $brand)) - { - $brandArr['id'] = $brand['id']; - } + return false; + } - if(array_key_exists('name', $brand)) - { - $brandArr['name'] = self::sanitize($brand['name'], 'string'); - } + if(array_key_exists('name', $brand) && isset($brand['name'])) + { + $brandArr['name'] = self::sanitize($brand['name'], 'string'); } - return json_encode($brandArr, JSON_PRETTY_PRINT); + return $brandArr; } + + return false; } } \ No newline at end of file diff --git a/lib/Helpers/CategoryHelper.php b/lib/Helpers/CategoryHelper.php index 3faea9c..6fd149f 100644 --- a/lib/Helpers/CategoryHelper.php +++ b/lib/Helpers/CategoryHelper.php @@ -8,7 +8,7 @@ namespace Retargeting\Helpers; -class CategoryHelper extends AbstractHelper implements Helper +final class CategoryHelper extends AbstractHelper implements Helper { /** * Format product category @@ -18,41 +18,33 @@ class CategoryHelper extends AbstractHelper implements Helper public static function validate($categoryData) { $categoryArr = []; - $breadCrumbArr = []; - $categoryArr['id'] = ''; - $categoryArr['name'] = ''; - $categoryArr['parent'] = false; - $categoryArr['breadcrumb'] = []; - - if(!empty($categoryData) && count($categoryData) < 2) - { - $categoryArr['id'] = $categoryData['id']; - $categoryArr['name'] = self::sanitize($categoryData['name'], 'string'); - $categoryArr['parent'] = false; - $categoryArr['breadcrumb'] = []; - } - else + if(!empty($categoryData)) { - if(!empty($categoryData)) + //Check if there are duplicated parent categories + $categoryData = self::filterArrayByKey($categoryData, 'parent'); + + //Get the first category if there is only one + if(count($categoryData) < 2) + { + $categoryArr['id'] = $categoryData[0]['id']; + $categoryArr['name'] = self::formatString($categoryData[0]['name']); + $categoryArr['parent'] = false; + $categoryArr['breadcrumb'] = []; + } + //Check if there are nested categories + else if (count($categoryData) >= 2) { foreach($categoryData as $category) { - if(isset($category['breadcrumb']['id'])) - { - $breadCrumbArr['id'] = $category['breadcrumb']['id']; - $breadCrumbArr['name'] = self::sanitize($category['breadcrumb']['name'], 'string'); - $breadCrumbArr['parent'] = $category['breadcrumb']['parent']; - } + $category['name'] = self::formatString($category['name']); + $category['breadcrumb'] = is_array($category['breadcrumb']) ? $category['breadcrumb'] : (array)$category['breadcrumb']; - $categoryArr['id'] = $category->id; - $categoryArr['name'] = self::sanitize($categoryData->name, 'string'); - $categoryArr['parent'] = $category->parent; - $categoryArr['breadcrumb'] = isset($category['breadcrumb']['id']) ? $breadCrumbArr : []; + $categoryArr[] = $category; } } } - return json_encode($categoryArr, JSON_PRETTY_PRINT); + return $categoryArr; } } \ No newline at end of file diff --git a/lib/Helpers/CodeHelper.php b/lib/Helpers/CodeHelper.php index bfc81b6..cceb22f 100644 --- a/lib/Helpers/CodeHelper.php +++ b/lib/Helpers/CodeHelper.php @@ -8,7 +8,7 @@ namespace Retargeting\Helpers; -class CodeHelper extends AbstractHelper implements Helper +final class CodeHelper extends AbstractHelper implements Helper { /** * Check if variation code has proper format diff --git a/lib/Helpers/CustomersApiHelper.php b/lib/Helpers/CustomersApiHelper.php new file mode 100644 index 0000000..199c9a5 --- /dev/null +++ b/lib/Helpers/CustomersApiHelper.php @@ -0,0 +1,68 @@ + 0) + { + self::_throwException('wrongFormat'); + } + + $customers = $data; + } + + return $customers; + } + + /** + * Get token + * @param $token + * @return mixed + * @throws \Exception + */ + public static function getToken($token) + { + if(empty($token)) + { + self::_throwException('emptyToken'); + } + + return $token; + } +} \ No newline at end of file diff --git a/lib/Helpers/DecryptionHelper.php b/lib/Helpers/DecryptionHelper.php index bcc4321..0123dc3 100644 --- a/lib/Helpers/DecryptionHelper.php +++ b/lib/Helpers/DecryptionHelper.php @@ -9,6 +9,7 @@ namespace Retargeting\Helpers; use Retargeting\Exceptions\DecryptException; +use Retargeting\Exceptions\RTGException; /** * Class Decryption @@ -24,6 +25,7 @@ class DecryptionHelper /** * DecryptionHelper constructor. * @param $token + * @throws RTGException */ public function __construct($token) { diff --git a/lib/Helpers/EmailHelper.php b/lib/Helpers/EmailHelper.php index 471308a..b5330cd 100644 --- a/lib/Helpers/EmailHelper.php +++ b/lib/Helpers/EmailHelper.php @@ -8,7 +8,7 @@ namespace Retargeting\Helpers; -class EmailHelper extends AbstractHelper implements Helper +final class EmailHelper extends AbstractHelper implements Helper { /** * Format email properly diff --git a/lib/Helpers/EncryptionHelper.php b/lib/Helpers/EncryptionHelper.php index a6dfb7d..c685da0 100644 --- a/lib/Helpers/EncryptionHelper.php +++ b/lib/Helpers/EncryptionHelper.php @@ -7,6 +7,7 @@ */ namespace Retargeting\Helpers; +use Retargeting\Exceptions\RTGException; /** * Class Encryption @@ -15,12 +16,14 @@ class EncryptionHelper { const METHOD = "AES-256-CBC"; + const HASH_ALGORITHM = 'sha512'; public static $token; /** - * Encryption constructor. + * EncryptionHelper constructor. * @param $token + * @throws RTGException */ public function __construct($token) { @@ -38,7 +41,6 @@ public static function encrypt($data) $encrypted = openssl_encrypt($data, self::METHOD, self::createKey(), OPENSSL_RAW_DATA, $iv); - return rtrim(strtr(base64_encode($iv . $encrypted), '+/', '-_'), '='); } @@ -47,7 +49,7 @@ public static function encrypt($data) */ private static function createKey() { - return hash('sha512', self::$token); + return hash(self::HASH_ALGORITHM, self::$token); } /** diff --git a/lib/Helpers/Helper.php b/lib/Helpers/Helper.php index ad3e976..384bfe2 100644 --- a/lib/Helpers/Helper.php +++ b/lib/Helpers/Helper.php @@ -12,11 +12,9 @@ interface Helper { /** - * Returns an empty array - * if $data is valid. - * - * @param mixed $data - * @return array An array of error messages + * Interface validate method + * @param $data + * @return mixed */ public static function validate($data); } \ No newline at end of file diff --git a/lib/Helpers/ProductFeedHelper.php b/lib/Helpers/ProductFeedHelper.php index afc1845..32ed5d4 100644 --- a/lib/Helpers/ProductFeedHelper.php +++ b/lib/Helpers/ProductFeedHelper.php @@ -8,7 +8,7 @@ namespace Retargeting\Helpers; -class ProductFeedHelper extends AbstractHelper implements Helper +final class ProductFeedHelper extends AbstractHelper implements Helper { /** * Check if product feed json is valid or not @@ -17,6 +17,8 @@ class ProductFeedHelper extends AbstractHelper implements Helper */ public static function validate($feed) { + $feed = json_encode($feed); + $result = json_decode($feed); switch (json_last_error()) { @@ -58,4 +60,15 @@ public static function validate($feed) return $result; } + + /** + * Formats price into format, e.g. 1000.99. + * + * @param int|float|string $price the price string to format. + * @return string|null the formatted price. + */ + public static function formatPrice($price) + { + return is_numeric($price) ? number_format($price, 2, '.', '') : null; + } } \ No newline at end of file diff --git a/lib/Helpers/TokenHelper.php b/lib/Helpers/TokenHelper.php index f714907..8457a9e 100644 --- a/lib/Helpers/TokenHelper.php +++ b/lib/Helpers/TokenHelper.php @@ -12,19 +12,8 @@ * Class Token * @package Retargeting\Helpers */ -class TokenHelper +final class TokenHelper { - - /** - * Used just for - * @TODO: Remove this - * @return string - */ - public static function createRandomToken() - { - return hash('sha256', time()); - } - /** * This method will be used to generate user token right after module setup. The key must be saved in * Retargeting admin account and on client website. diff --git a/lib/Helpers/UrlHelper.php b/lib/Helpers/UrlHelper.php index a4150c9..507510b 100644 --- a/lib/Helpers/UrlHelper.php +++ b/lib/Helpers/UrlHelper.php @@ -10,30 +10,32 @@ final class UrlHelper extends AbstractHelper implements Helper { - const HTTPS_HTTP_VALUE = ['https', 'http']; + const HTTP_PROTOCOLS = ['https', 'http']; const HTTPS_VALUE = 'https://'; /** * Check if url contains https/http - * @param mixed $url - * @return array|bool|mixed + * @param $url + * @return mixed + * @throws \Exception */ public static function validate($url) { - $url = (string)$url; - - $url = strip_tags(trim($url)); + if(empty($url)) + { + self::_throwException('emptyURL'); + } - $url = self::sanitize($url, 'url'); + $url = self::formatString($url); - $url = parse_url($url); + $parsedUrl = parse_url($url); - if(isset($url['scheme']) && in_array($url['scheme'], self::HTTPS_HTTP_VALUE)) + if(isset($parsedUrl['scheme']) && !in_array($parsedUrl['scheme'], self::HTTP_PROTOCOLS)) { - return $url; - } else { - return self::prepend(filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL), self::HTTPS_VALUE); + $url = self::prepend(filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL), self::HTTPS_VALUE) . $parsedUrl['path'] . '?' . $parsedUrl['query']; } + + return self::sanitize($url, 'url'); } /** diff --git a/lib/Helpers/VariationsHelper.php b/lib/Helpers/VariationsHelper.php index 935264b..54aee9c 100644 --- a/lib/Helpers/VariationsHelper.php +++ b/lib/Helpers/VariationsHelper.php @@ -8,33 +8,37 @@ namespace Retargeting\Helpers; -class VariationsHelper extends AbstractHelper implements Helper +final class VariationsHelper extends AbstractHelper implements Helper { /** * Format variations object - * @param mixed $variationData + * @param mixed $variation * @return array|\stdClass */ - public static function validate($variationData) + public static function validate($variation) { - $variationArr = []; + $variationArr = [ + 'variations' => false, + 'stock' => [] + ]; - if(is_array($variationData) && - array_key_exists( 'stock', $variationData) && - array_key_exists('variations', $variationData)) + if(is_array($variation)) { - if(!$variationData['variations']) + if(array_key_exists('variations', $variation) && isset($variation['variations'])) { - $variationArr['stock'] = $variationData->stock; - $variationArr['variations'] = false; + $variationArr['variations'] = $variation['variations']; } else { - $variationArr['variations'] = $variationData->variations; - $variationArr['stock'] = $variationData->stock; + $variationArr['variations'] = false; + } + + if(array_key_exists('stock', $variation) && isset($variation['stock'])) + { + $variationArr['name'] = $variation['stock']; } } - return json_encode($variationArr, JSON_PRETTY_PRINT); + return $variationArr; } } \ No newline at end of file diff --git a/lib/Order.php b/lib/Order.php index 7ad7dad..82cf540 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -12,7 +12,7 @@ class Order extends AbstractRetargetingSDK { - protected $orderNo = 0; + protected $orderNo; protected $lastName = ''; protected $firstName = ''; protected $email = ''; diff --git a/lib/Product.php b/lib/Product.php index 1e81266..e38ffd6 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -23,6 +23,7 @@ class Product extends AbstractRetargetingSDK protected $brand = []; protected $category = []; protected $inventory = []; + protected $additionalImages = []; /** * @return mixed @@ -45,7 +46,7 @@ public function setId($id) */ public function getName() { - return $this->getProperFormattedString($this->name); + return $this->name; } /** @@ -106,17 +107,17 @@ public function setPrice($price) } /** - * @return int + * @return float */ - public function getPromo(): int + public function getPromo() { return $this->promo; } /** - * @param int $promo + * @param float $promo */ - public function setPromo(int $promo) + public function setPromo($promo) { $this->promo = $promo; } @@ -124,7 +125,7 @@ public function setPromo(int $promo) /** * @return array */ - public function getBrand(): array + public function getBrand() { return $this->brand; } @@ -132,7 +133,7 @@ public function getBrand(): array /** * @param array $brand */ - public function setBrand(array $brand) + public function setBrand($brand) { $this->brand = $brand; } @@ -140,7 +141,7 @@ public function setBrand(array $brand) /** * @return array */ - public function getCategory(): array + public function getCategory() { return $this->category; } @@ -148,7 +149,7 @@ public function getCategory(): array /** * @param array $category */ - public function setCategory(array $category) + public function setCategory($category) { $this->category = $category; } @@ -156,7 +157,7 @@ public function setCategory(array $category) /** * @return array */ - public function getInventory(): array + public function getInventory() { return $this->inventory; } @@ -164,24 +165,42 @@ public function getInventory(): array /** * @param array $inventory */ - public function setInventory(array $inventory) + public function setInventory($inventory) { $this->inventory = $inventory; } /** - * Prepare product information - * @return string + * @return array + */ + public function getAdditionalImages() + { + return $this->additionalImages; + } + + /** + * @param array $additionalImages + */ + public function setAdditionalImages($additionalImages) + { + $this->additionalImages = $additionalImages; + } + + /** + * Prepare product info to array + * @return array + * @throws \Exception */ public function prepareProductInformation() { $id = $this->formatIntFloatString($this->getId()); $name = $this->getProperFormattedString($this->getName()); $url = UrlHelper::validate($this->getUrl()); + $img = UrlHelper::validate($this->getImg()); $price = $this->formatIntFloatString($this->getPrice()); - if($this->getPromo() > 0) + if($this->getPromo() > 0 && $this->getPromo() < $this->getPrice()) { $promo = $this->formatIntFloatString($this->getPromo()); } @@ -194,16 +213,42 @@ public function prepareProductInformation() $category = CategoryHelper::validate($this->getCategory()); $inventory = VariationsHelper::validate($this->getInventory()); - return $this->toJSON([ + $additionalImages = $this->validateArrayData($this->getAdditionalImages()); + + return [ 'id' => $id, 'name' => $name, 'url' => $url, - 'img' => $this->getImg(), + 'img' => $img, 'price' => $price, 'promo' => $promo, 'brand' => $brand, 'category' => $category, - 'inventory' => $inventory + 'inventory' => $inventory, + 'images' => $additionalImages + ]; + } + + /** + * Prepare product info to array + * @return string + * @throws \Exception + */ + public function prepareProductInformationToJson() + { + $data = self::prepareProductInformation(); + + return $this->toJSON([ + 'id' => $data['id'], + 'name' => $data['name'], + 'url' => $data['url'], + 'img' => $data['img'], + 'price' => $data['price'], + 'promo' => $data['promo'], + 'brand' => $data['brand'], + 'category' => $data['category'], + 'inventory' => $data['inventory'], + 'images' => $data['images'] ]); } } \ No newline at end of file diff --git a/lib/ProductFeed.php b/lib/ProductFeed.php index 5c07380..47b4ae4 100644 --- a/lib/ProductFeed.php +++ b/lib/ProductFeed.php @@ -17,7 +17,7 @@ class ProductFeed extends AbstractRetargetingSDK /** * @return array */ - public function getProductFeed(): array + public function getProductFeed() { return $this->productFeed; } @@ -25,17 +25,16 @@ public function getProductFeed(): array /** * @param array $productFeed */ - public function setProductFeed(array $productFeed) + public function setProductFeed($productFeed) { $this->productFeed = $productFeed; } /** - * Prepare product feed JSON * @return array|mixed */ public function prepareProductFeed() { - return ProductFeedHelper::validate($this->getProductFeed()); + return $this->toJSON(ProductFeedHelper::validate($this->getProductFeed())); } } \ No newline at end of file From 0cb5a61c58683737dfcf24c1a7a36dcad728775e Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Mon, 25 Mar 2019 17:37:47 +0200 Subject: [PATCH 08/24] Changes for sdk --- composer.json | 12 ++- composer.lock | 114 +++++++++++++------------ lib/AbstractRetargetingSDK.php | 23 +++-- lib/Category.php | 17 ++-- lib/Checkout.php | 6 +- lib/Email.php | 27 +++--- lib/Order.php | 61 ++++++++------ lib/OrderProducts.php | 16 +++- lib/Product.php | 74 ++++++++-------- lib/Variation.php | 11 +-- tests/Unit/ProductTest.php | 150 ++++++++++++++++++++++++++++++++- 11 files changed, 356 insertions(+), 155 deletions(-) diff --git a/composer.json b/composer.json index 50e0e35..ef7fa6d 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": ">=7.0.0", + "php": ">=7.2.0", "ext-json": "*", "ext-openssl": "*" }, @@ -19,7 +19,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^7.0", "mockery/mockery": "^1.0", "fzaninotto/faker": "^1.4" }, @@ -27,5 +27,13 @@ "psr-4": { "Tests\\": "tests/" } + }, + "config": { + "preferred-install": "dist", + "sort-packages": true, + "optimize-autoloader": true, + "platform": { + "php": "7.2.0" + } } } diff --git a/composer.lock b/composer.lock index b8902f5..a852cbb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,32 +4,34 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a53aa27fc92345d16984371263c548ee", + "content-hash": "88e7e2fb42005ec60364302425bb6b5f", "packages": [], "packages-dev": [ { "name": "doctrine/instantiator", - "version": "1.1.0", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/doctrine/instantiator.git", - "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda" + "reference": "a2c590166b2133a4633738648b6b064edae0814a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/instantiator/zipball/185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", - "reference": "185b8868aa9bf7159f5f953ed5afb2d7fcdc3bda", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/a2c590166b2133a4633738648b6b064edae0814a", + "reference": "a2c590166b2133a4633738648b6b064edae0814a", "shasum": "" }, "require": { "php": "^7.1" }, "require-dev": { - "athletic/athletic": "~0.1.8", + "doctrine/coding-standard": "^6.0", "ext-pdo": "*", "ext-phar": "*", - "phpunit/phpunit": "^6.2.3", - "squizlabs/php_codesniffer": "^3.0.2" + "phpbench/phpbench": "^0.13", + "phpstan/phpstan-phpunit": "^0.11", + "phpstan/phpstan-shim": "^0.11", + "phpunit/phpunit": "^7.0" }, "type": "library", "extra": { @@ -54,12 +56,12 @@ } ], "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", - "homepage": "https://github.com/doctrine/instantiator", + "homepage": "https://www.doctrine-project.org/projects/instantiator.html", "keywords": [ "constructor", "instantiate" ], - "time": "2017-07-22T11:58:36+00:00" + "time": "2019-03-17T17:37:11+00:00" }, { "name": "fzaninotto/faker", @@ -591,40 +593,40 @@ }, { "name": "phpunit/php-code-coverage", - "version": "7.0.2", + "version": "6.1.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "cfca9c5f7f2694ca0c7749ffb142927d9f05250f" + "reference": "807e6013b00af69b6c5d9ceb4282d0393dbb9d8d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/cfca9c5f7f2694ca0c7749ffb142927d9f05250f", - "reference": "cfca9c5f7f2694ca0c7749ffb142927d9f05250f", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/807e6013b00af69b6c5d9ceb4282d0393dbb9d8d", + "reference": "807e6013b00af69b6c5d9ceb4282d0393dbb9d8d", "shasum": "" }, "require": { "ext-dom": "*", "ext-xmlwriter": "*", - "php": "^7.2", - "phpunit/php-file-iterator": "^2.0.2", + "php": "^7.1", + "phpunit/php-file-iterator": "^2.0", "phpunit/php-text-template": "^1.2.1", - "phpunit/php-token-stream": "^3.0.1", + "phpunit/php-token-stream": "^3.0", "sebastian/code-unit-reverse-lookup": "^1.0.1", - "sebastian/environment": "^4.1", + "sebastian/environment": "^3.1 || ^4.0", "sebastian/version": "^2.0.1", "theseer/tokenizer": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^7.0" }, "suggest": { - "ext-xdebug": "^2.6.1" + "ext-xdebug": "^2.6.0" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "7.0-dev" + "dev-master": "6.1-dev" } }, "autoload": { @@ -650,7 +652,7 @@ "testing", "xunit" ], - "time": "2019-02-15T13:40:27+00:00" + "time": "2018-10-31T16:06:48+00:00" }, { "name": "phpunit/php-file-iterator", @@ -843,16 +845,16 @@ }, { "name": "phpunit/phpunit", - "version": "8.0.4", + "version": "7.5.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "a7af0201285445c9c73c4bdf869c486e36b41604" + "reference": "eb343b86753d26de07ecba7868fa983104361948" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a7af0201285445c9c73c4bdf869c486e36b41604", - "reference": "a7af0201285445c9c73c4bdf869c486e36b41604", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/eb343b86753d26de07ecba7868fa983104361948", + "reference": "eb343b86753d26de07ecba7868fa983104361948", "shasum": "" }, "require": { @@ -862,25 +864,27 @@ "ext-libxml": "*", "ext-mbstring": "*", "ext-xml": "*", - "ext-xmlwriter": "*", "myclabs/deep-copy": "^1.7", "phar-io/manifest": "^1.0.2", "phar-io/version": "^2.0", - "php": "^7.2", + "php": "^7.1", "phpspec/prophecy": "^1.7", - "phpunit/php-code-coverage": "^7.0", + "phpunit/php-code-coverage": "^6.0.7", "phpunit/php-file-iterator": "^2.0.1", "phpunit/php-text-template": "^1.2.1", - "phpunit/php-timer": "^2.0", + "phpunit/php-timer": "^2.1", "sebastian/comparator": "^3.0", "sebastian/diff": "^3.0", - "sebastian/environment": "^4.1", + "sebastian/environment": "^4.0", "sebastian/exporter": "^3.1", - "sebastian/global-state": "^3.0", + "sebastian/global-state": "^2.0", "sebastian/object-enumerator": "^3.0.3", "sebastian/resource-operations": "^2.0", "sebastian/version": "^2.0.1" }, + "conflict": { + "phpunit/phpunit-mock-objects": "*" + }, "require-dev": { "ext-pdo": "*" }, @@ -895,7 +899,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "8.0-dev" + "dev-master": "7.5-dev" } }, "autoload": { @@ -921,7 +925,7 @@ "testing", "xunit" ], - "time": "2019-02-18T09:23:05+00:00" + "time": "2019-03-16T07:31:17+00:00" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -1210,26 +1214,23 @@ }, { "name": "sebastian/global-state", - "version": "3.0.0", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/global-state.git", - "reference": "edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4" + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4", - "reference": "edf8a461cf1d4005f19fb0b6b8b95a9f7fa0adc4", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", + "reference": "e8ba02eed7bbbb9e59e43dedd3dddeff4a56b0c4", "shasum": "" }, "require": { - "php": "^7.2", - "sebastian/object-reflector": "^1.1.1", - "sebastian/recursion-context": "^3.0" + "php": "^7.0" }, "require-dev": { - "ext-dom": "*", - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^6.0" }, "suggest": { "ext-uopz": "*" @@ -1237,7 +1238,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "2.0-dev" } }, "autoload": { @@ -1260,7 +1261,7 @@ "keywords": [ "global state" ], - "time": "2019-02-01T05:30:01+00:00" + "time": "2017-04-27T15:39:26+00:00" }, { "name": "sebastian/object-enumerator", @@ -1494,16 +1495,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.10.0", + "version": "v1.11.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" + "reference": "82ebae02209c21113908c229e9883c419720738a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/82ebae02209c21113908c229e9883c419720738a", + "reference": "82ebae02209c21113908c229e9883c419720738a", "shasum": "" }, "require": { @@ -1515,7 +1516,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.9-dev" + "dev-master": "1.11-dev" } }, "autoload": { @@ -1537,7 +1538,7 @@ }, { "name": "Gert de Pagter", - "email": "BackEndTea@gmail.com" + "email": "backendtea@gmail.com" } ], "description": "Symfony polyfill for ctype functions", @@ -1548,7 +1549,7 @@ "polyfill", "portable" ], - "time": "2018-08-06T14:22:27+00:00" + "time": "2019-02-06T07:57:58+00:00" }, { "name": "theseer/tokenizer", @@ -1648,7 +1649,12 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=7.0.0" + "php": ">=7.2.0", + "ext-json": "*", + "ext-openssl": "*" }, - "platform-dev": [] + "platform-dev": [], + "platform-overrides": { + "php": "7.2.0" + } } diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index ff5bc80..6b5c29a 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -45,22 +45,35 @@ public function getProperFormattedString($text) */ public function formatIntFloatString($value) { - $value = strip_tags(trim($value)); - - if(is_numeric($value) && !is_float($value)) + if(!is_numeric($value)) { - return (int)$value; + return 0; } - else if(is_numeric($value) && is_float($value)) + + if($this->isFloat($value)) { return (float)$value; } + else if(!$this->isFloat($value)) + { + return (int)$value; + } else { return $this->getProperFormattedString($value); } } + /** + * Is value float or not + * @param $num + * @return bool + */ + function isFloat($num) + { + return is_float($num) || is_numeric($num) && ((float) $num != (int) $num); + } + /** * Format url from an array * @param $array diff --git a/lib/Category.php b/lib/Category.php index ed12107..1c0abcd 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -32,6 +32,8 @@ public function getId() */ public function setId($id) { + $id = $this->getProperFormattedString($id); + $this->id = $id; } @@ -48,6 +50,8 @@ public function getName() */ public function setName($name) { + $name = $this->getProperFormattedString($name); + $this->name = $name; } @@ -64,6 +68,8 @@ public function getParent() */ public function setParent($parent) { + $parent = is_bool($parent) && !$parent ? false : $parent; + $this->parent = $parent; } @@ -89,15 +95,10 @@ public function setBreadcrumb(array $breadcrumb) */ public function prepareCategoryData() { - $id = $this->getProperFormattedString($this->getId()); - $name = $this->getProperFormattedString($this->getName()); - - $parent = is_bool($this->getParent()) && !$this->getParent() ? false : $this->getParent(); - return $this->toJSON([ - 'id' => $id, - 'name' => $name, - 'parent' => $parent, + 'id' => $this->getId(), + 'name' => $this->getName(), + 'parent' => $this->getParent(), 'breadcrumb' => $this->getBreadcrumb() ]); } diff --git a/lib/Checkout.php b/lib/Checkout.php index 5bdc792..4bb6ac8 100644 --- a/lib/Checkout.php +++ b/lib/Checkout.php @@ -25,6 +25,8 @@ public function getProductIds(): array */ public function setProductIds(array $productIds) { + $productIds = is_array($productIds) ? $productIds : (array)$productIds; + $this->productIds = $productIds; } @@ -34,10 +36,8 @@ public function setProductIds(array $productIds) */ public function prepareCheckoutIds() { - $productIds = is_array($this->getProductIds()) ? $this->getProductIds() : (array)$this->getProductIds(); - return $this->toJSON( - $productIds + $this->getProductIds() ); } } \ No newline at end of file diff --git a/lib/Email.php b/lib/Email.php index 4e9f762..2e70dbd 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -31,6 +31,8 @@ public function getEmail() */ public function setEmail($email) { + $email = EmailHelper::validate($email); + $this->email = $email; } @@ -47,6 +49,8 @@ public function getName() */ public function setName($name) { + $name = $this->getProperFormattedString($name); + $this->name = $name; } @@ -63,6 +67,8 @@ public function getPhone() */ public function setPhone($phone) { + $phone = $this->formatIntFloatString($phone); + $this->phone = $phone; } @@ -79,6 +85,8 @@ public function getCity() */ public function setCity($city) { + $city = $this->getProperFormattedString($city); + $this->city = $city; } @@ -95,6 +103,8 @@ public function getSex() */ public function setSex($sex) { + $sex = is_numeric($sex) ? $sex : (int)$sex; + $this->sex = $sex; } @@ -104,19 +114,12 @@ public function setSex($sex) */ public function prepareEmailData() { - $email = EmailHelper::validate($this->getEmail()); - $name = $this->getProperFormattedString($this->getName()); - $phone = $this->formatIntFloatString($this->getPhone()); - $city = $this->getProperFormattedString($this->getCity()); - - $sex = is_numeric($this->getSex()) ? $this->getSex() : (int)$this->getSex(); - return $this->toJSON([ - 'email' => $email, - 'name' => $name, - 'phone' => $phone, - 'city' => $city, - 'sex' => $sex + 'email' => $this->getEmail(), + 'name' => $this->getName(), + 'phone' => $this->getPhone(), + 'city' => $this->getCity(), + 'sex' => $this->getSex() ]); } } \ No newline at end of file diff --git a/lib/Order.php b/lib/Order.php index 82cf540..0dc08e9 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -38,6 +38,8 @@ public function getOrderNo() */ public function setOrderNo($orderNo) { + $orderNo = $this->formatIntFloatString($orderNo); + $this->orderNo = $orderNo; } @@ -54,6 +56,8 @@ public function getLastName() */ public function setLastName($lastName) { + $lastName = $this->getProperFormattedString($lastName); + $this->lastName = $lastName; } @@ -70,6 +74,8 @@ public function getFirstName() */ public function setFirstName($firstName) { + $firstName = $this->getProperFormattedString($firstName); + $this->firstName = $firstName; } @@ -86,6 +92,8 @@ public function getEmail() */ public function setEmail($email) { + $email = EmailHelper::sanitize($email, 'email'); + $this->email = $email; } @@ -102,6 +110,8 @@ public function getPhone() */ public function setPhone($phone) { + $phone = $this->getProperFormattedString($phone); + $this->phone = $phone; } @@ -118,6 +128,8 @@ public function getState() */ public function setState($state) { + $state = $this->getProperFormattedString($state); + $this->state = $state; } @@ -134,6 +146,8 @@ public function getCity() */ public function setCity($city) { + $city = $this->getProperFormattedString($city); + $this->city = $city; } @@ -150,6 +164,8 @@ public function getAddress() */ public function setAddress($address) { + $address = $this->getProperFormattedString($address); + $this->address = $address; } @@ -166,6 +182,8 @@ public function getDiscount() */ public function setDiscount($discount) { + $discount = $this->formatIntFloatString($discount); + $this->discount = $discount; } @@ -182,6 +200,8 @@ public function getDiscountCode() */ public function setDiscountCode($discountCode) { + $discountCode = $this->formatIntFloatString($discountCode); + $this->discountCode = $discountCode; } @@ -198,6 +218,8 @@ public function getShipping() */ public function setShipping($shipping) { + $shipping = $this->formatIntFloatString($shipping); + $this->shipping = $shipping; } @@ -214,6 +236,8 @@ public function getTotal() */ public function setTotal($total) { + $this->formatIntFloatString($total); + $this->total = $total; } @@ -223,32 +247,19 @@ public function setTotal($total) */ public function prepareOrderInformation() { - $orderNo = $this->formatIntFloatString($this->getOrderNo()); - $lastName = $this->getProperFormattedString($this->getLastName()); - $firstName = $this->getProperFormattedString($this->getFirstName()); - $email = EmailHelper::sanitize($this->getEmail(), 'email'); - $phone = $this->getProperFormattedString($this->getPhone()); - $state = $this->getProperFormattedString($this->getState()); - $city = $this->getProperFormattedString($this->getCity()); - $address = $this->getProperFormattedString($this->getAddress()); - $discount = $this->formatIntFloatString($this->getDiscount()); - $discountCode = $this->formatIntFloatString($this->getDiscountCode()); - $shipping = $this->formatIntFloatString($this->getShipping()); - $total = $this->formatIntFloatString($this->getTotal()); - return $this->toJSON([ - 'order_no' => $orderNo, - 'lastname' => $lastName, - 'firstname' => $firstName, - 'email' => $email, - 'phone' => $phone, - 'state' => $state, - 'city' => $city, - 'address' => $address, - 'discount' => $discount, - 'discount_code' => $discountCode, - 'shipping' => $shipping, - 'total' => $total + 'order_no' => $this->getOrderNo(), + 'lastname' => $this->getLastName(), + 'firstname' => $this->getFirstName(), + 'email' => $this->getEmail(), + 'phone' => $this->getPhone(), + 'state' => $this->getState(), + 'city' => $this->getCity(), + 'address' => $this->getAddress(), + 'discount' => $this->getDiscount(), + 'discount_code' => $this->getDiscountCode(), + 'shipping' => $this->getShipping(), + 'total' => $this->getTotal() ]); } } \ No newline at end of file diff --git a/lib/OrderProducts.php b/lib/OrderProducts.php index 544d107..5db7092 100644 --- a/lib/OrderProducts.php +++ b/lib/OrderProducts.php @@ -28,6 +28,8 @@ public function getId() */ public function setId($id) { + $id = $this->formatIntFloatString($id); + $this->id = $id; } @@ -44,6 +46,8 @@ public function getQuantity() */ public function setQuantity($quantity) { + $quantity = $this->formatIntFloatString($quantity); + $this->quantity = $quantity; } @@ -60,6 +64,8 @@ public function getPrice() */ public function setPrice($price) { + $price = $this->formatIntFloatString($price); + $this->price = $price; } @@ -76,6 +82,8 @@ public function getVariationCode() */ public function setVariationCode($variationCode) { + $variationCode = $this->getProperFormattedString($variationCode); + $this->variationCode = $variationCode; } @@ -86,10 +94,10 @@ public function setVariationCode($variationCode) public function prepareOrderProductsInfo() { return $this->toJSON([ - 'id' => $this->formatIntFloatString($this->getId()), - 'quantity' => $this->formatIntFloatString($this->getQuantity()), - 'price' => $this->formatIntFloatString($this->getPrice()), - 'variation_code' => $this->getProperFormattedString($this->getVariationCode()) + 'id' => $this->getId(), + 'quantity' => $this->getQuantity(), + 'price' => $this->getPrice(), + 'variation_code' => $this->getVariationCode() ]); } } \ No newline at end of file diff --git a/lib/Product.php b/lib/Product.php index e38ffd6..210191d 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -38,6 +38,8 @@ public function getId() */ public function setId($id) { + $id = $this->formatIntFloatString($id); + $this->id = $id; } @@ -54,6 +56,8 @@ public function getName() */ public function setName($name) { + $name = $this->getProperFormattedString($name); + $this->name = $name; } @@ -67,10 +71,12 @@ public function getUrl() /** * @param $url - * @return array|bool|mixed + * @throws \Exception */ public function setUrl($url) { + $url = UrlHelper::validate($url); + $this->url = $url; } @@ -83,10 +89,13 @@ public function getImg() } /** - * @param mixed $img + * @param $img + * @throws \Exception */ public function setImg($img) { + $img = UrlHelper::validate($img); + $this->img = $img; } @@ -103,6 +112,8 @@ public function getPrice() */ public function setPrice($price) { + $price = $this->formatIntFloatString($price); + $this->price = $price; } @@ -119,6 +130,15 @@ public function getPromo() */ public function setPromo($promo) { + if($promo > 0 && $promo < $this->getPrice()) + { + $promo = $this->formatIntFloatString($promo); + } + else + { + $promo = 0; + } + $this->promo = $promo; } @@ -135,6 +155,8 @@ public function getBrand() */ public function setBrand($brand) { + $brand = BrandHelper::validate($brand); + $this->brand = $brand; } @@ -151,6 +173,8 @@ public function getCategory() */ public function setCategory($category) { + $category = CategoryHelper::validate($category); + $this->category = $category; } @@ -167,6 +191,8 @@ public function getInventory() */ public function setInventory($inventory) { + $inventory = VariationsHelper::validate($inventory); + $this->inventory = $inventory; } @@ -183,6 +209,8 @@ public function getAdditionalImages() */ public function setAdditionalImages($additionalImages) { + $additionalImages = $this->validateArrayData($additionalImages); + $this->additionalImages = $additionalImages; } @@ -193,39 +221,17 @@ public function setAdditionalImages($additionalImages) */ public function prepareProductInformation() { - $id = $this->formatIntFloatString($this->getId()); - $name = $this->getProperFormattedString($this->getName()); - $url = UrlHelper::validate($this->getUrl()); - $img = UrlHelper::validate($this->getImg()); - - $price = $this->formatIntFloatString($this->getPrice()); - - if($this->getPromo() > 0 && $this->getPromo() < $this->getPrice()) - { - $promo = $this->formatIntFloatString($this->getPromo()); - } - else - { - $promo = 0; - } - - $brand = BrandHelper::validate($this->getBrand()); - $category = CategoryHelper::validate($this->getCategory()); - $inventory = VariationsHelper::validate($this->getInventory()); - - $additionalImages = $this->validateArrayData($this->getAdditionalImages()); - return [ - 'id' => $id, - 'name' => $name, - 'url' => $url, - 'img' => $img, - 'price' => $price, - 'promo' => $promo, - 'brand' => $brand, - 'category' => $category, - 'inventory' => $inventory, - 'images' => $additionalImages + 'id' => $this->getId(), + 'name' => $this->getName(), + 'url' => $this->getUrl(), + 'img' => $this->getImg(), + 'price' => $this->getPrice(), + 'promo' => $this->getPromo(), + 'brand' => $this->getBrand(), + 'category' => $this->getCategory(), + 'inventory' => $this->getInventory(), + 'images' => $this->getAdditionalImages() ]; } diff --git a/lib/Variation.php b/lib/Variation.php index 622d58c..72d8801 100644 --- a/lib/Variation.php +++ b/lib/Variation.php @@ -13,7 +13,7 @@ class Variation extends AbstractRetargetingSDK { protected $code = ''; - protected $stock = 0; + protected $stock = false; protected $details = []; /** @@ -70,13 +70,10 @@ public function setDetails(array $details) */ public function prepareVariationInfo() { - $code = CodeHelper::validate($this->getCode()); - $stock = (bool)$this->getStock(); - return $this->toJSON([ - 'code' => $code, - 'stock' => $stock, - 'details' => $this->getDetails() + 'code' => $this->getCode(), + 'stock' => (bool)$this->getStock(), + 'details' => $this->getDetails() ]); } } \ No newline at end of file diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index 71277f0..1524ecd 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -15,34 +15,182 @@ */ class ProductTest extends TestCase { + /** + * @throws \Exception + */ public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub $this->product = new Product(); $this->product->setUrl('http://google.ro'); $this->product->setImg('https://www.google.com/img.png'); } + /** + * Test if product has product id + */ public function test_if_product_has_id() { $this->product->setId(123); $this->assertNotNull($this->product->getId()); } + /** + * Test if product has name + */ public function test_if_product_has_name() { $this->product->setName('Fooo'); $this->assertNotNull($this->product->getName()); } + /** + * Test if name is a string + */ + public function test_if_product_name_is_string() + { + $this->product->setName('Galaxy Tab 10.0'); + $this->assertIsString($this->product->getName(), 'Galaxy Tab 11.0'); + } + + /** + * Test if product url is set up + */ public function test_if_product_url_is_set() { $this->assertEquals($this->product->getUrl(), 'http://google.ro'); } + /** + * Test if product has an image + */ public function test_if_product_has_image() { $this->assertEquals($this->product->getImg(), 'https://www.google.com/img.png'); } + + /** + * Test if product has price + */ + public function test_if_product_has_price() + { + $this->product->setPrice(100.20); + $this->assertNotNull($this->product->getPrice()); + } + + /** + * Test product price when is float + */ + public function test_when_product_price_is_float() + { + $this->product->setPrice('12.33'); + $this->assertEquals($this->product->getPrice(), 12.33); + } + + /** + * Test product price when is integer + */ + public function test_when_product_price_is_int() + { + $this->product->setPrice('12'); + $this->assertEquals($this->product->getPrice(), 12); + } + + public function test_when_product_promo_price_is_zero_and_promo_is_greater_than_price() + { + $this->product->setPrice(20); + $this->product->setPromo(80); + $this->assertEquals($this->product->getPromo(), 0); + } + + /** + * Test if product brand is array + */ + public function test_if_product_brand_is_array() + { + $this->product->setBrand([ + 'id' => 1, + 'name' => 'Apple' + ]); + + $this->assertIsArray($this->product->getBrand()); + } + + /** + * Test if product has brand + */ + public function test_if_product_has_brand() + { + $this->product->setBrand([ + 'id' => '1', + 'name' => 'Apple' + ]); + + $this->assertEquals($this->product->getBrand(), ['id' => 1, 'name' => 'Apple']); + } + + /** + * + */ + public function test_if_product_category_has_correct_format_with_only_one_parent_category() + { + $this->product->setCategory([ + [ + "id" => 12, + "name" => "Women footwear", + "parent" => false, + "breadcrumb" => [] + ] + ]); + + $this->assertEquals($this->product->getCategory(), [ + "id" => 12, + "name" => "Women footwear", + "parent" => false, + "breadcrumb" => [] + ]); + } + + /** + * Test if product has category + */ + public function test_if_product_has_category_with_parent_category_and_breadcrumb() + { + $this->product->setCategory([ + [ + "id" => 75, + "name" => "Men footwear", + "parent" => false, + "breadcrumb" => [] + ], + [ + "id" => 22, + "name" => "Sport sneakers", + "parent" => 21, + "breadcrumb" => [ + ["id" => 21, "name" => "Sneakers", "parent" => 20], + ["id" => 20, "name" => "Shoes", "parent" => false] + ] + ] + ]); + + $this->assertEquals($this->product->getCategory(), [ + [ + "id" => 75, + "name" => "Men footwear", + "parent" => false, + "breadcrumb" => [] + ], + [ + "id" => 22, + "name" => "Sport sneakers", + "parent" => 21, + "breadcrumb" => [ + ["id" => 21, "name" => "Sneakers", "parent" => 20], + ["id" => 20, "name" => "Shoes", "parent" => false] + ] + ] + ]); + } } + From 57873844fbc3623f6e9bf4009248aeebf87c4c24 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Tue, 26 Mar 2019 17:40:09 +0200 Subject: [PATCH 09/24] Changes for sdk --- lib/AbstractRetargetingSDK.php | 4 +- lib/Brand.php | 4 ++ lib/Helpers/AbstractHelper.php | 1 + lib/Helpers/UrlHelper.php | 5 ++ lib/Helpers/VariationsHelper.php | 2 +- readme.md | 90 +++++++++++++++++++++++++++++--- tests/Unit/BrandTest.php | 27 ++++++++++ tests/Unit/ProductTest.php | 87 ++++++++++++++++++++++++++++-- 8 files changed, 207 insertions(+), 13 deletions(-) diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index 6b5c29a..143f77c 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -7,6 +7,8 @@ */ namespace Retargeting; +use Retargeting\Helpers\UrlHelper; + /** * Class AbstractRetargetingSDK */ @@ -82,7 +84,7 @@ function isFloat($num) public function validateArrayData($array) { $mappedArray = array_map(function($item){ - return $this->getProperFormattedString($item); + return UrlHelper::validate($item); }, $array); return $mappedArray; diff --git a/lib/Brand.php b/lib/Brand.php index 0ef0ddf..3d8118e 100644 --- a/lib/Brand.php +++ b/lib/Brand.php @@ -47,6 +47,10 @@ public function setName($name) $this->name = $name; } + /** + * Prepare brand information + * @return string + */ public function prepareBrandInformation() { return $this->toJSON(BrandHelper::validate([ diff --git a/lib/Helpers/AbstractHelper.php b/lib/Helpers/AbstractHelper.php index 01fad28..846d89c 100644 --- a/lib/Helpers/AbstractHelper.php +++ b/lib/Helpers/AbstractHelper.php @@ -90,6 +90,7 @@ public static function _throwException($message) { $messages = array( "emptyURL" => "Url is required. Please don't leave it empty.", + "wrongUrl" => "The url has wrong format.", "emptyCustomerData" => "Customer data is required. Please don't leave it empty.", "emptyToken" => "Token is required. Please don't leave it empty.", "wrongFormat" => "The array format you provided is wrong." diff --git a/lib/Helpers/UrlHelper.php b/lib/Helpers/UrlHelper.php index 507510b..2b7bf26 100644 --- a/lib/Helpers/UrlHelper.php +++ b/lib/Helpers/UrlHelper.php @@ -30,6 +30,11 @@ public static function validate($url) $parsedUrl = parse_url($url); + if(!array_key_exists('host', $parsedUrl)) + { + self::_throwException('wrongUrl'); + } + if(isset($parsedUrl['scheme']) && !in_array($parsedUrl['scheme'], self::HTTP_PROTOCOLS)) { $url = self::prepend(filter_input(INPUT_GET, 'link', FILTER_SANITIZE_URL), self::HTTPS_VALUE) . $parsedUrl['path'] . '?' . $parsedUrl['query']; diff --git a/lib/Helpers/VariationsHelper.php b/lib/Helpers/VariationsHelper.php index 54aee9c..d69fa63 100644 --- a/lib/Helpers/VariationsHelper.php +++ b/lib/Helpers/VariationsHelper.php @@ -35,7 +35,7 @@ public static function validate($variation) if(array_key_exists('stock', $variation) && isset($variation['stock'])) { - $variationArr['name'] = $variation['stock']; + $variationArr['stock'] = $variation['stock']; } } diff --git a/readme.md b/readme.md index 8c4c921..d438418 100644 --- a/readme.md +++ b/readme.md @@ -1,12 +1,88 @@ -# Retargeting SDK +# Retargeting SDK +## Overview +Retargeting SDK is a software development tool for E-Commerce platforms that simplifies the implementation of Retargeting extension. + +##Minimum requirements +The Retargeting SDK requires at least PHP version 5.4.0 and it's also compatible with PHP >= 7.0.0 + +## How to install +Clone the repository in your platform root folder. + +##Example + +###Product class for sendProduct implementation ```php -require "vendor/autoload.php"; -$product = new \Retargeting\Product(); +use Retargeting/Product; + +$brand = [ + 'id' => 90, + 'name' => 'Nike' +]; + +$category = [ + [ + "id" => 75, + "name" => "Men footwear", + "parent" => false, + "breadcrumb" => [] + ], + [ + "id" => 22, + "name" => "Sport sneakers", + "parent" => 21, + "breadcrumb" => [ + ["id" => 21, "name" => "Sneakers", "parent" => 20], + ["id" => 20, "name" => "Shoes", "parent" => false] + ] +]; + +$inventory = [ + 'variations' => true, + 'stock' => [ + '42-B' => true, + '42-C' => false, + '42-R' => true, + ] +]; + +$product = new Product(); $product->setId(123); -$product->setName('Fooo'); -$product->setUrl('https://retargeting.biz'); +$product->setName('Shoes'); +$product->setUrl('https://www.example.com/shoes/sport-sneakers/sneaker_1'); +$product->setImg('https://www.example.com/catalog/image/sneaker_1.png'); +$product->setPrice(100.23); +$product->setPromo(80); +$product->setBrand($brand); +$product->setCategory($category); +$product->setInventory($category); +$product->setAdditionalImages($inventory) + +echo $product->prepareProductInformation(); +``` -var_dump($product->prepareProductInformation()); -``` \ No newline at end of file +| **Parameter** | **Type** | **Required** | **Description** | +|---|---|---|---| +| id | Number or text | Required | The product item identifier, ie. itemcode. It should identify to the sold product, but not necessarily some specific variant of the product. Must be unique in your site. | +| name | Text | Required | The product name | +| url | URL | Required | Complete URL of the item. Must start with http:// or https://. | +| img | URL | Required | Complete URL of an image of the item. | +| price | Number or text | Required | Current product price. If the product is on promotion (price is reduced) then this parameter gets the value of the price before promotion was applied to the product (old price). | +| promo | Number or text | Optional | Promotional price (new price). When the product isn’t on promotion (no reduced price), send value 0. | +| stock | Bool (0/1) | Required | Stock of the product.For product in stock send value 1. When the product isn’t on stock send value 0. | +| brand | Object | Required | Details about product brand. If the product does not belong to any brand, send false value. The object containing brand details, has the following properties: id, name. | +| brand.id | Number or text | Required | The brand item identifier. | +| brand.name | Text | Required | Brand name | +| category | Object | Required | An object that contain details about products category. The object should contain the following properties: id, name, parent | +| category.id | Number or text | Required | The category identifier | +| category.name | Text | Required | Category name | +| category.parent | Number, text, false | Required | Id of parent category. If there isn’t any parent category, send false value. | +| breadcrumb | Array | Required | Array containing all the parent categories of the category to which the product belongs (in this array you must not add the product category). If the category does not have a parent category (category.parent is set false), send an empty array. Each parent category is sent as object and contains the following properties: id, name, parent. | +| breadcrumb.id | Number or text | Required | Category id | +| breadcrumb.name | Text | Required | Category Name | +| breadcrumb.parent | Number, text, false | Required | Id of parent category. If there isn’t any parent category, send false value. | +| inventory | Object | Required | Inventory details | +| inventory.variations | True/False | Required | True for products with variations. False for products without variations. | +| inventory.stock | True/False/Object | Required | For product with variations, you should send an object with stock for each variations. | +| callback_function | Function | Optional | With this parameter you can define a function that runs itself after the action’s parent function executes | diff --git a/tests/Unit/BrandTest.php b/tests/Unit/BrandTest.php index 6ceb77f..1a7c366 100644 --- a/tests/Unit/BrandTest.php +++ b/tests/Unit/BrandTest.php @@ -25,15 +25,42 @@ public function setUp(): void $this->brand = new Brand(); } + /** + * Check if brand has id + */ public function testIfBrandHasId() { $this->brand->setId(33); $this->assertNotNull($this->brand->getId()); } + /** + * Check if brand has name + */ public function testIfBrandHasName() { $this->brand->setName('Nike'); $this->assertNotNull($this->brand->getName()); } + + /** + * Check if brand is array + */ + public function testIfBrandIsArray() + { + $this->brand->setId(23); + $this->brand->setName('Apple'); + + $this->assertNotNull($this->brand->prepareBrandInformation()); + } + + /** + * Check if brand has proper json format + */ + public function testIfBrandHasProperFormat() + { + $this->brand->setName('Samsung'); + + $this->assertEquals($this->brand->prepareBrandInformation(), json_encode(['id' => 99, 'name' => 'Samsung'], JSON_PRETTY_PRINT)); + } } diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index 1524ecd..7f63c1f 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -22,7 +22,7 @@ public function setUp(): void { $this->product = new Product(); - $this->product->setUrl('http://google.ro'); + $this->product->setUrl('http://www.google.ro'); $this->product->setImg('https://www.google.com/img.png'); } @@ -58,7 +58,7 @@ public function test_if_product_name_is_string() */ public function test_if_product_url_is_set() { - $this->assertEquals($this->product->getUrl(), 'http://google.ro'); + $this->assertEquals($this->product->getUrl(), 'http://www.google.ro'); } /** @@ -130,7 +130,7 @@ public function test_if_product_has_brand() } /** - * + * Test if product category has correct format with only one parent category without subcategory and breadcrumb */ public function test_if_product_category_has_correct_format_with_only_one_parent_category() { @@ -152,7 +152,7 @@ public function test_if_product_category_has_correct_format_with_only_one_parent } /** - * Test if product has category + * Test if product has category with parent category, subcategory and breadcrumb */ public function test_if_product_has_category_with_parent_category_and_breadcrumb() { @@ -192,5 +192,84 @@ public function test_if_product_has_category_with_parent_category_and_breadcrumb ] ]); } + + /** + * Test if product inventory is array + */ + public function test_if_product_inventory_is_array() + { + $this->product->setInventory([ + "variations" => true, + "stock" => [ + "42-B" => true, + "42-W" => false, + "43-B" => true, + "43-W" => true + ] + ]); + + $this->assertIsArray($this->product->getInventory()); + } + + /** + * Test if product inventory is array + */ + public function test_if_product_inventory_has_correct_format() + { + $this->product->setInventory([ + "variations" => true, + "stock" => [ + "42-B" => true, + "42-W" => false, + "43-B" => true, + "43-W" => true + ] + ]); + + $this->assertEquals($this->product->getInventory(), [ + "variations" => true, + "stock" => [ + "42-B" => true, + "42-W" => false, + "43-B" => true, + "43-W" => true + ] + ]); + } + + /** + * Test if product additional images is not null + */ + public function test_if_product_has_additional_images() + { + $this->product->setAdditionalImages([ + 'https://www.example.com/image/product-test-1.png', + 'https://www.example.com/image/product-test-2.png', + 'https://www.example.com/image/product-test-3.png', + 'https://www.example.com/image/product-test-4.png', + ]); + + $this->assertNotNull($this->product->getAdditionalImages()); + } + + /** + * Check if images url have proper format and data is returned correctly + */ + public function test_if_product_prepare_information_return_correct_format_array() + { + $this->product->setAdditionalImages([ + 'https://www.example.com/image/product-test-1.png', + 'https://www.example.com/image/product-test-2.png', + 'https://www.example.com/image/product-test-3.png', + 'https://www.example.com/image/product-test-4.png', + ]); + + $this->assertEquals($this->product->getAdditionalImages(), [ + 'https://www.example.com/image/product-test-1.png', + 'https://www.example.com/image/product-test-2.png', + 'https://www.example.com/image/product-test-3.png', + 'https://www.example.com/image/product-test-4.png', + ]); + } } From b09238d56ef86252a29ff3dfd8c5eb7bc514863c Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 10:23:48 +0200 Subject: [PATCH 10/24] Changes for sdk --- lib/Api/readme.md | 2 ++ readme.md | 14 +++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/Api/readme.md b/lib/Api/readme.md index 8a37d63..196ac38 100644 --- a/lib/Api/readme.md +++ b/lib/Api/readme.md @@ -2,6 +2,8 @@ ## Customers + + Each customer object must be encrypted using Encryption Helpers. When you develop this API Endpoint, you can leave it plain. Response example (plain) diff --git a/readme.md b/readme.md index d438418..7e62fbc 100644 --- a/readme.md +++ b/readme.md @@ -12,8 +12,8 @@ Clone the repository in your platform root folder. ##Example ###Product class for sendProduct implementation -```php +```php use Retargeting/Product; $brand = [ @@ -47,6 +47,14 @@ $inventory = [ ] ]; +$additionalImages = [ + 'https://www.example.com/catalog/image/sneaker_1.png', + 'https://www.example.com/catalog/image/sneaker_2.png', + 'https://www.example.com/catalog/image/sneaker_3.png', + 'https://www.example.com/catalog/image/sneaker_4.png', + 'https://www.example.com/catalog/image/sneaker_5.png', +]; + $product = new Product(); $product->setId(123); $product->setName('Shoes'); @@ -56,8 +64,8 @@ $product->setPrice(100.23); $product->setPromo(80); $product->setBrand($brand); $product->setCategory($category); -$product->setInventory($category); -$product->setAdditionalImages($inventory) +$product->setInventory($inventory); +$product->setAdditionalImages($additionalImages) echo $product->prepareProductInformation(); ``` From 5e0279b8bc5724a3ef604960d4fb8cccad5abf16 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 10:27:54 +0200 Subject: [PATCH 11/24] Changes for sdk --- readme.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 7e62fbc..daba0d0 100644 --- a/readme.md +++ b/readme.md @@ -11,7 +11,7 @@ Clone the repository in your platform root folder. ##Example -###Product class for sendProduct implementation +###Product class for sendProduct implementation (Sample) ```php use Retargeting/Product; @@ -70,6 +70,69 @@ $product->setAdditionalImages($additionalImages) echo $product->prepareProductInformation(); ``` +###Product class for sendProduct implementation (Response) + +```json +[ + { + "id": 42, + "name": "Apple Cinema 30\"", + "url": "http:\/\/localhost\/upload\/test", + "img": "http:\/\/localhost\/upload\/image\/catalog\/demo\/apple_cinema_30.jpg", + "price": 122, + "promo": 90, + "brand": { + "id": "8", + "name": "Apple" + }, + "category": [ + { + "id": "20", + "name": "Desktops", + "parent": false, + "breadcrumb": [] + }, + { + "id": "28", + "name": "Monitors", + "parent": "25", + "breadcrumb": [ + { + "id": "25", + "name": "Components", + "parent": false + } + ] + } + ], + "inventory": { + "variations": true, + "stock": [], + "name": { + "Small": true, + "Medium": true, + "Large": true, + "Checkbox 1": true, + "Checkbox 2": true, + "Checkbox 3": true, + "Checkbox 4": true, + "Red": true, + "Blue": true, + "Green": true, + "Yellow": true + } + }, + "images": [ + "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_logo.jpg", + "http:\/\/localhost\/upload\/image\/catalog\/demo\/hp_1.jpg", + "http:\/\/localhost\/upload\/image\/catalog\/demo\/compaq_presario.jpg", + "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_eos_5d_1.jpg", + "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_eos_5d_2.jpg" + ] + } +] +``` + | **Parameter** | **Type** | **Required** | **Description** | |---|---|---|---| | id | Number or text | Required | The product item identifier, ie. itemcode. It should identify to the sold product, but not necessarily some specific variant of the product. Must be unique in your site. | From a5df568df1e5f087331287ec254279ed078629a8 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 10:30:52 +0200 Subject: [PATCH 12/24] Changes for sdk --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index daba0d0..07d5284 100644 --- a/readme.md +++ b/readme.md @@ -1,13 +1,13 @@ # Retargeting SDK ## Overview -Retargeting SDK is a software development tool for E-Commerce platforms that simplifies the implementation of Retargeting extension. +Retargeting SDK is a software development tool for E-Commerce platforms that simplifies the implementation of Retargeting extension ##Minimum requirements The Retargeting SDK requires at least PHP version 5.4.0 and it's also compatible with PHP >= 7.0.0 ## How to install -Clone the repository in your platform root folder. +Clone the repository in your platform root folder ##Example From b40e1b74217c86a0a089235002c73afcda3ea281 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 10:38:16 +0200 Subject: [PATCH 13/24] Changes for sdk --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 07d5284..9fa1ab0 100644 --- a/readme.md +++ b/readme.md @@ -1,17 +1,17 @@ # Retargeting SDK ## Overview -Retargeting SDK is a software development tool for E-Commerce platforms that simplifies the implementation of Retargeting extension +Retargeting SDK is a software development tool for E-Commerce platforms that simplifies the implementation of Retargeting extension. -##Minimum requirements -The Retargeting SDK requires at least PHP version 5.4.0 and it's also compatible with PHP >= 7.0.0 +## Minimum requirements +The Retargeting SDK requires at least PHP version 5.4.0 and it's also compatible with PHP >= 7.0.0. ## How to install -Clone the repository in your platform root folder +Clone the repository in your platform root folder. -##Example +## Example -###Product class for sendProduct implementation (Sample) +### Product class for sendProduct implementation (Sample) ```php use Retargeting/Product; From 153e3546749066b807d5e53d2031d8db65d36c20 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 10:42:14 +0200 Subject: [PATCH 14/24] Changes for sdk --- readme.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 9fa1ab0..c79a91b 100644 --- a/readme.md +++ b/readme.md @@ -70,15 +70,15 @@ $product->setAdditionalImages($additionalImages) echo $product->prepareProductInformation(); ``` -###Product class for sendProduct implementation (Response) +### Product class for sendProduct implementation (Response) ```json [ { "id": 42, "name": "Apple Cinema 30\"", - "url": "http:\/\/localhost\/upload\/test", - "img": "http:\/\/localhost\/upload\/image\/catalog\/demo\/apple_cinema_30.jpg", + "url": "http://localhost/upload/test", + "img": "http://localhost/upload/image/catalog/demo/apple_cinema_30.jpg", "price": 122, "promo": 90, "brand": { @@ -123,11 +123,11 @@ echo $product->prepareProductInformation(); } }, "images": [ - "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_logo.jpg", - "http:\/\/localhost\/upload\/image\/catalog\/demo\/hp_1.jpg", - "http:\/\/localhost\/upload\/image\/catalog\/demo\/compaq_presario.jpg", - "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_eos_5d_1.jpg", - "http:\/\/localhost\/upload\/image\/catalog\/demo\/canon_eos_5d_2.jpg" + "http://localhost/upload/image/catalog/demo/canon_logo.jpg", + "http://localhost/upload/image/catalog/demo/hp_1.jpg", + "http://localhost/upload/image/catalog/demo/compaq_presario.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_1.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_2.jpg" ] } ] From e91ca44fa6e25be173f4caf6c3f5b8072be00b72 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Wed, 27 Mar 2019 11:04:13 +0200 Subject: [PATCH 15/24] Changes for sdk --- readme.md | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/readme.md b/readme.md index c79a91b..58eefdb 100644 --- a/readme.md +++ b/readme.md @@ -17,51 +17,50 @@ Clone the repository in your platform root folder. use Retargeting/Product; $brand = [ - 'id' => 90, - 'name' => 'Nike' + 'id' => 8, + 'name' => 'Apple' ]; $category = [ [ - "id" => 75, - "name" => "Men footwear", + "id" => 20, + "name" => "Desktop", "parent" => false, "breadcrumb" => [] ], [ - "id" => 22, - "name" => "Sport sneakers", - "parent" => 21, + "id" => 28, + "name" => "Monitors", + "parent" => 25, "breadcrumb" => [ - ["id" => 21, "name" => "Sneakers", "parent" => 20], - ["id" => 20, "name" => "Shoes", "parent" => false] + ["id" => 25, "name" => "Components", "parent" => false] ] ]; $inventory = [ 'variations' => true, 'stock' => [ - '42-B' => true, - '42-C' => false, - '42-R' => true, + 'Red' => true, + 'Small' => false, + 'Medium' => true, ] ]; $additionalImages = [ - 'https://www.example.com/catalog/image/sneaker_1.png', - 'https://www.example.com/catalog/image/sneaker_2.png', - 'https://www.example.com/catalog/image/sneaker_3.png', - 'https://www.example.com/catalog/image/sneaker_4.png', - 'https://www.example.com/catalog/image/sneaker_5.png', + "http://localhost/upload/image/catalog/demo/canon_logo.jpg", + "http://localhost/upload/image/catalog/demo/hp_1.jpg", + "http://localhost/upload/image/catalog/demo/compaq_presario.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_1.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_2.jpg" ]; $product = new Product(); -$product->setId(123); +$product->setId(42); $product->setName('Shoes'); -$product->setUrl('https://www.example.com/shoes/sport-sneakers/sneaker_1'); -$product->setImg('https://www.example.com/catalog/image/sneaker_1.png'); -$product->setPrice(100.23); -$product->setPromo(80); +$product->setUrl('http://localhost/upload/test'); +$product->setImg('http://localhost/upload/image/catalog/demo/apple_cinema_30.jpg'); +$product->setPrice(122); +$product->setPromo(90); $product->setBrand($brand); $product->setCategory($category); $product->setInventory($inventory); From a825f88105415821f4d52c6609ce08790b4248ec Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Thu, 28 Mar 2019 10:35:23 +0200 Subject: [PATCH 16/24] Changes for sdk --- lib/AbstractRetargetingSDK.php | 2 + lib/Api/StockManagement.php | 12 +-- lib/Category.php | 8 +- lib/Email.php | 5 +- lib/Helpers/AbstractHelper.php | 8 +- lib/Helpers/CategoryHelper.php | 34 ++++++- lib/Helpers/CustomersApiHelper.php | 2 + lib/Helpers/EmailHelper.php | 12 ++- lib/Order.php | 6 +- lib/OrderProducts.php | 103 --------------------- readme.md | 34 +++---- tests/Unit/ApiCustomersTest.php | 82 ++++++++++++++++- tests/Unit/BrandTest.php | 13 ++- tests/Unit/CategoryTest.php | 94 ++++++++++++++++++- tests/Unit/EmailTest.php | 142 ++++++++++++++++++++++++++++- tests/Unit/OrderProductsTest.php | 39 -------- tests/Unit/OrderTest.php | 141 +++++++++++++++++++++++++++- tests/Unit/ProductTest.php | 125 ++++++++++++++++++++++++- 18 files changed, 659 insertions(+), 203 deletions(-) delete mode 100644 lib/OrderProducts.php delete mode 100644 tests/Unit/OrderProductsTest.php diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index 143f77c..634f230 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -30,6 +30,8 @@ public function toJSON(array $data) */ public function getProperFormattedString($text) { + $text = (string)$text; + if ((bool)$text) { return trim(strip_tags(html_entity_decode( html_entity_decode($text), diff --git a/lib/Api/StockManagement.php b/lib/Api/StockManagement.php index 59a7227..1041a08 100644 --- a/lib/Api/StockManagement.php +++ b/lib/Api/StockManagement.php @@ -141,12 +141,12 @@ public function prepareStockInfo() { return [ 'productId' => $this->getProductId(), - 'name' => $this->getName(), - 'price' => $this->getPrice(), - 'promo' => $this->getPromo(), - 'image' => $this->getImage(), - 'url' => $this->getUrl(), - 'stock' => $this->isStock(), + 'name' => $this->getName(), + 'price' => $this->getPrice(), + 'promo' => $this->getPromo(), + 'image' => $this->getImage(), + 'url' => $this->getUrl(), + 'stock' => $this->isStock(), ]; } diff --git a/lib/Category.php b/lib/Category.php index 1c0abcd..e75f1b6 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -8,13 +8,15 @@ namespace Retargeting; +use Retargeting\Helpers\CategoryHelper; + /** * Class Category * @package Retargeting */ class Category extends AbstractRetargetingSDK { - protected $id = 0; + protected $id; protected $name = ''; protected $parent = false; protected $breadcrumb = []; @@ -68,7 +70,7 @@ public function getParent() */ public function setParent($parent) { - $parent = is_bool($parent) && !$parent ? false : $parent; + $parent = (is_bool($parent) && !$parent) || $parent === '' ? false : $parent; $this->parent = $parent; } @@ -86,6 +88,8 @@ public function getBreadcrumb() */ public function setBreadcrumb(array $breadcrumb) { + $breadcrumb = CategoryHelper::validateBreadcrumb($breadcrumb); + $this->breadcrumb = $breadcrumb; } diff --git a/lib/Email.php b/lib/Email.php index 2e70dbd..5d61744 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -27,7 +27,8 @@ public function getEmail() } /** - * @param mixed $email + * @param $email + * @throws \Exception */ public function setEmail($email) { @@ -67,7 +68,7 @@ public function getPhone() */ public function setPhone($phone) { - $phone = $this->formatIntFloatString($phone); + $phone = $this->getProperFormattedString($phone); $this->phone = $phone; } diff --git a/lib/Helpers/AbstractHelper.php b/lib/Helpers/AbstractHelper.php index 846d89c..3c49c04 100644 --- a/lib/Helpers/AbstractHelper.php +++ b/lib/Helpers/AbstractHelper.php @@ -52,9 +52,7 @@ public static function sanitize($var, $type) */ public static function formatString($string) { - $string = stripslashes(htmlspecialchars_decode(trim(strip_tags((string)$string)))); - - $string = self::sanitize($string, 'string'); + $string = trim(strip_tags((string)$string)); return $string; } @@ -93,7 +91,9 @@ public static function _throwException($message) "wrongUrl" => "The url has wrong format.", "emptyCustomerData" => "Customer data is required. Please don't leave it empty.", "emptyToken" => "Token is required. Please don't leave it empty.", - "wrongFormat" => "The array format you provided is wrong." + "wrongFormatToken" => "Token format is wrong.", + "wrongFormat" => "The array format you provided is wrong.", + "invalidEmail" => "Invalid email format." ); throw new \Exception($messages[$message]); diff --git a/lib/Helpers/CategoryHelper.php b/lib/Helpers/CategoryHelper.php index 6fd149f..75cc249 100644 --- a/lib/Helpers/CategoryHelper.php +++ b/lib/Helpers/CategoryHelper.php @@ -19,7 +19,7 @@ public static function validate($categoryData) { $categoryArr = []; - if(!empty($categoryData)) + if(is_array($categoryData) && !empty($categoryData)) { //Check if there are duplicated parent categories $categoryData = self::filterArrayByKey($categoryData, 'parent'); @@ -47,4 +47,36 @@ public static function validate($categoryData) return $categoryArr; } + + /** + * @param $breadcrumb + * @return array + */ + public static function validateBreadcrumb($breadcrumb) + { + $breadcrumbArr = []; + + if(is_array($breadcrumb) && !empty($breadcrumb)) + { + if(array_key_exists('0', $breadcrumb)) + { + foreach ($breadcrumb as $value) + { + $value['id'] = self::formatString($value['id']); + $value['name'] = self::formatString($value['name']); + $value['parent'] = is_bool($value['parent']) && !$value['parent'] ? false : $value['parent']; + + $breadcrumbArr[] = $value; + } + } + else { + $breadcrumbArr['id'] = $breadcrumb['id']; + $breadcrumbArr['name'] = self::formatString($breadcrumb['name']); + $breadcrumbArr['parent'] = is_bool($breadcrumb['parent']) && !$breadcrumb['parent'] ? false : $breadcrumb['parent']; + } + + } + + return $breadcrumbArr; + } } \ No newline at end of file diff --git a/lib/Helpers/CustomersApiHelper.php b/lib/Helpers/CustomersApiHelper.php index 199c9a5..1fc95d0 100644 --- a/lib/Helpers/CustomersApiHelper.php +++ b/lib/Helpers/CustomersApiHelper.php @@ -8,6 +8,8 @@ namespace Retargeting\Helpers; +use Retargeting\ApiCustomersTest; + final class CustomersApiHelper extends AbstractHelper implements Helper { const CUSTOMER_DATA_KEYS = [ diff --git a/lib/Helpers/EmailHelper.php b/lib/Helpers/EmailHelper.php index b5330cd..37ce207 100644 --- a/lib/Helpers/EmailHelper.php +++ b/lib/Helpers/EmailHelper.php @@ -11,12 +11,18 @@ final class EmailHelper extends AbstractHelper implements Helper { /** - * Format email properly - * @param mixed $email - * @return array|mixed + * Validate email + * @param $email + * @return mixed + * @throws \Exception */ public static function validate($email) { + if(!filter_var($email, FILTER_VALIDATE_EMAIL)) + { + self::_throwException('invalidEmail'); + } + $email = self::sanitize($email, 'email'); return $email; diff --git a/lib/Order.php b/lib/Order.php index 0dc08e9..2339b76 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -182,7 +182,7 @@ public function getDiscount() */ public function setDiscount($discount) { - $discount = $this->formatIntFloatString($discount); + $discount = $this->getProperFormattedString($discount); $this->discount = $discount; } @@ -200,7 +200,7 @@ public function getDiscountCode() */ public function setDiscountCode($discountCode) { - $discountCode = $this->formatIntFloatString($discountCode); + $discountCode = $this->getProperFormattedString($discountCode); $this->discountCode = $discountCode; } @@ -218,7 +218,7 @@ public function getShipping() */ public function setShipping($shipping) { - $shipping = $this->formatIntFloatString($shipping); + $shipping = $this->getProperFormattedString($shipping); $this->shipping = $shipping; } diff --git a/lib/OrderProducts.php b/lib/OrderProducts.php deleted file mode 100644 index 5db7092..0000000 --- a/lib/OrderProducts.php +++ /dev/null @@ -1,103 +0,0 @@ -id; - } - - /** - * @param mixed $id - */ - public function setId($id) - { - $id = $this->formatIntFloatString($id); - - $this->id = $id; - } - - /** - * @return boolean - */ - public function getQuantity() - { - return $this->quantity; - } - - /** - * @param boolean $quantity - */ - public function setQuantity($quantity) - { - $quantity = $this->formatIntFloatString($quantity); - - $this->quantity = $quantity; - } - - /** - * @return mixed - */ - public function getPrice() - { - return $this->price; - } - - /** - * @param mixed $price - */ - public function setPrice($price) - { - $price = $this->formatIntFloatString($price); - - $this->price = $price; - } - - /** - * @return mixed - */ - public function getVariationCode() - { - return $this->variationCode; - } - - /** - * @param mixed $variationCode - */ - public function setVariationCode($variationCode) - { - $variationCode = $this->getProperFormattedString($variationCode); - - $this->variationCode = $variationCode; - } - - /** - * Prepare order products for save order - * @return string - */ - public function prepareOrderProductsInfo() - { - return $this->toJSON([ - 'id' => $this->getId(), - 'quantity' => $this->getQuantity(), - 'price' => $this->getPrice(), - 'variation_code' => $this->getVariationCode() - ]); - } -} \ No newline at end of file diff --git a/readme.md b/readme.md index 58eefdb..6f217a4 100644 --- a/readme.md +++ b/readme.md @@ -11,8 +11,9 @@ Clone the repository in your platform root folder. ## Example -### Product class for sendProduct implementation (Sample) +### Product class for sendProduct implementation +#### Sample request ```php use Retargeting/Product; @@ -69,8 +70,7 @@ $product->setAdditionalImages($additionalImages) echo $product->prepareProductInformation(); ``` -### Product class for sendProduct implementation (Response) - +#### Sample response ```json [ { @@ -106,8 +106,7 @@ echo $product->prepareProductInformation(); ], "inventory": { "variations": true, - "stock": [], - "name": { + "stock": { "Small": true, "Medium": true, "Large": true, @@ -132,27 +131,28 @@ echo $product->prepareProductInformation(); ] ``` -| **Parameter** | **Type** | **Required** | **Description** | +| **Method** | **Type** | **Required** | **Description** | |---|---|---|---| -| id | Number or text | Required | The product item identifier, ie. itemcode. It should identify to the sold product, but not necessarily some specific variant of the product. Must be unique in your site. | -| name | Text | Required | The product name | -| url | URL | Required | Complete URL of the item. Must start with http:// or https://. | -| img | URL | Required | Complete URL of an image of the item. | -| price | Number or text | Required | Current product price. If the product is on promotion (price is reduced) then this parameter gets the value of the price before promotion was applied to the product (old price). | -| promo | Number or text | Optional | Promotional price (new price). When the product isn’t on promotion (no reduced price), send value 0. | -| stock | Bool (0/1) | Required | Stock of the product.For product in stock send value 1. When the product isn’t on stock send value 0. | -| brand | Object | Required | Details about product brand. If the product does not belong to any brand, send false value. The object containing brand details, has the following properties: id, name. | +| setId | Number or text | Required | The product item identifier, ie. itemcode. It should identify to the sold product, but not necessarily some specific variant of the product. Must be unique in your site. | +| setName | Text | Required | The product name | +| setUrl | URL | Required | Complete URL of the item. Must start with http:// or https://. | +| setImg | URL | Required | Complete URL of an image of the item. | +| setPrice | Number or text | Required | Current product price. If the product is on promotion (price is reduced) then this parameter gets the value of the price before promotion was applied to the product (old price). | +| setPromo | Number or text | Optional | Promotional price (new price). When the product isn’t on promotion (no reduced price), send value 0. | +| setStock | Bool (0/1) | Required | Stock of the product.For product in stock send value 1. When the product isn’t on stock send value 0. | +| setBrand | Object | Required | Details about product brand. If the product does not belong to any brand, send false value. The object containing brand details, has the following properties: id, name. | | brand.id | Number or text | Required | The brand item identifier. | | brand.name | Text | Required | Brand name | -| category | Object | Required | An object that contain details about products category. The object should contain the following properties: id, name, parent | +| setCategory | Object | Required | An object that contain details about products category. The object should contain the following properties: id, name, parent | | category.id | Number or text | Required | The category identifier | | category.name | Text | Required | Category name | | category.parent | Number, text, false | Required | Id of parent category. If there isn’t any parent category, send false value. | -| breadcrumb | Array | Required | Array containing all the parent categories of the category to which the product belongs (in this array you must not add the product category). If the category does not have a parent category (category.parent is set false), send an empty array. Each parent category is sent as object and contains the following properties: id, name, parent. | +| setBreadcrumb | Array | Required | Array containing all the parent categories of the category to which the product belongs (in this array you must not add the product category). If the category does not have a parent category (category.parent is set false), send an empty array. Each parent category is sent as object and contains the following properties: id, name, parent. | | breadcrumb.id | Number or text | Required | Category id | | breadcrumb.name | Text | Required | Category Name | | breadcrumb.parent | Number, text, false | Required | Id of parent category. If there isn’t any parent category, send false value. | -| inventory | Object | Required | Inventory details | +| setInventory | Object | Required | Inventory details | | inventory.variations | True/False | Required | True for products with variations. False for products without variations. | | inventory.stock | True/False/Object | Required | For product with variations, you should send an object with stock for each variations. | +| setAdditionalImages | Object | Required | All product images can be assigned here. Accepts an object of urls. | callback_function | Function | Optional | With this parameter you can define a function that runs itself after the action’s parent function executes | diff --git a/tests/Unit/ApiCustomersTest.php b/tests/Unit/ApiCustomersTest.php index a1b4cbf..26ffcf4 100644 --- a/tests/Unit/ApiCustomersTest.php +++ b/tests/Unit/ApiCustomersTest.php @@ -8,14 +8,19 @@ namespace Retargeting; - use PHPUnit\Framework\TestCase; +use Retargeting\Api\Customers; +use Retargeting\Helpers\DecryptionHelper; +use Retargeting\Helpers\EncryptionHelper; /** - * @property Product product + * Class ApiCustomersTest + * @package Tests\Unit + * @property Customers customersInstance */ class ApiCustomersTest extends TestCase { + const TOKEN = "df2ce5cba06265db9bffeb6caf8d9fcf46a5a1712f774bca67535a82bdcf1955"; protected $customer = [ 'firstName' => 'John', @@ -25,11 +30,80 @@ class ApiCustomersTest extends TestCase 'status' => true ]; + /** + * @throws \Exception + */ public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub + $this->customersInstance = new Customers(self::TOKEN); + + $this->customersInstance->setToken(self::TOKEN); + $this->customersInstance->setData($this->customer); + $this->customersInstance->setCurrentPage(2); + $this->customersInstance->setLastPage(120); + $this->customersInstance->setPrevPage('https://www.example.com/api/retargetingtracker?page=1'); + $this->customersInstance->setNextPage('https://www.example.com/api/retargetingtracker?page=3'); + } + + /** + * Test if customer has data + */ + public function testIfCustomersHasData() + { + $this->assertNotEmpty($this->customersInstance->getData()); + } + + /** + * Test if token is not null + */ + public function testIfCustomerHasToken() + { + $this->assertNotNull($this->customersInstance->getToken()); + } + + /** + * Test if page related data is not empty + */ + public function testIfCustomerHasPageData() + { + $this->assertNotNull($this->customersInstance->getCurrentPage()); + $this->assertNotNull($this->customersInstance->getLastPage()); + $this->assertNotNull($this->customersInstance->getNextPage()); + $this->assertNotNull($this->customersInstance->getPrevPage()); + } + + /** + * Test if token is type of hashed + * @throws Exceptions\DecryptException + * @throws Exceptions\RTGException + */ + public function testIfCustomerDataIsHashed() + { + $encryption = new EncryptionHelper(self::TOKEN); + + $data = $encryption->encrypt(json_encode($this->customer, JSON_PRETTY_PRINT)); + $this->customersInstance->setData($data); + + $decryption = new DecryptionHelper(self::TOKEN); + + $decryptedData = $decryption->decrypt($this->customersInstance->getData()); + + $this->assertEquals(json_decode($decryptedData, JSON_PRETTY_PRINT), $this->customer); } - //@TODO: write tests + /** + * Test if customer prepare api information has proper format + * @throws \Exception + */ + public function testIfCustomerPrepareApiInfoHasProperFormat() + { + $this->assertEquals($this->customersInstance->prepareCustomersApiInfo(), json_encode([ + 'data' => $this->customer, + 'current_page' => 2, + 'last_page' => 120, + 'next_page' => 'https://www.example.com/api/retargetingtracker?page=3', + 'prev_page' => 'https://www.example.com/api/retargetingtracker?page=1' + ], JSON_PRETTY_PRINT)); + } } diff --git a/tests/Unit/BrandTest.php b/tests/Unit/BrandTest.php index 1a7c366..7a98503 100644 --- a/tests/Unit/BrandTest.php +++ b/tests/Unit/BrandTest.php @@ -20,8 +20,6 @@ class BrandTest extends TestCase { public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub - $this->brand = new Brand(); } @@ -44,14 +42,14 @@ public function testIfBrandHasName() } /** - * Check if brand is array + * Check if brand prepare information is json */ - public function testIfBrandIsArray() + public function testIfBrandPrepareInformationIsJson() { $this->brand->setId(23); $this->brand->setName('Apple'); - $this->assertNotNull($this->brand->prepareBrandInformation()); + $this->assertJson($this->brand->prepareBrandInformation()); } /** @@ -59,8 +57,9 @@ public function testIfBrandIsArray() */ public function testIfBrandHasProperFormat() { - $this->brand->setName('Samsung'); + $this->brand->setId(9000); + $this->brand->setName('Adidas'); - $this->assertEquals($this->brand->prepareBrandInformation(), json_encode(['id' => 99, 'name' => 'Samsung'], JSON_PRETTY_PRINT)); + $this->assertEquals($this->brand->prepareBrandInformation(), json_encode(['id' => 9000, 'name' => 'Adidas'], JSON_PRETTY_PRINT)); } } diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php index 0c093a5..de12ef7 100644 --- a/tests/Unit/CategoryTest.php +++ b/tests/Unit/CategoryTest.php @@ -18,14 +18,102 @@ class CategoryTest extends TestCase { public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub - $this->category = new Category(); + + $this->category->setId(89); + $this->category->setName('Shoes'); + $this->category->setParent(''); + + $this->category->setBreadcrumb([ + ["id" => 21, "name" => "Sneakers", "parent" => 20], + ["id" => 20, "name" => "Shoes", "parent" => false] + ]); } + /** + * Check if category has identifier + */ public function testIfCategoryHasId() { - $this->category->setId(89); $this->assertNotNull($this->category->getId()); } + + /** + * Check if category has name + */ + public function testIfCategoryHasName() + { + $this->assertNotNull($this->category->getName()); + } + + /** + * Check if category has parent or not. If not return false. + */ + public function testIfCategoryHasParent() + { + $this->assertNotNull($this->category->getParent()); + } + + /** + * Check if category has no parent category + */ + public function testIfCategoryHasNoParent() + { + $this->assertFalse($this->category->getParent()); + } + + /** + * Check if there no breadcrumb set up + */ + public function testIfCategoryHasNoBreadcrumb() + { + $this->category->setBreadcrumb([]); + + $this->assertEquals($this->category->getBreadcrumb(), []); + } + + /** + * Check if there is only one record for breadcrumb + */ + public function testIfCategoryHasOneCategoryBreadcrumb() + { + $this->category->setBreadcrumb([ + 'id' => 2, + 'name' => "Men's footwear", + 'parent' => false + ]); + + $this->assertEquals($this->category->getBreadcrumb(), [ + 'id' => 2, + 'name' => "Men's footwear", + 'parent' => false + ]); + } + + /** + * Check if there is more the on record for breadcrumb + */ + public function testIfCategoryHasTwoOreMoreCategoryBreadcrumb() + { + $this->assertEquals($this->category->getBreadcrumb(), [ + ["id" => 21, "name" => "Sneakers", "parent" => 20], + ["id" => 20, "name" => "Shoes", "parent" => false] + ]); + } + + /** + * Check if prepare category data return correct formed json + */ + public function testIfCategoryPrepareDataHasProperFormat() + { + $this->assertEquals($this->category->prepareCategoryData(), json_encode([ + 'id' => '89', + 'name' => 'Shoes', + 'parent' => false, + 'breadcrumb' => [ + ["id" => '21', "name" => "Sneakers", "parent" => 20], + ["id" => '20', "name" => "Shoes", "parent" => false] + ] + ], JSON_PRETTY_PRINT)); + } } \ No newline at end of file diff --git a/tests/Unit/EmailTest.php b/tests/Unit/EmailTest.php index ee980b3..f3b0bea 100644 --- a/tests/Unit/EmailTest.php +++ b/tests/Unit/EmailTest.php @@ -18,15 +18,149 @@ class EmailTest extends TestCase { public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub - $this->email = new Email(); } + /** + * Test if email is not empty + */ public function test_if_email_is_not_empty() { - $this->email->setEmail('test@google.com'); + $this->email->setEmail('john.doe@mail.com'); + + $this->assertNotNull($this->email->getEmail()); + } + + /** + * Test if email has proper format + */ + public function test_if_email_has_proper_format() + { + $this->email->setEmail('john.doe@mail.com'); + + $this->assertRegExp('/^.+\@\S+\.\S+$/', $this->email->getEmail()); + } + + /** + * Test if name is not empty + */ + public function test_if_name_is_not_empty() + { + $this->email->setName('John Doe'); + + $this->assertNotNull($this->email->getName()); + } + + /** + * Test if name is string + */ + public function test_if_name_is_string() + { + $this->email->setName('John Doe'); + + $this->assertIsString($this->email->getName()); + } + + /** + * Test if phone number is not empty + */ + public function test_if_phone_is_not_empty() + { + $this->email->setPhone('(298) 407-4029'); + + $this->assertNotNull($this->email->getPhone()); + } + + /** + * Test if phone number is not empty + */ + public function test_if_phone_is_string() + { + $this->email->setPhone('(298) 407-4029'); + + $this->assertIsString($this->email->getPhone()); + } + + /** + * Test if phone has proper format + */ + public function test_if_phone_has_proper_format() + { + $this->email->setPhone(' (298) 407-4029 '); + + $this->assertEquals($this->email->getPhone(), '(298) 407-4029'); + } + + /** + * Test if city number is not empty + */ + public function test_if_city_is_not_empty() + { + $this->email->setCity('Berlin'); + + $this->assertNotNull($this->email->getCity()); + } + + /** + * Test if city number is not empty + */ + public function test_if_city_is_string() + { + $this->email->setCity('Berlin'); + + $this->assertIsString($this->email->getCity()); + } + + /** + * Test if city has proper format + */ + public function test_if_city_has_proper_format() + { + $this->email->setCity('Berlin '); + + $this->assertEquals($this->email->getCity(), 'Berlin'); + } + + /** + * Test if sex is not empty + */ + public function test_if_sex_is_not_empty() + { + $this->email->setSex(0); + + $this->assertNotNull($this->email->getSex()); + } + + /** + * Test if sex is of type boolean + */ + public function test_if_sex_is_boolean() + { + $this->email->setSex(1); + + $this->assertIsNumeric($this->email->getSex()); + } + + /** + * Test if prepare email data return proper formatted json + * @throws \Exception + */ + public function test_if_prepare_email_data_has_proper_format() + { + $this->email->setEmail('john.doe@mail.com'); + $this->email->setName('John Doe'); + $this->email->setPhone('(298) 407-4029'); + $this->email->setCity('Berlin'); + $this->email->setSex(0); - $this->assertNotEmpty($this->email->getEmail()); + $this->assertEquals($this->email->prepareEmailData(), + json_encode([ + 'email' => 'john.doe@mail.com', + 'name' => 'John Doe', + 'phone' => '(298) 407-4029', + 'city' => 'Berlin', + 'sex' => 0 + ], JSON_PRETTY_PRINT) + ); } } \ No newline at end of file diff --git a/tests/Unit/OrderProductsTest.php b/tests/Unit/OrderProductsTest.php deleted file mode 100644 index bf5d018..0000000 --- a/tests/Unit/OrderProductsTest.php +++ /dev/null @@ -1,39 +0,0 @@ -orderProducts = new OrderProducts(); - } - - public function testIfOrderProductsHasId() - { - $this->orderProducts->setId(111); - $this->assertNotNull($this->orderProducts->getId()); - } - - public function testIfOrderProductsHasQuantity() - { - $this->orderProducts->setQuantity(1); - $this->assertNotNull($this->orderProducts->getQuantity()); - } -} \ No newline at end of file diff --git a/tests/Unit/OrderTest.php b/tests/Unit/OrderTest.php index a391b9b..8e187e3 100644 --- a/tests/Unit/OrderTest.php +++ b/tests/Unit/OrderTest.php @@ -16,17 +16,150 @@ */ class OrderTest extends TestCase { + /** + * + */ public function setUp(): void { - parent::setUp(); // TODO: Change the autogenerated stub - $this->order = new Order(); + $this->order->setOrderNo(28); + $this->order->setLastName('Doe'); + $this->order->setFirstName('John'); + $this->order->setEmail('john.doe@mail.com'); + $this->order->setPhone('40771445255'); + $this->order->setState('Germany'); + $this->order->setCity('Berlin'); + $this->order->setAddress('Sample address'); + $this->order->setDiscount(20); + $this->order->setDiscountCode('RAX204'); + $this->order->setShipping('Sample shipping street'); + $this->order->setTotal(396); } + /** + * Test if order has order number + */ public function test_if_order_has_no() { - $this->order->setOrderNo(28); - $this->assertNotNull($this->order->getOrderNo()); } + + /** + * Test if order has last name + */ + public function test_if_order_has_last_name() + { + $this->assertNotNull($this->order->getLastName()); + } + + /** + * Test if order has first name + */ + public function test_if_order_has_first_name() + { + $this->assertNotNull($this->order->getFirstName()); + } + + /** + * Test if order has email + */ + public function test_if_order_has_email() + { + $this->assertNotNull($this->order->getEmail()); + } + + /** + * Test if order email is valid + */ + public function test_if_order_has_valid_email() + { + $this->assertRegExp('/^.+\@\S+\.\S+$/', $this->order->getEmail()); + } + + /** + * Test if order has phone + */ + public function test_if_order_has_phone() + { + $this->assertNotNull($this->order->getPhone()); + } + + /** + * Test if order has state + */ + public function test_if_order_has_state() + { + $this->assertNotNull($this->order->getState()); + } + + /** + * Test if order has city + */ + public function test_if_order_has_city() + { + $this->assertNotNull($this->order->getCity()); + } + + /** + * Test if order has address + */ + public function test_if_order_has_address() + { + $this->assertNotNull($this->order->getAddress()); + } + + /** + * Test if order has discount + */ + public function test_if_order_has_discount() + { + $this->assertNotNull($this->order->getDiscount()); + } + + /** + * Test if order has discount code + */ + public function test_if_order_has_discount_code() + { + $this->assertNotNull($this->order->getDiscountCode()); + } + + /** + * Test if order has shipping + */ + public function test_if_order_has_shipping() + { + $this->assertNotNull($this->order->getShipping()); + } + + /** + * Test if order has total + */ + public function test_if_order_has_total() + { + $this->assertNotNull($this->order->getTotal()); + } + + /** + * Test if order prepare information has correct json format + */ + public function test_if_order_prepare_information_has_correct_json_format() + { + $order = [ + 'order_no' => 28, + 'lastname' => 'Doe', + 'firstname' => 'John', + 'email' => 'john.doe@mail.com', + 'phone' => '40771445255', + 'state' => 'Germany', + 'city' => 'Berlin', + 'address' => 'Sample address', + 'discount' => "20", + 'discount_code' => 'RAX204', + 'shipping' => 'Sample shipping street', + 'total' => 396 + ]; + + $this->assertEquals($this->order->prepareOrderInformation(), json_encode($order, JSON_PRETTY_PRINT)); + } } \ No newline at end of file diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index 7f63c1f..b02af81 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -255,7 +255,7 @@ public function test_if_product_has_additional_images() /** * Check if images url have proper format and data is returned correctly */ - public function test_if_product_prepare_information_return_correct_format_array() + public function test_if_product_additional_images_return_correct_format_array() { $this->product->setAdditionalImages([ 'https://www.example.com/image/product-test-1.png', @@ -271,5 +271,128 @@ public function test_if_product_prepare_information_return_correct_format_array( 'https://www.example.com/image/product-test-4.png', ]); } + + /** + * Check product prepare information returns correct array format + */ + public function test_if_product_prepare_information_return_correct_format_array() + { + $brand = [ + 'id' => "8", + 'name' => 'Apple' + ]; + + $category = [ + [ + "id" => "20", + "name" => "Desktops", + "parent" => false, + "breadcrumb" => [] + ], + [ + "id" => "28", + "name" => "Monitors", + "parent" => "25", + "breadcrumb" => [ + ["id" => "25", "name" => "Components", "parent" => false] + ] + ] + ]; + + $inventory = [ + 'variations' => true, + 'stock' => [ + "Small" => true, + "Medium" => true, + "Large" => true, + "Checkbox 1" => true, + "Checkbox 2" => true, + "Checkbox 3" => true, + "Checkbox 4" => true, + "Red" => true, + "Blue" => true, + "Green" => true, + "Yellow" => true + ] + ]; + + $additionalImages = [ + "http://localhost/upload/image/catalog/demo/canon_logo.jpg", + "http://localhost/upload/image/catalog/demo/hp_1.jpg", + "http://localhost/upload/image/catalog/demo/compaq_presario.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_1.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_2.jpg" + ]; + + $product = new Product(); + $product->setId(42); + $product->setName('Apple Cinema 30"'); + $product->setUrl('http://localhost/upload/test'); + $product->setImg('http://localhost/upload/image/catalog/demo/apple_cinema_30.jpg'); + $product->setPrice(122); + $product->setPromo(90); + $product->setBrand($brand); + $product->setCategory($category); + $product->setInventory($inventory); + $product->setAdditionalImages($additionalImages); + + $result = json_encode([ + "id" => 42, + "name" => "Apple Cinema 30\"", + "url" => "http://localhost/upload/test", + "img" => "http://localhost/upload/image/catalog/demo/apple_cinema_30.jpg", + "price" => 122, + "promo" => 90, + "brand" => [ + "id" => "8", + "name" => "Apple" + ], + "category" => [ + [ + "id" => "20", + "name" => "Desktops", + "parent" => false, + "breadcrumb" => [] + ], + [ + "id" => "28", + "name" => "Monitors", + "parent" => "25", + "breadcrumb" => [ + [ + "id" => "25", + "name" => "Components", + "parent" => false + ] + ] + ] + ], + "inventory" => [ + "variations" => true, + "stock" => [ + "Small" => true, + "Medium" => true, + "Large" => true, + "Checkbox 1" => true, + "Checkbox 2" => true, + "Checkbox 3" => true, + "Checkbox 4" => true, + "Red" => true, + "Blue" => true, + "Green" => true, + "Yellow" => true + ] + ], + "images" => [ + "http://localhost/upload/image/catalog/demo/canon_logo.jpg", + "http://localhost/upload/image/catalog/demo/hp_1.jpg", + "http://localhost/upload/image/catalog/demo/compaq_presario.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_1.jpg", + "http://localhost/upload/image/catalog/demo/canon_eos_5d_2.jpg" + ] + ], JSON_PRETTY_PRINT); + + $this->assertEquals($product->prepareProductInformationToJson(), $result); + } } From 7ac3e66da9b1d619edfef67bd5d738062138c9d8 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Thu, 28 Mar 2019 10:38:21 +0200 Subject: [PATCH 17/24] Changes for sdk --- readme.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 6f217a4..22af64d 100644 --- a/readme.md +++ b/readme.md @@ -138,8 +138,7 @@ echo $product->prepareProductInformation(); | setUrl | URL | Required | Complete URL of the item. Must start with http:// or https://. | | setImg | URL | Required | Complete URL of an image of the item. | | setPrice | Number or text | Required | Current product price. If the product is on promotion (price is reduced) then this parameter gets the value of the price before promotion was applied to the product (old price). | -| setPromo | Number or text | Optional | Promotional price (new price). When the product isn’t on promotion (no reduced price), send value 0. | -| setStock | Bool (0/1) | Required | Stock of the product.For product in stock send value 1. When the product isn’t on stock send value 0. | +| setPromo | Number or text | Optional | Promotional price (new price). When the product isn’t on promotion (no reduced price), send value 0. | | | setBrand | Object | Required | Details about product brand. If the product does not belong to any brand, send false value. The object containing brand details, has the following properties: id, name. | | brand.id | Number or text | Required | The brand item identifier. | | brand.name | Text | Required | Brand name | From 3c9e8450cf79da93b3087ca67559775055a94890 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Thu, 28 Mar 2019 11:51:22 +0200 Subject: [PATCH 18/24] Changes for sdk --- tests/Unit/StockManagementTest.php | 115 +++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/Unit/StockManagementTest.php diff --git a/tests/Unit/StockManagementTest.php b/tests/Unit/StockManagementTest.php new file mode 100644 index 0000000..8039f84 --- /dev/null +++ b/tests/Unit/StockManagementTest.php @@ -0,0 +1,115 @@ + 'HAF220', + 'name' => 'Samsung Galaxy Tab 10.0', + 'price' => '990.90', + 'promo' => '870.20', + 'image' => 'https://www.example.com/catalog/image/samsung-galaxy-tab.png', + 'url' => 'https://www.example.com/tablets/samsung-galaxy-tab', + 'stock' => true + ]; + + public function setUp(): void + { + $this->stock = new StockManagement(); + + $this->stock->setProductId('HAF220'); + $this->stock->setName('Samsung Galaxy Tab 10.0'); + $this->stock->setPrice(990.90); + $this->stock->setPromo(870.20); + $this->stock->setImage('https://www.example.com/catalog/image/samsung-galaxy-tab.png'); + $this->stock->setUrl('https://www.example.com/tablets/samsung-galaxy-tab'); + $this->stock->setStock(true); + } + +// protected $productId; +// protected $name; +// protected $price; +// protected $promo; +// protected $image; +// protected $url; +// protected $stock = false; + + /** + * Test if stock management has product Id + */ + public function testIfStockManagementHasProductId() + { + $this->assertNotNull($this->stock->getProductId()); + } + + /** + * Test if stock management has name + */ + public function testIfStockManagementHasName() + { + $this->assertNotNull($this->stock->getName()); + } + + /** + * Test if stock management has price + */ + public function testIfStockManagementHasPrice() + { + $this->assertNotNull($this->stock->getPrice()); + } + + /** + * Test if stock management has promo + */ + public function testIfStockManagementHasPromo() + { + $this->assertNotNull($this->stock->getPromo()); + } + + /** + * Test if stock management has image + */ + public function testIfStockManagementHasImage() + { + $this->assertNotNull($this->stock->getImage()); + } + + /** + * Test if stock management has url + */ + public function testIfStockManagementHasUrl() + { + $this->assertNotNull($this->stock->getUrl()); + } + + /** + * Test if stock management is bool + */ + public function testIfStockManagementIsBool() + { + $this->assertIsBool($this->stock->isStock()); + } + + /** + * Test if stock management prepare info has proper format + */ + public function testIfStockManagementPrepareStockInfoHasProperFormat() + { + $this->assertEquals($this->stock->prepareStockInfo(), $this->stockSample); + } +} \ No newline at end of file From f007aab2b17b559810843ceb80c798f4363588c4 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Thu, 28 Mar 2019 12:16:23 +0200 Subject: [PATCH 19/24] Changes for sdk --- composer.json | 2 +- lib/Api/readme.md | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index ef7fa6d..7e7aa01 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^7.0", + "phpunit/phpunit": "8.0", "mockery/mockery": "^1.0", "fzaninotto/faker": "^1.4" }, diff --git a/lib/Api/readme.md b/lib/Api/readme.md index 196ac38..8f867a7 100644 --- a/lib/Api/readme.md +++ b/lib/Api/readme.md @@ -2,8 +2,6 @@ ## Customers - - Each customer object must be encrypted using Encryption Helpers. When you develop this API Endpoint, you can leave it plain. Response example (plain) @@ -51,3 +49,29 @@ Response example (encrypted) } ``` + +## Stock Management + +Call api stock management each time you make some changes on your products. + +Request example + +```php +use Retargeting/Api; + +$stock = new StockManagement(); + +$stock->setProductId(123); +$stock->setName('Canon EOS 5D'); +$stock->setPrice(900); +$stock->setPromo(800); +$stock->setImage('https://www.example.com/products/image/canon-eos-5d.jpg'); +$stock->setUrl('https://www.example.com/products/camera/canon-eos-5d'); +$stock->setStock(true); + +$api_key = 'YOUR_RTG_GENERATED_API_KEY'; +$product_data = $stock->prepareStockInfo(); + +//Update stock each time you make some change +$stock->updateStock($api_key, $product_data); +``` From a3f60b763fd7f080ddf540f816cd445c4ce7ab37 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 29 Mar 2019 10:39:24 +0200 Subject: [PATCH 20/24] Changes for sdk --- composer.json | 6 +- lib/AbstractRetargetingSDK.php | 6 +- lib/Api/Client.php | 21 +++---- lib/Api/Customers.php | 2 +- lib/Api/StockManagement.php | 2 +- lib/Brand.php | 4 +- lib/Category.php | 26 ++++++++- lib/Checkout.php | 4 +- lib/Email.php | 33 ++++++++--- lib/Exceptions/DecryptException.php | 2 +- lib/Exceptions/RTGException.php | 2 +- lib/Helpers/AbstractHelper.php | 9 +-- lib/Helpers/BrandHelper.php | 2 +- lib/Helpers/CategoryHelper.php | 2 +- lib/Helpers/CodeHelper.php | 2 +- lib/Helpers/CustomersApiHelper.php | 4 +- lib/Helpers/DecryptionHelper.php | 6 +- lib/Helpers/EmailHelper.php | 41 +++++++++++++- lib/Helpers/EncryptionHelper.php | 31 +--------- lib/Helpers/Helper.php | 3 +- lib/Helpers/NonceHelper.php | 87 ----------------------------- lib/Helpers/ProductFeedHelper.php | 58 ++++--------------- lib/Helpers/TokenHelper.php | 2 +- lib/Helpers/UrlHelper.php | 2 +- lib/Helpers/VariationsHelper.php | 4 +- lib/Order.php | 23 +++++++- lib/Product.php | 10 ++-- lib/ProductFeed.php | 4 +- lib/Variation.php | 6 +- tests/Unit/ApiCustomersTest.php | 2 +- tests/Unit/BrandTest.php | 2 +- tests/Unit/CategoryTest.php | 2 +- tests/Unit/EmailTest.php | 2 +- tests/Unit/OrderTest.php | 2 +- tests/Unit/ProductTest.php | 2 +- tests/Unit/StockManagementTest.php | 2 +- 36 files changed, 184 insertions(+), 234 deletions(-) delete mode 100644 lib/Helpers/NonceHelper.php diff --git a/composer.json b/composer.json index 7e7aa01..adcf21e 100644 --- a/composer.json +++ b/composer.json @@ -9,13 +9,13 @@ } ], "require": { - "php": ">=7.2.0", + "php": ">=5.6.0", "ext-json": "*", "ext-openssl": "*" }, "autoload": { "psr-4": { - "Retargeting\\": "lib" + "RetargetingSDK\\": "lib" } }, "require-dev": { @@ -33,7 +33,7 @@ "sort-packages": true, "optimize-autoloader": true, "platform": { - "php": "7.2.0" + "php": "5.6.0" } } } diff --git a/lib/AbstractRetargetingSDK.php b/lib/AbstractRetargetingSDK.php index 634f230..5a6434a 100644 --- a/lib/AbstractRetargetingSDK.php +++ b/lib/AbstractRetargetingSDK.php @@ -5,9 +5,10 @@ * Date: 2019-02-19 * Time: 07:48 */ -namespace Retargeting; -use Retargeting\Helpers\UrlHelper; +namespace RetargetingSDK; + +use RetargetingSDK\Helpers\UrlHelper; /** * Class AbstractRetargetingSDK @@ -49,6 +50,7 @@ public function getProperFormattedString($text) */ public function formatIntFloatString($value) { + if(!is_numeric($value)) { return 0; diff --git a/lib/Api/Client.php b/lib/Api/Client.php index 628eda7..7cf80c2 100644 --- a/lib/Api/Client.php +++ b/lib/Api/Client.php @@ -5,7 +5,7 @@ * Date: 2019-03-14 * Time: 13:53 */ -namespace Retargeting\Api; +namespace RetargetingSDK\Api; class Client { @@ -43,13 +43,13 @@ class Client * Property: the API request path (/api/path) * @var array */ - private $api_path = array(); + private $api_path = []; /** * Property: the API request parameters * @var array */ - private $api_parameters = array(); + private $api_parameters = []; /** * Method: constructor method for Retargeting REST API Client class @@ -95,7 +95,7 @@ public function setApiVersion($api_version) */ public function setResponseFormat($response_format = "json") { - if (in_array($response_format, array("json", "serial"))) { + if (in_array($response_format, ["json", "serial"])) { $this->response_format = $response_format; } else { $this->_throwException("responseFormat"); @@ -153,13 +153,14 @@ private function _processRequest() $api_uri = $this->api_uri."/".$this->api_version."/".implode("/", $this->api_path).".".$this->response_format; - $this->api_path = array(); - $api_parameters = array( + $this->api_path = []; + + $api_parameters = [ "api_key" => $this->api_key - ); + ]; $api_parameters = http_build_query(array_merge($api_parameters, $this->api_parameters)); - $this->api_parameters = array(); + $this->api_parameters = []; $curl_request = curl_init(); curl_setopt($curl_request, CURLOPT_URL, $api_uri); @@ -187,14 +188,14 @@ private function _processRequest() */ private function _throwException($message) { - $messages = array( + $messages = [ "checkApiKey" => "You need an API KEY to use Retargeting API. Please go to your Retargeting Administration Panel to set up or check your API KEY.", "apiUriType" => "The API uri must be string", "apiVersionType" => "The API version must be a string", "responseFormat" => "The response format can only be json or serial (php serialize)", "decodingMode" => "Decoding must be boolean", "emptyApiPath" => "You API request is empty" - ); + ]; throw new \Exception($messages[$message]); } diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php index 61f1ef7..6673ea5 100644 --- a/lib/Api/Customers.php +++ b/lib/Api/Customers.php @@ -6,7 +6,7 @@ * Time: 12:24 */ -namespace Retargeting\Api; +namespace RetargetingSDK\Api; use Retargeting\AbstractRetargetingSDK; use Retargeting\Helpers\CustomersApiHelper; diff --git a/lib/Api/StockManagement.php b/lib/Api/StockManagement.php index 1041a08..a9d1b8b 100644 --- a/lib/Api/StockManagement.php +++ b/lib/Api/StockManagement.php @@ -6,7 +6,7 @@ * Time: 7:39 PM */ -namespace Retargeting\Api; +namespace RetargetingSDK\Api; use Retargeting\AbstractRetargetingSDK; use Retargeting\Exceptions\RTGException; diff --git a/lib/Brand.php b/lib/Brand.php index 3d8118e..95428d4 100644 --- a/lib/Brand.php +++ b/lib/Brand.php @@ -6,9 +6,9 @@ * Time: 08:03 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\BrandHelper; +use RetargetingSDK\Helpers\BrandHelper; class Brand extends AbstractRetargetingSDK { diff --git a/lib/Category.php b/lib/Category.php index e75f1b6..ae3ee55 100644 --- a/lib/Category.php +++ b/lib/Category.php @@ -6,9 +6,10 @@ * Time: 08:02 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\CategoryHelper; +use RetargetingSDK\Helpers\CategoryHelper; +use RetargetingSDK\Helpers\UrlHelper; /** * Class Category @@ -16,8 +17,9 @@ */ class Category extends AbstractRetargetingSDK { - protected $id; + protected $id = '-1'; protected $name = ''; + protected $url = ''; protected $parent = false; protected $breadcrumb = []; @@ -57,6 +59,23 @@ public function setName($name) $this->name = $name; } + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @param $url + * @throws \Exception + */ + public function setUrl($url) + { + $this->url = UrlHelper::validate($url); + } + /** * @return int */ @@ -102,6 +121,7 @@ public function prepareCategoryData() return $this->toJSON([ 'id' => $this->getId(), 'name' => $this->getName(), + 'url' => $this->getUrl(), 'parent' => $this->getParent(), 'breadcrumb' => $this->getBreadcrumb() ]); diff --git a/lib/Checkout.php b/lib/Checkout.php index 4bb6ac8..b59e30c 100644 --- a/lib/Checkout.php +++ b/lib/Checkout.php @@ -6,7 +6,7 @@ * Time: 08:04 */ -namespace Retargeting; +namespace RetargetingSDK; class Checkout extends AbstractRetargetingSDK { @@ -15,7 +15,7 @@ class Checkout extends AbstractRetargetingSDK /** * @return array */ - public function getProductIds(): array + public function getProductIds() { return $this->productIds; } diff --git a/lib/Email.php b/lib/Email.php index 5d61744..a16bfa6 100644 --- a/lib/Email.php +++ b/lib/Email.php @@ -6,9 +6,9 @@ * Time: 08:03 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\EmailHelper; +use RetargetingSDK\Helpers\EmailHelper; class Email extends AbstractRetargetingSDK { @@ -17,6 +17,7 @@ class Email extends AbstractRetargetingSDK protected $phone = ''; protected $city = ''; protected $sex = ''; + protected $birthday = ''; /** * @return mixed @@ -109,6 +110,23 @@ public function setSex($sex) $this->sex = $sex; } + /** + * @return string + */ + public function getBirthday() + { + return $this->birthday; + } + + /** + * @param $birthday + * @throws \Exception + */ + public function setBirthday($birthday) + { + $this->birthday = EmailHelper::validateBirthday($birthday); + } + /** * Prepare email data * @return string @@ -116,11 +134,12 @@ public function setSex($sex) public function prepareEmailData() { return $this->toJSON([ - 'email' => $this->getEmail(), - 'name' => $this->getName(), - 'phone' => $this->getPhone(), - 'city' => $this->getCity(), - 'sex' => $this->getSex() + 'email' => $this->getEmail(), + 'name' => $this->getName(), + 'phone' => $this->getPhone(), + 'city' => $this->getCity(), + 'sex' => $this->getSex(), + 'birthday' => $this->getBirthday() ]); } } \ No newline at end of file diff --git a/lib/Exceptions/DecryptException.php b/lib/Exceptions/DecryptException.php index 3e5afd1..f9801e1 100644 --- a/lib/Exceptions/DecryptException.php +++ b/lib/Exceptions/DecryptException.php @@ -6,7 +6,7 @@ * Time: 12:32 */ -namespace Retargeting\Exceptions; +namespace RetargetingSDK\Exceptions; /** * Class DecryptException diff --git a/lib/Exceptions/RTGException.php b/lib/Exceptions/RTGException.php index 7a7c272..01ca57d 100644 --- a/lib/Exceptions/RTGException.php +++ b/lib/Exceptions/RTGException.php @@ -6,7 +6,7 @@ * Time: 13:04 */ -namespace Retargeting\Exceptions; +namespace RetargetingSDK\Exceptions; /** * Class RTGException diff --git a/lib/Helpers/AbstractHelper.php b/lib/Helpers/AbstractHelper.php index 3c49c04..c43c9c5 100644 --- a/lib/Helpers/AbstractHelper.php +++ b/lib/Helpers/AbstractHelper.php @@ -6,7 +6,7 @@ * Time: 11:17 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; class AbstractHelper { @@ -86,15 +86,16 @@ public static function filterArrayByKey($array, $keyname) */ public static function _throwException($message) { - $messages = array( + $messages = [ "emptyURL" => "Url is required. Please don't leave it empty.", "wrongUrl" => "The url has wrong format.", "emptyCustomerData" => "Customer data is required. Please don't leave it empty.", "emptyToken" => "Token is required. Please don't leave it empty.", "wrongFormatToken" => "Token format is wrong.", "wrongFormat" => "The array format you provided is wrong.", - "invalidEmail" => "Invalid email format." - ); + "invalidEmail" => "Invalid email format.", + "wrongPrice" => "Wrong price format." + ]; throw new \Exception($messages[$message]); } diff --git a/lib/Helpers/BrandHelper.php b/lib/Helpers/BrandHelper.php index 452bf84..31a2d67 100644 --- a/lib/Helpers/BrandHelper.php +++ b/lib/Helpers/BrandHelper.php @@ -6,7 +6,7 @@ * Time: 11:57 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class BrandHelper extends AbstractHelper implements Helper { diff --git a/lib/Helpers/CategoryHelper.php b/lib/Helpers/CategoryHelper.php index 75cc249..6f1da33 100644 --- a/lib/Helpers/CategoryHelper.php +++ b/lib/Helpers/CategoryHelper.php @@ -6,7 +6,7 @@ * Time: 12:09 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class CategoryHelper extends AbstractHelper implements Helper { diff --git a/lib/Helpers/CodeHelper.php b/lib/Helpers/CodeHelper.php index cceb22f..5fd4c6d 100644 --- a/lib/Helpers/CodeHelper.php +++ b/lib/Helpers/CodeHelper.php @@ -6,7 +6,7 @@ * Time: 16:03 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class CodeHelper extends AbstractHelper implements Helper { diff --git a/lib/Helpers/CustomersApiHelper.php b/lib/Helpers/CustomersApiHelper.php index 1fc95d0..9b65598 100644 --- a/lib/Helpers/CustomersApiHelper.php +++ b/lib/Helpers/CustomersApiHelper.php @@ -6,9 +6,7 @@ * Time: 11:02 */ -namespace Retargeting\Helpers; - -use Retargeting\ApiCustomersTest; +namespace RetargetingSDK\Helpers; final class CustomersApiHelper extends AbstractHelper implements Helper { diff --git a/lib/Helpers/DecryptionHelper.php b/lib/Helpers/DecryptionHelper.php index 0123dc3..a11b6c8 100644 --- a/lib/Helpers/DecryptionHelper.php +++ b/lib/Helpers/DecryptionHelper.php @@ -6,10 +6,10 @@ * Time: 12:25 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; -use Retargeting\Exceptions\DecryptException; -use Retargeting\Exceptions\RTGException; +use RetargetingSDK\Exceptions\DecryptException; +use RetargetingSDK\Exceptions\RTGException; /** * Class Decryption diff --git a/lib/Helpers/EmailHelper.php b/lib/Helpers/EmailHelper.php index 37ce207..eb7a8ac 100644 --- a/lib/Helpers/EmailHelper.php +++ b/lib/Helpers/EmailHelper.php @@ -6,7 +6,7 @@ * Time: 14:43 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class EmailHelper extends AbstractHelper implements Helper { @@ -27,4 +27,43 @@ public static function validate($email) return $email; } + + /** + * Validate user birthday + * @param $birthday + * @return \DateTime|string + * @throws \Exception + */ + public static function validateBirthday($birthday) + { + $dob = ''; + + if(self::isDate($birthday)) + { + $dob = new \DateTime($birthday); + + return $dob->format('d-m-Y'); + } + + return $dob; + } + + /** + * Check if the value is a valid date + * @param mixed $value + * @return boolean + */ + public static function isDate($value) + { + if (!$value) { + return false; + } else { + $date = date_parse($value); + if($date['error_count'] == 0 && $date['warning_count'] == 0) { + return checkdate($date['month'], $date['day'], $date['year']); + } else { + return false; + } + } + } } \ No newline at end of file diff --git a/lib/Helpers/EncryptionHelper.php b/lib/Helpers/EncryptionHelper.php index c685da0..53f966e 100644 --- a/lib/Helpers/EncryptionHelper.php +++ b/lib/Helpers/EncryptionHelper.php @@ -6,8 +6,9 @@ * Time: 12:25 */ -namespace Retargeting\Helpers; -use Retargeting\Exceptions\RTGException; +namespace RetargetingSDK\Helpers; + +use RetargetingSDK\Exceptions\RTGException; /** * Class Encryption @@ -51,30 +52,4 @@ private static function createKey() { return hash(self::HASH_ALGORITHM, self::$token); } - - /** - * @param int $length - * @param null $time - * @return string - */ - private function generateNonce($length = 12, $time = null) - { - $time = ($time === null) ? time() : $time; - $nonce = gmstrftime('%Y-%m-%dT%H:%M:%SZ', $time); - - if ($length < 1) { - return $nonce; - } - - $length = (int)$length; - $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; - - $unique = ''; - for ($i = 0; $i < $length; $i++) { - $unique .= substr($chars, (rand() % (strlen($chars))), 1); - } - - return rtrim(strtr(base64_encode($nonce . $unique), '+/', '-_'), '='); - } - } diff --git a/lib/Helpers/Helper.php b/lib/Helpers/Helper.php index 384bfe2..af11b0d 100644 --- a/lib/Helpers/Helper.php +++ b/lib/Helpers/Helper.php @@ -6,8 +6,7 @@ * Time: 11:44 */ -namespace Retargeting\Helpers; - +namespace RetargetingSDK\Helpers; interface Helper { diff --git a/lib/Helpers/NonceHelper.php b/lib/Helpers/NonceHelper.php deleted file mode 100644 index a7b8bdc..0000000 --- a/lib/Helpers/NonceHelper.php +++ /dev/null @@ -1,87 +0,0 @@ - 255) { - return false; - } - - $result = preg_match('/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/', - $nonce, - $matches); - if ($result != 1 || count($matches) != 8) { - return false; - } - - $stamp = gmmktime($matches[4], - $matches[5], - $matches[6], - $matches[2], - $matches[3], - $matches[1]); - - $time = time(); - if ($stamp < ($time - $clockSkew) - || $stamp > ($time + $clockSkew)) { - - return false; - } - - return true; - } - -} diff --git a/lib/Helpers/ProductFeedHelper.php b/lib/Helpers/ProductFeedHelper.php index 32ed5d4..237fba4 100644 --- a/lib/Helpers/ProductFeedHelper.php +++ b/lib/Helpers/ProductFeedHelper.php @@ -6,7 +6,7 @@ * Time: 17:56 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class ProductFeedHelper extends AbstractHelper implements Helper { @@ -17,58 +17,22 @@ final class ProductFeedHelper extends AbstractHelper implements Helper */ public static function validate($feed) { - $feed = json_encode($feed); - - $result = json_decode($feed); - - switch (json_last_error()) { - case JSON_ERROR_NONE: - $error = ''; - break; - case JSON_ERROR_DEPTH: - $error = 'The maximum stack depth has been exceeded.'; - break; - case JSON_ERROR_STATE_MISMATCH: - $error = 'Invalid or malformed JSON.'; - break; - case JSON_ERROR_CTRL_CHAR: - $error = 'Control character error, possibly incorrectly encoded.'; - break; - case JSON_ERROR_SYNTAX: - $error = 'Syntax error, malformed JSON.'; - break; - case JSON_ERROR_UTF8: - $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; - break; - case JSON_ERROR_RECURSION: - $error = 'One or more recursive references in the value to be encoded.'; - break; - case JSON_ERROR_INF_OR_NAN: - $error = 'One or more NAN or INF values in the value to be encoded.'; - break; - case JSON_ERROR_UNSUPPORTED_TYPE: - $error = 'A value of a type that cannot be encoded was given.'; - break; - default: - $error = 'Unknown JSON error occured.'; - break; - } - - if ($error !== '') { - return []; - } - - return $result; + return $feed; } /** * Formats price into format, e.g. 1000.99. - * - * @param int|float|string $price the price string to format. - * @return string|null the formatted price. + * @param $price + * @return float + * @throws \Exception */ public static function formatPrice($price) { - return is_numeric($price) ? number_format($price, 2, '.', '') : null; + if(!is_numeric($price)) + { + self::_throwException('wrongPrice'); + } + + return floatval(round($price, 2)); } } \ No newline at end of file diff --git a/lib/Helpers/TokenHelper.php b/lib/Helpers/TokenHelper.php index 8457a9e..0bba36a 100644 --- a/lib/Helpers/TokenHelper.php +++ b/lib/Helpers/TokenHelper.php @@ -6,7 +6,7 @@ * Time: 13:40 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; /** * Class Token diff --git a/lib/Helpers/UrlHelper.php b/lib/Helpers/UrlHelper.php index 2b7bf26..b57370c 100644 --- a/lib/Helpers/UrlHelper.php +++ b/lib/Helpers/UrlHelper.php @@ -6,7 +6,7 @@ * Time: 10:50 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class UrlHelper extends AbstractHelper implements Helper { diff --git a/lib/Helpers/VariationsHelper.php b/lib/Helpers/VariationsHelper.php index d69fa63..847be92 100644 --- a/lib/Helpers/VariationsHelper.php +++ b/lib/Helpers/VariationsHelper.php @@ -6,7 +6,7 @@ * Time: 12:46 */ -namespace Retargeting\Helpers; +namespace RetargetingSDK\Helpers; final class VariationsHelper extends AbstractHelper implements Helper { @@ -30,7 +30,7 @@ public static function validate($variation) } else { - $variationArr['variations'] = false; + $variationArr['vavriations'] = false; } if(array_key_exists('stock', $variation) && isset($variation['stock'])) diff --git a/lib/Order.php b/lib/Order.php index 2339b76..760d22f 100644 --- a/lib/Order.php +++ b/lib/Order.php @@ -6,9 +6,9 @@ * Time: 08:03 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\EmailHelper; +use RetargetingSDK\Helpers\EmailHelper; class Order extends AbstractRetargetingSDK { @@ -20,6 +20,7 @@ class Order extends AbstractRetargetingSDK protected $state = ''; protected $city = ''; protected $address = ''; + protected $birthday = ''; protected $discount = ''; protected $discountCode = '0'; protected $shipping = ''; @@ -169,6 +170,23 @@ public function setAddress($address) $this->address = $address; } + /** + * @return string + */ + public function getBirthday() + { + return $this->birthday; + } + + /** + * @param $birthday + * @throws \Exception + */ + public function setBirthday($birthday) + { + $this->birthday = EmailHelper::validateBirthday($birthday); + } + /** * @return mixed */ @@ -256,6 +274,7 @@ public function prepareOrderInformation() 'state' => $this->getState(), 'city' => $this->getCity(), 'address' => $this->getAddress(), + 'birthday' => $this->getBirthday(), 'discount' => $this->getDiscount(), 'discount_code' => $this->getDiscountCode(), 'shipping' => $this->getShipping(), diff --git a/lib/Product.php b/lib/Product.php index 210191d..a5a9f4f 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -5,12 +5,12 @@ * Date: 2019-02-19 * Time: 07:48 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\BrandHelper; -use Retargeting\Helpers\CategoryHelper; -use Retargeting\Helpers\UrlHelper; -use Retargeting\Helpers\VariationsHelper; +use RetargetingSDK\Helpers\BrandHelper; +use RetargetingSDK\Helpers\CategoryHelper; +use RetargetingSDK\Helpers\UrlHelper; +use RetargetingSDK\Helpers\VariationsHelper; class Product extends AbstractRetargetingSDK { diff --git a/lib/ProductFeed.php b/lib/ProductFeed.php index 47b4ae4..d86b8c6 100644 --- a/lib/ProductFeed.php +++ b/lib/ProductFeed.php @@ -6,9 +6,9 @@ * Time: 08:04 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\ProductFeedHelper; +use RetargetingSDK\Helpers\ProductFeedHelper; class ProductFeed extends AbstractRetargetingSDK { diff --git a/lib/Variation.php b/lib/Variation.php index 72d8801..565b451 100644 --- a/lib/Variation.php +++ b/lib/Variation.php @@ -6,9 +6,9 @@ * Time: 08:03 */ -namespace Retargeting; +namespace RetargetingSDK; -use Retargeting\Helpers\CodeHelper; +use RetargetingSDK\Helpers\CodeHelper; class Variation extends AbstractRetargetingSDK { @@ -51,7 +51,7 @@ public function setStock(int $stock) /** * @return array */ - public function getDetails(): array + public function getDetails() { return $this->details; } diff --git a/tests/Unit/ApiCustomersTest.php b/tests/Unit/ApiCustomersTest.php index 26ffcf4..af1e690 100644 --- a/tests/Unit/ApiCustomersTest.php +++ b/tests/Unit/ApiCustomersTest.php @@ -6,7 +6,7 @@ * Time: 10:25 */ -namespace Retargeting; +namespace RetargetingSDK; use PHPUnit\Framework\TestCase; use Retargeting\Api\Customers; diff --git a/tests/Unit/BrandTest.php b/tests/Unit/BrandTest.php index 7a98503..6b0d463 100644 --- a/tests/Unit/BrandTest.php +++ b/tests/Unit/BrandTest.php @@ -9,7 +9,7 @@ namespace Tests\Unit; use PHPUnit\Framework\TestCase; -use Retargeting\Brand; +use RetargetingSDK\Brand; /** * Class BrandTest diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php index de12ef7..4f352dc 100644 --- a/tests/Unit/CategoryTest.php +++ b/tests/Unit/CategoryTest.php @@ -9,7 +9,7 @@ namespace Tests\Unit; use PHPUnit\Framework\TestCase; -use Retargeting\Category; +use RetargetingSDK\Category; /** * @property Category category diff --git a/tests/Unit/EmailTest.php b/tests/Unit/EmailTest.php index f3b0bea..9ff014a 100644 --- a/tests/Unit/EmailTest.php +++ b/tests/Unit/EmailTest.php @@ -9,7 +9,7 @@ namespace Tests\Unit; use PHPUnit\Framework\TestCase; -use Retargeting\Email; +use RetargetingSDK\Email; /** * @property Email email diff --git a/tests/Unit/OrderTest.php b/tests/Unit/OrderTest.php index 8e187e3..41b7044 100644 --- a/tests/Unit/OrderTest.php +++ b/tests/Unit/OrderTest.php @@ -9,7 +9,7 @@ namespace Tests\Unit; use PHPUnit\Framework\TestCase; -use Retargeting\Order; +use RetargetingSDK\Order; /** * @property Order order diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index b02af81..7a3d65f 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -6,7 +6,7 @@ * Time: 10:25 */ -namespace Retargeting; +namespace RetargetingSDK; use PHPUnit\Framework\TestCase; diff --git a/tests/Unit/StockManagementTest.php b/tests/Unit/StockManagementTest.php index 8039f84..5b5d320 100644 --- a/tests/Unit/StockManagementTest.php +++ b/tests/Unit/StockManagementTest.php @@ -9,7 +9,7 @@ namespace Tests\Unit; use PHPUnit\Framework\TestCase; -use Retargeting\Api\StockManagement; +use RetargetingSDK\Api\StockManagement; /** * Class StockManagementTest From cbd52382394b5c17d4b881408dfe84331ccbf042 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 29 Mar 2019 11:17:50 +0200 Subject: [PATCH 21/24] Changes for sdk --- lib/Api/Customers.php | 4 ++-- lib/Api/StockManagement.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php index 6673ea5..97f8098 100644 --- a/lib/Api/Customers.php +++ b/lib/Api/Customers.php @@ -8,8 +8,8 @@ namespace RetargetingSDK\Api; -use Retargeting\AbstractRetargetingSDK; -use Retargeting\Helpers\CustomersApiHelper; +use RetargetingSDK\AbstractRetargetingSDK; +use RetargetingSDK\Helpers\CustomersApiHelper; /** * Class Customers diff --git a/lib/Api/StockManagement.php b/lib/Api/StockManagement.php index a9d1b8b..082a192 100644 --- a/lib/Api/StockManagement.php +++ b/lib/Api/StockManagement.php @@ -8,8 +8,8 @@ namespace RetargetingSDK\Api; -use Retargeting\AbstractRetargetingSDK; -use Retargeting\Exceptions\RTGException; +use RetargetingSDK\AbstractRetargetingSDK; +use RetargetingSDK\Exceptions\RTGException; class StockManagement extends AbstractRetargetingSDK { From a2eb636e43ab0973683e0726630c3419eacb4a92 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 29 Mar 2019 11:19:05 +0200 Subject: [PATCH 22/24] Changes for sdk --- tests/Unit/ApiCustomersTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Unit/ApiCustomersTest.php b/tests/Unit/ApiCustomersTest.php index af1e690..4a4f504 100644 --- a/tests/Unit/ApiCustomersTest.php +++ b/tests/Unit/ApiCustomersTest.php @@ -9,9 +9,9 @@ namespace RetargetingSDK; use PHPUnit\Framework\TestCase; -use Retargeting\Api\Customers; -use Retargeting\Helpers\DecryptionHelper; -use Retargeting\Helpers\EncryptionHelper; +use RetargetingSDK\Api\Customers; +use RetargetingSDK\Helpers\DecryptionHelper; +use RetargetingSDK\Helpers\EncryptionHelper; /** * Class ApiCustomersTest From dd21ac1ae04c96c66c287b212a4653ea700f8ca8 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 29 Mar 2019 11:40:02 +0200 Subject: [PATCH 23/24] Changes for sdk --- tests/Unit/CategoryTest.php | 19 ++++++++++- tests/Unit/EmailTest.php | 65 +++++++++++++++---------------------- tests/Unit/OrderTest.php | 23 +++++++++++-- tests/Unit/ProductTest.php | 1 + 4 files changed, 65 insertions(+), 43 deletions(-) diff --git a/tests/Unit/CategoryTest.php b/tests/Unit/CategoryTest.php index 4f352dc..ae0b871 100644 --- a/tests/Unit/CategoryTest.php +++ b/tests/Unit/CategoryTest.php @@ -23,7 +23,7 @@ public function setUp(): void $this->category->setId(89); $this->category->setName('Shoes'); $this->category->setParent(''); - + $this->category->setUrl('https://www.example.com/desktops/monitors'); $this->category->setBreadcrumb([ ["id" => 21, "name" => "Sneakers", "parent" => 20], ["id" => 20, "name" => "Shoes", "parent" => false] @@ -46,6 +46,22 @@ public function testIfCategoryHasName() $this->assertNotNull($this->category->getName()); } + /** + * Test if url is not empty + */ + public function testIfUrlIsNotEmpty() + { + $this->assertNotNull($this->category->getUrl()); + } + + /** + * Test if url has correct format + */ + public function testIfUrlHasProperFormat() + { + $this->assertEquals($this->category->getUrl(), 'https://www.example.com/desktops/monitors'); + } + /** * Check if category has parent or not. If not return false. */ @@ -109,6 +125,7 @@ public function testIfCategoryPrepareDataHasProperFormat() $this->assertEquals($this->category->prepareCategoryData(), json_encode([ 'id' => '89', 'name' => 'Shoes', + 'url' => 'https://www.example.com/desktops/monitors', 'parent' => false, 'breadcrumb' => [ ["id" => '21', "name" => "Sneakers", "parent" => 20], diff --git a/tests/Unit/EmailTest.php b/tests/Unit/EmailTest.php index 9ff014a..a1c74c7 100644 --- a/tests/Unit/EmailTest.php +++ b/tests/Unit/EmailTest.php @@ -19,6 +19,13 @@ class EmailTest extends TestCase public function setUp(): void { $this->email = new Email(); + + $this->email->setEmail('john.doe@mail.com'); + $this->email->setName('John Doe'); + $this->email->setPhone('(298) 407-4029'); + $this->email->setCity('Berlin'); + $this->email->setSex(0); + $this->email->setBirthday('20-02-1960'); } /** @@ -26,8 +33,6 @@ public function setUp(): void */ public function test_if_email_is_not_empty() { - $this->email->setEmail('john.doe@mail.com'); - $this->assertNotNull($this->email->getEmail()); } @@ -36,8 +41,6 @@ public function test_if_email_is_not_empty() */ public function test_if_email_has_proper_format() { - $this->email->setEmail('john.doe@mail.com'); - $this->assertRegExp('/^.+\@\S+\.\S+$/', $this->email->getEmail()); } @@ -46,8 +49,6 @@ public function test_if_email_has_proper_format() */ public function test_if_name_is_not_empty() { - $this->email->setName('John Doe'); - $this->assertNotNull($this->email->getName()); } @@ -56,8 +57,6 @@ public function test_if_name_is_not_empty() */ public function test_if_name_is_string() { - $this->email->setName('John Doe'); - $this->assertIsString($this->email->getName()); } @@ -66,8 +65,6 @@ public function test_if_name_is_string() */ public function test_if_phone_is_not_empty() { - $this->email->setPhone('(298) 407-4029'); - $this->assertNotNull($this->email->getPhone()); } @@ -76,28 +73,14 @@ public function test_if_phone_is_not_empty() */ public function test_if_phone_is_string() { - $this->email->setPhone('(298) 407-4029'); - $this->assertIsString($this->email->getPhone()); } - /** - * Test if phone has proper format - */ - public function test_if_phone_has_proper_format() - { - $this->email->setPhone(' (298) 407-4029 '); - - $this->assertEquals($this->email->getPhone(), '(298) 407-4029'); - } - /** * Test if city number is not empty */ public function test_if_city_is_not_empty() { - $this->email->setCity('Berlin'); - $this->assertNotNull($this->email->getCity()); } @@ -106,8 +89,6 @@ public function test_if_city_is_not_empty() */ public function test_if_city_is_string() { - $this->email->setCity('Berlin'); - $this->assertIsString($this->email->getCity()); } @@ -116,8 +97,6 @@ public function test_if_city_is_string() */ public function test_if_city_has_proper_format() { - $this->email->setCity('Berlin '); - $this->assertEquals($this->email->getCity(), 'Berlin'); } @@ -126,8 +105,6 @@ public function test_if_city_has_proper_format() */ public function test_if_sex_is_not_empty() { - $this->email->setSex(0); - $this->assertNotNull($this->email->getSex()); } @@ -136,30 +113,40 @@ public function test_if_sex_is_not_empty() */ public function test_if_sex_is_boolean() { - $this->email->setSex(1); - $this->assertIsNumeric($this->email->getSex()); } + /** + * Test if birthday is not empty + * @throws \Exception + */ + public function test_if_birthday_is_not_empty() + { + $this->assertNotNull($this->email->getBirthday()); + } + + /** + * Test if birthday has correct format + */ + public function test_if_birthday_has_proper_format() + { + $this->assertEquals($this->email->getBirthday(), '20-02-1960'); + } + /** * Test if prepare email data return proper formatted json * @throws \Exception */ public function test_if_prepare_email_data_has_proper_format() { - $this->email->setEmail('john.doe@mail.com'); - $this->email->setName('John Doe'); - $this->email->setPhone('(298) 407-4029'); - $this->email->setCity('Berlin'); - $this->email->setSex(0); - $this->assertEquals($this->email->prepareEmailData(), json_encode([ 'email' => 'john.doe@mail.com', 'name' => 'John Doe', 'phone' => '(298) 407-4029', 'city' => 'Berlin', - 'sex' => 0 + 'sex' => 0, + 'birthday' => '20-02-1960' ], JSON_PRETTY_PRINT) ); } diff --git a/tests/Unit/OrderTest.php b/tests/Unit/OrderTest.php index 41b7044..b2ebc02 100644 --- a/tests/Unit/OrderTest.php +++ b/tests/Unit/OrderTest.php @@ -16,9 +16,6 @@ */ class OrderTest extends TestCase { - /** - * - */ public function setUp(): void { $this->order = new Order(); @@ -30,6 +27,7 @@ public function setUp(): void $this->order->setState('Germany'); $this->order->setCity('Berlin'); $this->order->setAddress('Sample address'); + $this->order->setBirthday('01-01-1990'); $this->order->setDiscount(20); $this->order->setDiscountCode('RAX204'); $this->order->setShipping('Sample shipping street'); @@ -108,6 +106,24 @@ public function test_if_order_has_address() $this->assertNotNull($this->order->getAddress()); } + /** + * Test if birthday is not empty + * @throws \Exception + */ + public function test_if_birthday_is_not_empty() + { + $this->assertNotNull($this->order->getBirthday()); + } + + /** + * Test if birthday has correct format + */ + public function test_if_birthday_has_proper_format() + { + $this->assertEquals($this->order->getBirthday(), '01-01-1990'); + } + + /** * Test if order has discount */ @@ -154,6 +170,7 @@ public function test_if_order_prepare_information_has_correct_json_format() 'state' => 'Germany', 'city' => 'Berlin', 'address' => 'Sample address', + 'birthday' => '01-01-1990', 'discount' => "20", 'discount_code' => 'RAX204', 'shipping' => 'Sample shipping street', diff --git a/tests/Unit/ProductTest.php b/tests/Unit/ProductTest.php index 7a3d65f..27163a0 100644 --- a/tests/Unit/ProductTest.php +++ b/tests/Unit/ProductTest.php @@ -9,6 +9,7 @@ namespace RetargetingSDK; use PHPUnit\Framework\TestCase; +use RetargetingSDK\Product; /** * @property Product product From c272a43101ae6134469f8cd2e76a03e4319c95e6 Mon Sep 17 00:00:00 2001 From: andreicotaga Date: Fri, 29 Mar 2019 17:22:40 +0200 Subject: [PATCH 24/24] Changes for sdk --- lib/Api/Customers.php | 2 +- lib/Product.php | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/Api/Customers.php b/lib/Api/Customers.php index 97f8098..5171106 100644 --- a/lib/Api/Customers.php +++ b/lib/Api/Customers.php @@ -20,7 +20,7 @@ class Customers extends AbstractRetargetingSDK protected $token; protected $data = []; protected $currentPage = 1; - protected $lastPage = 20; + protected $lastPage = ''; protected $nextPage = ''; protected $prevPage = ''; diff --git a/lib/Product.php b/lib/Product.php index a5a9f4f..ad9e34f 100644 --- a/lib/Product.php +++ b/lib/Product.php @@ -9,6 +9,7 @@ use RetargetingSDK\Helpers\BrandHelper; use RetargetingSDK\Helpers\CategoryHelper; +use RetargetingSDK\Helpers\ProductFeedHelper; use RetargetingSDK\Helpers\UrlHelper; use RetargetingSDK\Helpers\VariationsHelper; @@ -108,11 +109,12 @@ public function getPrice() } /** - * @param mixed $price + * @param $price + * @throws \Exception */ public function setPrice($price) { - $price = $this->formatIntFloatString($price); + $price = ProductFeedHelper::formatPrice($price); $this->price = $price; } @@ -126,13 +128,14 @@ public function getPromo() } /** - * @param float $promo + * @param $promo + * @throws \Exception */ public function setPromo($promo) { if($promo > 0 && $promo < $this->getPrice()) { - $promo = $this->formatIntFloatString($promo); + $promo = ProductFeedHelper::formatPrice($promo); } else {