Skip to content

Commit

Permalink
Normalize DateTime/Date/Time and add tests for it as in PR #18
Browse files Browse the repository at this point in the history
  • Loading branch information
abbadon1334 committed Apr 8, 2020
1 parent 1539bd1 commit ce932c1
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 69 deletions.
73 changes: 50 additions & 23 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace atk4\api;

use atk4\data\Field;
use atk4\data\Model;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Response\JsonResponse;
Expand Down Expand Up @@ -41,6 +42,12 @@ class Api
/** @var int Response options */
protected $response_options = JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;

/**
* @var bool If set to true, the first array element of Model->export
* will be returned (GET single record)
* If not, the array will be returned as-is
*/
public $single_record = true;

/**
* Reads everything off globals.
Expand Down Expand Up @@ -149,14 +156,45 @@ public function exec($callable, $vars = [])
// if callable function returns agile data model, then export it
// this is important for REST API implementation
if ($ret instanceof Model) {
$ret = $this->exportModel($ret);

$data = [];

$allowed_fields = $this->getAllowedFields($ret, 'read');
if ($this->single_record) {
/** @var Field $field */
foreach($ret->getFields() as $fieldName => $field) {
if(!in_array($fieldName, $allowed_fields)) {
continue;
}
$data[$field->actual ?? $fieldName] = $field->toString();
}
} else {
foreach ($ret as $m) {
/** @var Model $m */
$record = [];
/** @var Field $field */
foreach ($ret->getFields() as $fieldName => $field) {
if(!in_array($fieldName, $allowed_fields)) {
continue;
}
$record[$field->actual ?? $fieldName] = $field->toString();
}
$data[] = $record;
}
}

$ret = $data;
}

// no response, just step out
if ($ret === null) {
return;
}

if ($ret === true) { // manage delete
$ret = [];
}

// emit successful response
$this->successResponse($ret);
}
Expand Down Expand Up @@ -194,7 +232,7 @@ protected function call($callable, $vars = [])
*/
protected function exportModel(Model $m)
{
return $m->export($this->getAllowedFields($m, 'read'), null, false);
return $m->export($this->getAllowedFields($m, 'read'), null, true);
}

/**
Expand Down Expand Up @@ -242,7 +280,7 @@ protected function loadModelByValue(Model $m, $value)
protected function getAllowedFields(Model $m, $action = 'read')
{
// take model only_fields into account
$fields = is_array($m->only_fields) ? $m->only_fields : [];
$fields = is_array($m->only_fields) && !empty($m->only_fields) ? $m->only_fields : array_keys($m->getFields());

// limit by apiFields
if (isset($m->apiFields, $m->apiFields[$action])) {
Expand Down Expand Up @@ -297,7 +335,7 @@ protected function successResponse($response)
// for testing purposes there can be situations when emitter is disabled. then do nothing.
if ($this->emitter) {
$this->emitter->emit($this->response);
exit;
exit; // @todo find a solution to remove this exit.
}

// @todo Should we also stop script execution if no emitter is defined or just ignore that?
Expand Down Expand Up @@ -395,6 +433,7 @@ public function rest($pattern, $model = null, $methods = ['read', 'modify', 'del
// GET all records
if (in_array('read', $methods)) {
$f = function (...$params) use ($model) {
$this->single_record = false;
if (is_callable($model)) {
$model = $this->call($model, $params);
}
Expand All @@ -413,24 +452,11 @@ public function rest($pattern, $model = null, $methods = ['read', 'modify', 'del
$model = $this->call($model, $params);
}

$this->loadModelByValue($model, $id);

//calculate only once
$allowed_fields = $this->getAllowedFields($model, 'read');
$data = [];
// get all field-elements
foreach ($model->elements as $field => $f) {
//only use allowed fields
if(!in_array($field, $allowed_fields)) {
continue;
}

if ($f instanceof \atk4\data\Field) {
$data[$field] = $f->toString();
}
}
// limit fields
$model->onlyFields($this->getAllowedFields($model, 'read'));

return $data;
// load model and get field values
return $this->loadModelByValue($model, $id);
};

$this->get($pattern.'/:id', $f);
Expand All @@ -452,7 +478,7 @@ public function rest($pattern, $model = null, $methods = ['read', 'modify', 'del
$this->loadModelByValue($model, $id)->save($this->request_data);
$model->onlyFields($this->getAllowedFields($model, 'read'));

return $model->get();
return $model;
};
$this->patch($pattern.'/:id', $f);
$this->post($pattern.'/:id', $f);
Expand All @@ -472,7 +498,8 @@ public function rest($pattern, $model = null, $methods = ['read', 'modify', 'del
$model->onlyFields($this->getAllowedFields($model, 'read'));

$this->response_code = 201; // http code for created
return $model->get();

return $model;
};
$this->post($pattern, $f);
}
Expand Down
Loading

0 comments on commit ce932c1

Please sign in to comment.