find(Fuente::class)->array(); $output = [ 'fuentes' => $fuentes ]; return $this->withJson($response, $output); } public function show(Request $request, Response $response, Factory $factory, $fuente_id): Response { $fuente = $factory->find(Fuente::class)->one($fuente_id); $output = [ 'input' => $fuente_id, 'fuente' => $fuente?->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) { $fuente = Fuente::add($factory, $in); $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; } } else { $fuente = Fuente::add($factory, $input); $results []= ['fuente' => $fuente?->toArray(), 'agregado' => $fuente?->save()]; } $output = [ 'input' => $input, 'fuentes' => $results ]; return $this->withJson($response, $output); } public function edit(Request $request, Response $response, Factory $factory, $fuente_id): Response { $fuente = $factory->find(Fuente::class)->one($fuente_id); $output = [ 'input' => $fuente_id, 'old' => $fuente->toArray() ]; $input = json_decode($request->getBody()); $fuente->edit($input); $output['fuente'] = $fuente->toArray(); return $this->withJson($response, $output); } public function delete(Request $request, Response $response, Factory $factory, $fuente_id): Response { $fuente = $factory->find(Fuente::class)->one($fuente_id); $output = [ 'input' => $fuente_id, 'fuente' => $fuente->toArray(), 'eliminado' => $fuente->delete() ]; return $this->withJson($response, $output); } public function entradas(Request $request, Response $response, Factory $factory, $fuente_id): Response { $fuente = $factory->find(Fuente::class)->one($fuente_id); $entradas = null; if ($fuente !== null) { $entradas = $fuente->entradas(); if ($entradas !== null) { usort($entradas, function($a, $b) { $d = $a->fecha()->diffInDays($b->fecha(), false); if ($d === 0) { return strcmp($a->cuenta()->nombre, $b->cuenta()->nombre); } return $d; }); array_walk($entradas, function(&$item) { $item = $item->toArray(); }); } } $output = [ 'input' => $fuente_id, 'fuente' => $fuente?->toArray(), 'entradas' => $entradas ]; return $this->withJson($response, $output); } }