Skip to content

Fix syntax error and improve constructor type declarations #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 88 additions & 87 deletions src/Sellix.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,102 @@

define("BASE_DIR", dirname(__FILE__));

require_once BASE_DIR."/Blacklists/index.php";
require_once BASE_DIR."/Whitelists/index.php";
require_once BASE_DIR."/Categories/index.php";
require_once BASE_DIR."/Coupons/index.php";
require_once BASE_DIR."/Feedback/index.php";
require_once BASE_DIR."/Orders/index.php";
require_once BASE_DIR."/Groups/index.php";
require_once BASE_DIR."/Products/index.php";
require_once BASE_DIR."/Queries/index.php";
require_once BASE_DIR."/Payments/index.php";
require_once BASE_DIR."/Customers/index.php";
require_once BASE_DIR."/Subscriptions/index.php";
require_once BASE_DIR."/Exceptions/index.php";;
require_once BASE_DIR."/Tests/index.php";
require_once BASE_DIR . "/Blacklists/index.php";
require_once BASE_DIR . "/Whitelists/index.php";
require_once BASE_DIR . "/Categories/index.php";
require_once BASE_DIR . "/Coupons/index.php";
require_once BASE_DIR . "/Feedback/index.php";
require_once BASE_DIR . "/Orders/index.php";
require_once BASE_DIR . "/Groups/index.php";
require_once BASE_DIR . "/Products/index.php";
require_once BASE_DIR . "/Queries/index.php";
require_once BASE_DIR . "/Payments/index.php";
require_once BASE_DIR . "/Customers/index.php";
require_once BASE_DIR . "/Subscriptions/index.php";
require_once BASE_DIR . "/Exceptions/index.php";
require_once BASE_DIR . "/Tests/index.php";

class Sellix {
protected string $api_key;

use Blacklists;
use Whitelists;
use Categories;
use Groups;
use Coupons;
use Feedback;
use Orders;
use Products;
use Queries;
use Payments;
use Customers;
use Subscriptions;

static $API_ENDPOINT = 'https://dev.sellix.io/v1';

public function __construct(string $api_key, string $merchant = NULL) {
$this->api_key = $api_key;
$this->merchant = $merchant;
}

private function handle_response($response, $key = NULL) {
if ($response->status != 200) {
throw new SellixException($response->error, $response->status);
} else {
if ($key === NULL) {
return NULL;
}

if (is_array($key) && array_key_exists("oneOf", $key)) {
$updated_response = new \stdClass();
foreach ($key["oneOf"] as $option) {
if (strpos($option, ",") !== false) {
foreach (explode(",", $option) as $key_option) {
if (property_exists($response->data, $key_option)) {
$updated_response->$key_option = $response->data->$key_option;
}
protected string $api_key;
protected ?string $merchant;

use Blacklists;
use Whitelists;
use Categories;
use Groups;
use Coupons;
use Feedback;
use Orders;
use Products;
use Queries;
use Payments;
use Customers;
use Subscriptions;

static $API_ENDPOINT = 'https://dev.sellix.io/v1';

public function __construct(string $api_key, ?string $merchant = NULL) {
$this->api_key = $api_key;
$this->merchant = $merchant;
}

private function handle_response($response, $key = NULL) {
if ($response->status != 200) {
throw new SellixException($response->error, $response->status);
} else {
if ($key === NULL) {
return NULL;
}
} else if (property_exists($response->data, $option)) {
return $response->data->$option;
}
}

return $updated_response;
}
if (is_array($key) && array_key_exists("oneOf", $key)) {
$updated_response = new \stdClass();
foreach ($key["oneOf"] as $option) {
if (strpos($option, ",") !== false) {
foreach (explode(",", $option) as $key_option) {
if (property_exists($response->data, $key_option)) {
$updated_response->$key_option = $response->data->$key_option;
}
}
} else if (property_exists($response->data, $option)) {
return $response->data->$option;
}
}

return $updated_response;
}

return $response->data->$key;
}
}

private function request($component, $action = "GET", $payload = NULL) {
$curl = curl_init();

$headers = [
"Content-type: application/json",
"Authorization: Bearer " . $this->api_key
];

if ($this->merchant) {
$headers[] = "X-Sellix-Merchant: ".$this->merchant;
return $response->data->$key;
}
}

$curl_array = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $action,
CURLOPT_URL => self::$API_ENDPOINT.$component,
CURLOPT_HTTPHEADER => $headers
];
private function request($component, $action = "GET", $payload = NULL) {
$curl = curl_init();

if ($payload) {
$curl_array[CURLOPT_POSTFIELDS] = json_encode($payload);
}
$headers = [
"Content-type: application/json",
"Authorization: Bearer " . $this->api_key
];

curl_setopt_array($curl, $curl_array);
if ($this->merchant) {
$headers[] = "X-Sellix-Merchant: " . $this->merchant;
}

$curl_array = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $action,
CURLOPT_URL => self::$API_ENDPOINT . $component,
CURLOPT_HTTPHEADER => $headers
];

if ($payload) {
$curl_array[CURLOPT_POSTFIELDS] = json_encode($payload);
}

curl_setopt_array($curl, $curl_array);

$response = curl_exec($curl);
curl_close($curl);
$response = curl_exec($curl);
curl_close($curl);

return json_decode($response);
}
}
return json_decode($response);
}
}