72 lines
2.6 KiB
PHP
72 lines
2.6 KiB
PHP
<?php
|
|
namespace Contabilidad\Common\Controller;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use ProVM\Common\Define\Controller\Json;
|
|
use ProVM\Common\Factory\Model as Factory;
|
|
use Contabilidad\Moneda;
|
|
|
|
class Monedas {
|
|
use Json;
|
|
|
|
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
|
$monedas = $factory->find(Moneda::class)->array();
|
|
if ($monedas) {
|
|
usort($monedas, function($a, $b) {
|
|
return strcmp($a['denominacion'], $b['denominacion']);
|
|
});
|
|
}
|
|
$output = [
|
|
'monedas' => $monedas
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function show(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
|
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
|
$output = [
|
|
'input' => $moneda_id,
|
|
'moneda' => $moneda?->toArray()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function add(Request $request, Response $response, Factory $factory): Response {
|
|
$input = json_decode($request->getBody());
|
|
$results = [];
|
|
if (is_array($input)) {
|
|
foreach ($input as $in) {
|
|
$moneda = Moneda::add($factory, $in);
|
|
$results []= ['moneda' => $moneda?->toArray(), 'agregado' => $moneda?->save()];
|
|
}
|
|
} else {
|
|
$moneda = Moneda::add($factory, $input);
|
|
$results []= ['moneda' => $moneda?->toArray(), 'agregado' => $moneda?->save()];
|
|
}
|
|
$output = [
|
|
'input' => $input,
|
|
'monedas' => $results
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function edit(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
|
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
|
$output = [
|
|
'input' => $moneda_id,
|
|
'old' => $moneda->toArray()
|
|
];
|
|
$input = json_decode($request->getBody());
|
|
$moneda->edit($input);
|
|
$output['moneda'] = $moneda->toArray();
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function delete(Request $request, Response $response, Factory $factory, $moneda_id): Response {
|
|
$moneda = $factory->find(Moneda::class)->one($moneda_id);
|
|
$output = [
|
|
'input' => $moneda_id,
|
|
'moneda' => $moneda->toArray(),
|
|
'eliminado' => $moneda->delete()
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|