81 lines
2.7 KiB
PHP
81 lines
2.7 KiB
PHP
<?php
|
|
namespace Incoviba\API\Common\Controller;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Incoviba\API\Common\Factory\Model as Factory;
|
|
use Incoviba\API\Common\Define\Controller\Json;
|
|
use Incoviba\Common\Banco;
|
|
|
|
class Bancos {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$bancos = $factory->find(Banco::class)->array();
|
|
$url = '' . $request->getUri();
|
|
array_walk($bancos, function (&$item) use ($url) {
|
|
$item['link'] = [
|
|
'rel' => 'banco',
|
|
'title' => $item['nombre'],
|
|
'href' => str_replace('/bancos', "/banco/{$item['id']}", $url)
|
|
];
|
|
});
|
|
return $this->withJson($response, compact('bancos'));
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $banco_id): Response {
|
|
$banco = $factory->find(Banco::class)->one($banco_id);
|
|
$output = [
|
|
'input' => $banco_id,
|
|
'banco' => $banco->toArray(),
|
|
'link' => [
|
|
'rel' => 'bancos',
|
|
'title' => 'Bancos',
|
|
'href' => str_replace("/banco/{$banco_id}", '/bancos', $request->getUri())
|
|
]
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$post = $request->getParsedBody();
|
|
$output = [
|
|
'input' => $post
|
|
];
|
|
if (in_array('bancos', $post)) {
|
|
$output['bancos'] = [];
|
|
foreach ($post['bancos'] as $input) {
|
|
$banco = Banco::add($factory, $input);
|
|
$banco []= [
|
|
'banco' => $banco->toArray(),
|
|
'created' => $banco->is_new() ? $banco->save() : false
|
|
];
|
|
}
|
|
} elseif (in_array('banco', $post)) {
|
|
$banco = Banco::add($factory, $post);
|
|
$output['banco'] = $banco;
|
|
$output['created'] = $banco->is_new() ? $banco->save() : false;
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $banco_id): Response {
|
|
$post = $request->getParsedBody();
|
|
$input = compact('banco_id', 'post');
|
|
$banco = $factory->find(Banco::class)->one($banco_id);
|
|
$output = [
|
|
'input' => $input,
|
|
'banco' => $banco->toArray()
|
|
];
|
|
$output['edited'] = $banco->edit($post);
|
|
$output['changes'] = $banco->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $banco_id): Response {
|
|
$banco = $factory->find(Banco::class)->one($banco_id);
|
|
$output = [
|
|
'input' => $banco_id,
|
|
'banco' => $banco->toArray()
|
|
];
|
|
$output['deleted'] = $banco->delete();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|