Skip to content

Commit

Permalink
adding types & removing doc types
Browse files Browse the repository at this point in the history
  • Loading branch information
jturbide committed Feb 21, 2024
1 parent 985bdb7 commit b17f2b8
Showing 1 changed file with 27 additions and 64 deletions.
91 changes: 27 additions & 64 deletions src/Mvc/Controller/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
namespace Zemit\Mvc\Controller;

use League\Csv\ByteSequence;
use League\Csv\CannotInsertRecord;
use League\Csv\CharsetConverter;
use League\Csv\InvalidArgument;
use League\Csv\Writer;
use Phalcon\Dispatcher\Exception;
use Phalcon\Events\Manager;
use Phalcon\Exception;
use Phalcon\Http\Response;
use Phalcon\Http\ResponseInterface;
use Phalcon\Mvc\Dispatcher;
Expand All @@ -32,30 +34,26 @@ class Rest extends \Zemit\Mvc\Controller
use Rest\Fractal;

/**
* @param string|int|null $id
* @return void
* @throws \Phalcon\Dispatcher\Exception
* @throws Exception
*/
public function indexAction(string|int $id = null)
public function indexAction(string|int $id = null): void
{
$this->restForwarding($id);
}

/**
* @param string|int|null $id
* @return void
* @throws \Phalcon\Dispatcher\Exception
* @throws Exception
*/
protected function restForwarding(string|int $id = null): void
{
$id ??= $this->getParam('id');
if ($this->request->isPost() || $this->request->isPut() || $this->request->isPatch()) {
$this->dispatcher->forward(['action' => 'save']);
}
elseif ($this->request->isDelete()) {
else if ($this->request->isDelete()) {
$this->dispatcher->forward(['action' => 'delete']);
}
elseif ($this->request->isGet()) {
else if ($this->request->isGet()) {
if (is_null($id)) {
$this->dispatcher->forward(['action' => 'getList']);
}
Expand All @@ -66,25 +64,17 @@ protected function restForwarding(string|int $id = null): void
}

/**
* Retrieving a single record
* Alias of method getAction()
*
* @param string|int|null $id
* @return bool|ResponseInterface
* @deprecated Should use getAction() method instead
*/
public function getSingleAction(string|int $id = null)
public function getSingleAction(string|int $id = null): false|ResponseInterface
{
return $this->getAction($id);
}

/**
* Retrieving a single record
*
* @param string|int|null $id
* @return bool|ResponseInterface
*/
public function getAction(string|int $id = null)
public function getAction(string|int $id = null): false|ResponseInterface
{
$modelName = $this->getModelClassName();
$single = $this->getSingle($id, $modelName, null);
Expand All @@ -104,25 +94,18 @@ public function getAction(string|int $id = null)
}

/**
* Retrieving a record list
* Alias of method getListAction()
*
* @return ResponseInterface
* @throws \Exception
* @deprecated Should use getListAction() method instead
*/
public function getAllAction()
public function getAllAction(): ResponseInterface
{
return $this->getListAction();
}

/**
* Retrieving a record list
*
* @return ResponseInterface
* @throws \Exception
*/
public function getListAction()
public function getListAction(): ResponseInterface
{
$model = $this->getModelClassName();

Expand Down Expand Up @@ -152,11 +135,9 @@ public function getListAction()

/**
* Exporting a record list into a CSV stream
*
* @return ResponseInterface|null
* @throws \Exception
*/
public function exportAction()
public function exportAction(): ?ResponseInterface
{
$model = $this->getModelClassName();
$find = $this->getFind();
Expand All @@ -169,19 +150,12 @@ public function exportAction()

/**
* Download a JSON / CSV / XLSX
*
* @TODO optimize this using stream and avoid storage large dataset into variables
*
* @param $list
* @param $fileName
* @param $contentType
* @param $params
* @return ResponseInterface|void
* @throws \League\Csv\CannotInsertRecord
* @throws \League\Csv\InvalidArgument
* @throws \Zemit\Exception
* @throws CannotInsertRecord
* @throws InvalidArgument
* @throws \League\Csv\Exception
*/
public function download($list = [], $fileName = null, $contentType = null, $params = null)
public function download(array $list = [], string $fileName = null, string $contentType = null, array $params = null): ?ResponseInterface
{
$params ??= $this->getParams();
$contentType ??= $this->getContentType();
Expand Down Expand Up @@ -300,7 +274,7 @@ public function download($list = [], $fileName = null, $contentType = null, $par
// XLSX
if ($contentType === 'xlsx') {
$xlsxArray = [];
$xlsxArray []= $listColumns;
$xlsxArray [] = $listColumns;

foreach ($list as $row) {
$outputRow = [];
Expand Down Expand Up @@ -497,10 +471,8 @@ public function arrayCustomOrder(array $arrayToOrder, array $orderList): array
/**
* Count a record list
* @TODO add total count / deleted count / active count
*
* @return ResponseInterface
*/
public function countAction()
public function countAction(): ResponseInterface
{
$model = $this->getModelClassName();

Expand All @@ -519,10 +491,8 @@ public function countAction()

/**
* Prepare a new model for the frontend
*
* @return ResponseInterface
*/
public function newAction()
public function newAction(): ResponseInterface
{
$model = $this->getModelClassName();

Expand All @@ -534,7 +504,7 @@ public function newAction()
$this->view->source = $entity->getSource();
$this->view->single = $this->expose($entity);

return $this->setRestResponse();
return $this->setRestResponse(true);
}

/**
Expand Down Expand Up @@ -611,7 +581,7 @@ public function validateAction($id = null)
/**
* Saving a record (create & update)
*/
public function saveAction(?int $id = null): ?ResponseInterface
public function saveAction(?int $id = null): ResponseInterface
{
$ret = $this->save($id);
$this->view->setVars($ret);
Expand Down Expand Up @@ -659,7 +629,7 @@ public function saveResultHasKey(array $array, string $key): bool
/**
* Deleting a record
*/
public function deleteAction(?int $id = null): ?ResponseInterface
public function deleteAction(string|int $id = null): false|ResponseInterface
{
$entity = $this->getSingle($id);

Expand All @@ -671,19 +641,16 @@ public function deleteAction(?int $id = null): ?ResponseInterface

if (!$entity) {
$this->response->setStatusCode(404, 'Not Found');
return false;
}

return $this->setRestResponse($ret['deleted']);
}

/**
* Restoring record
*
* @param null $id
*
* @return bool|ResponseInterface
*/
public function restoreAction($id = null)
public function restoreAction(string|int $id = null): false|ResponseInterface
{
$entity = $this->getSingle($id);

Expand All @@ -703,13 +670,9 @@ public function restoreAction($id = null)

/**
* Re-ordering a position
*
* @param null $id
* @param null $position
*
* @return bool|ResponseInterface
* @throws \Exception
*/
public function reorderAction($id = null, $position = null)
public function reorderAction(string|int $id = null, int $position = null): false|ResponseInterface
{
$entity = $this->getSingle($id);
$position = $this->getParam('position', 'int', $position);
Expand Down

0 comments on commit b17f2b8

Please sign in to comment.