Limpieza de objetos externos
This commit is contained in:
24
api/common/Controller/Base.php
Normal file
24
api/common/Controller/Base.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use function Safe\{json_decode, file_get_contents};
|
||||
use ProVM\Alias\Controller\Json;
|
||||
|
||||
class Base
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container): ResponseInterface
|
||||
{
|
||||
$folder = $container->get('folders')->documentation;
|
||||
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||
$folder,
|
||||
'base.json'
|
||||
]);
|
||||
$documentation = json_decode(file_get_contents($filename));
|
||||
return $this->withJson($response, $documentation);
|
||||
}
|
||||
}
|
125
api/common/Controller/Cuentas.php
Normal file
125
api/common/Controller/Cuentas.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
namespace Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use PDOException;
|
||||
use function Safe\{json_decode,error_log};
|
||||
use ProVM\Alias\Controller\Json;
|
||||
use Contabilidad\Repository\Cuenta;
|
||||
|
||||
class Cuentas
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
{
|
||||
$cuentas = [];
|
||||
try {
|
||||
$cuentas = $repository->fetchAll();
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
}
|
||||
return $this->withJson($response, compact('cuentas'));
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
|
||||
{
|
||||
$cuenta = null;
|
||||
try {
|
||||
$cuenta = $repository->fetchById($cuenta_id);
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
}
|
||||
return $this->withJson($response, compact('cuenta'));
|
||||
}
|
||||
public function add(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'cuentas' => []
|
||||
];
|
||||
foreach ($json as $data) {
|
||||
$cuenta = $repository->create((array) $data);
|
||||
$status = true;
|
||||
$exists = true;
|
||||
if ($cuenta->isNew()) {
|
||||
$exists = false;
|
||||
try {
|
||||
$repository->save($cuenta);
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
}
|
||||
$output['cuentas'] []= [
|
||||
'cuenta' => $cuenta,
|
||||
'exists' => $exists,
|
||||
'added' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function edit(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'cuentas' => []
|
||||
];
|
||||
foreach ($json as $data) {
|
||||
$cuenta = $repository->fetchById($data->id);
|
||||
$old = clone $cuenta;
|
||||
try {
|
||||
$cuenta->edit((array) $data);
|
||||
$status = $cuenta->isDirty();
|
||||
if ($status) {
|
||||
$repository->save($cuenta);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
$output['cuentas'] []= [
|
||||
'antes' => $old,
|
||||
'cuenta' => $cuenta,
|
||||
'edited' => $status
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function editOne(ServerRequestInterface $request, ResponseInterface $response, Cuenta $repository, $cuenta_id): ResponseInterface
|
||||
{
|
||||
$cuenta = $repository->fetchById($cuenta_id);
|
||||
$body = $request->getBody();
|
||||
$contents = $body->getContents();
|
||||
$json = json_decode($contents, JSON_OBJECT_AS_ARRAY);
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'old' => clone $cuenta
|
||||
];
|
||||
try {
|
||||
$cuenta->edit((array) $json);
|
||||
$status = $cuenta->isDirty();
|
||||
if ($status) {
|
||||
$repository->save($cuenta);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
error_log($e);
|
||||
$status = false;
|
||||
}
|
||||
$output['cuenta'] = $cuenta;
|
||||
$output['edited'] = $status;
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user