packthub_ebook_integration-1.0.0/src/Plugin/Products.php
src/Plugin/Products.php
<?php
namespace Drupal\packt\Plugin;
class Products {
private string $orderBy = 'title';
private array $filters = [];
private int $pageCount = 1;
private int $limit = 10;
private array $response;
private string $productID;
public function getResponse(): array {
return $this->response;
}
/**
* @param bool $pageActual
* If is set to true, the result will be of giving page example page 4. If false
* results are from page 1 to 4.
* @param bool $all
* True all data returned from api call will be given if a false only products list
* is given if not other metadata is given.
*
* @return array
*/
public function getProducts(bool $pageActual = true, bool $all = true): array {
if($pageActual) {
$results = $this->runCurl($this->pageCount)->getResponse();
if($all) {
return $results;
}
return !empty($results['products']) ? $results['products'] : [
'current_page' => $results['current_page'] ?? 0,
'from' => $results['from'] ?? 0,
'last_page' => $results['last_page'] ?? 0,
'per_page' => $results['per_page'] ?? 0,
'to' => $results['to'] ?? 0
];
}
$data = [];
for ($i = 1; $i <= $this->pageCount; $i++) {
$results = $this->runCurl($i)->getResponse();
if($all) {
$data[] = $results;
}else {
$data[] = !empty($results['products']) ? $results['products'] : [
'current_page' => $results['current_page'] ?? 0,
'from' => $results['from'] ?? 0,
'last_page' => $results['last_page'] ?? 0,
'per_page' => $results['per_page'] ?? 0,
'to' => $results['to'] ?? 0
];
}
}
return $data;
}
private function runCurl(int $page = 1): Products {
// Build up params string
$params = [];
$api = "https://api.packt.com/api/v2/products";
//Add product id if exist.
if(!empty($this->productID)) {
$api .= "/$this->productID";
}
$params[] = "token=".packt_api_token();
// Add page number
$params[] = "page=$page";
// Add limit
$params[] = "limit=$this->limit";
$params[] = "orderBy=$this->orderBy";
$params[] = "orderType=";
// Add filters
foreach ($this->filters as $filter) {
if(!empty($filter['name']) && !empty($filter['operator']) && !empty($filter['value'])) {
$v = urlencode($filter['value']);
$params[] = "filter[name]={$filter['name']}&filter[operator]={$filter['operator']}&filter[value]={$v}";
}
}
// Joining all params.
$paramsLine = implode('&', $params);
$url = trim("$api?$paramsLine", '?');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$d = curl_exec($curl);
$this->response = !empty($d) ? json_decode($d, true) : [];
curl_close($curl);
return $this;
}
/**
* @param string $key title,published_from, language, product_type.
*
* @return void
*/
public function setOrderBy(string $key): void {
$keys = [ 'product_type', 'title', 'published_from', 'language'];
if(in_array($key, $keys)) {
$this->orderBy = $key;
}
}
/**
* @param int $page number of page or pages.
*
* @return void
*/
public function setPage(int $page): void {
$this->pageCount = $page;
}
/**
* @param int $limit Returned records count.
*
* @return void
*/
public function setLimit(int $limit): void {
$this->limit = $limit;
}
public function setOrderType(string $value): void {
if(in_array(strtoupper($value), ['DESC', 'ASC'])) {
// $this->orderType = strtoupper($value);
}
}
/**
* @param string $name Accepted values for name: product_type, title, published_from, language.
* @param string $operator Accepted Values for operator: =, <, <=, >, >=, !=
* @param int|string|bool $value
*
* @return void
*/
public function setFilter(string $name, string $operator, int|string|bool $value): void {
$names = ['product_type', 'title', 'published_from', 'language'];
if(in_array(strtolower($name), $names)) {
$operators = ['=', '<', '<=', '>', '>=', '!=', 'IS NULL' ,'IS NOT NULL' ,'RANGE', 'ANY', 'ALL', 'NOT ANY', 'NOT ALL'];
if(in_array(strtoupper($operator), $operators)) {
$this->filters[] = [
'name'=> $name,
'operator'=> strtoupper($operator),
'value'=> $value
];
}
}
}
/**
* @param string $product Product ID or ISBN
*
* @return void
*/
public function setProductID(string $product): void {
$this->productID = $product;
}
}
