Skip to content

Commit

Permalink
REST API: Extended with pre, post and getItem Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dergel committed Oct 23, 2023
1 parent f0f885b commit 4162d18
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions plugins/rest/lib/rest/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ class rex_yform_rest_route

/** @var rex_yform_manager_table */
public $table;

/** @var rex_yform_manager_query */
public $query;
public $instance;

private $includes;

private $preFunc;
private $postFunc;
private $getItemFunc;

public static $requestMethods = ['get', 'post', 'delete'];

private $additionalHeaders = [];
Expand All @@ -27,6 +33,9 @@ public function __construct(array $config)
$this->type = $config['type'];
$this->table = $this->config['table'];
$this->query = $this->config['query'];
$this->preFunc = $this->config['preFunc'] ?? null;
$this->postFunc = $this->config['postFunc'] ?? null;
$this->getItemFunc = $this->config['getItemFunc'] ?? null;
$this->instance = $this->table->createDataset();
$this->path = ('/' == mb_substr($this->config['path'], -1)) ? mb_substr($this->config['path'], 0, -1) : $this->config['path'];
}
Expand Down Expand Up @@ -87,10 +96,12 @@ public function handleRequest(array $paths, array $get)
}

/** @var rex_yform_manager_table $table */
$table = $this->config['table'];
$table = $this->getTable();
$query = $this->getQuery();

/** @var rex_yform_manager_query $query */
$query = $this->config['query'];
if (is_callable($this->preFunc)) {
call_user_func($this->preFunc, $this);
}

switch ($requestMethod) {
case 'get':
Expand Down Expand Up @@ -123,7 +134,6 @@ public function handleRequest(array $paths, array $get)
$currentPage = ($currentPage < 0) ? 1 : $currentPage;

$query->limit(($currentPage - 1) * $per_page, $per_page);

if (isset($get['order']) && is_array($get['order'])) {
foreach ($get['order'] as $orderName => $orderValue) {
if (array_key_exists($orderName, $fields)) {
Expand Down Expand Up @@ -176,7 +186,7 @@ public function handleRequest(array $paths, array $get)
$id = $path;
$id_column = 'id';
if ('' != $query->getTableAlias()) {
$id_column = $query->getTableAlias() . '.id';
$id_column = $query->getTableAlias().'.id';
}

$query
Expand Down Expand Up @@ -213,10 +223,9 @@ public function handleRequest(array $paths, array $get)
foreach ($instances as $instance) {
$data[] = $this->getInstanceData(
$instance,
array_merge($paths, [$instance->getId()]),
array_merge($paths, [$instance->getId()])
);
}

if ($baseInstances) {
$links = [];
$meta = [];
Expand Down Expand Up @@ -244,18 +253,18 @@ public function handleRequest(array $paths, array $get)
$links['self'] = rex_yform_rest::getLinkByPath($this, $linkParams);
$links['first'] = rex_yform_rest::getLinkByPath($this, array_merge(
$linkParams,
['page' => 1],
['page' => 1]
));
if (($currentPage - 1) > 0) {
$links['prev'] = rex_yform_rest::getLinkByPath($this, array_merge(
$linkParams,
['page' => ($currentPage - 1)],
['page' => ($currentPage - 1)]
));
}
if (($currentPage * $per_page) < $itemsAll) {
$links['next'] = rex_yform_rest::getLinkByPath($this, array_merge(
$linkParams,
['page' => ($currentPage + 1)],
['page' => ($currentPage + 1)]
));
}

Expand All @@ -271,11 +280,18 @@ public function handleRequest(array $paths, array $get)
} else {
$data = $this->getInstanceData(
$instance,
array_merge($paths),
array_merge($paths)
);
if (is_callable($this->getItemFunc)) {
$data = call_user_func($this->getItemFunc, $this, $data);
}
}
}

if (is_callable($this->postFunc)) {
$data = call_user_func($this->postFunc, $this, $data);
}

$this->sendContent('200', $data);

break;
Expand Down Expand Up @@ -552,7 +568,7 @@ private function filterFieldsByInclude(array $fields, array $parents = []): arra

$newFields = [];
foreach ($fields as $key => $field) {
$compareKey = 0 == count($parents) ? $key : implode('.', $parents) . '.' . $key;
$compareKey = 0 == count($parents) ? $key : implode('.', $parents).'.'.$key;
if (in_array($compareKey, $this->getIncludes(), true)) {
$newFields[$key] = $field;
}
Expand Down Expand Up @@ -590,7 +606,7 @@ public function getInstanceRelationships(rex_yform_manager_dataset $instance, $p
$relationInstance,
array_merge($paths, [$field->getName(), $relationInstance->getId()]),
$onlyId,
$fieldParents,
$fieldParents
);
}
$return[$field->getName()] = [
Expand All @@ -617,7 +633,6 @@ public function getInstanceRelationships(rex_yform_manager_dataset $instance, $p

/**
* @param false $attributCall
* @return mixed
*/
public function getInstanceValue($instance, $key, $attributCall = false)
{
Expand All @@ -632,9 +647,6 @@ public function getRequestMethod(): string
return strtolower($_SERVER['REQUEST_METHOD']);
}

/**
* @param mixed $instance
*/
public function getTypeFromInstance($instance = null): string
{
if (!$instance) {
Expand All @@ -647,4 +659,26 @@ public function getTypeFromInstance($instance = null): string
}
return $type;
}

public function setQuery(rex_yform_manager_query $query): self
{
$this->query = $query;
return $this;
}

public function getQuery(): rex_yform_manager_query
{
return $this->query;
}

public function setTable(rex_yform_manager_table $table): self
{
$this->table = $table;
return $this;
}

public function getTable(): rex_yform_manager_table
{
return $this->table;
}
}

0 comments on commit 4162d18

Please sign in to comment.