find(TipoFuente::class)->array(); if ($tipos_fuentes) { usort($tipos_fuentes, function($a, $b) { return strcmp($a['descripcion'], $b['descripcion']); }); } $output = [ 'tipos_fuentes' => $tipos_fuentes ]; return $this->withJson($response, $output); } public function show(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); $output = [ 'input' => $tipo_fuente_id, 'tipo_fuente' => $tipo_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) { $tipo_fuente = TipoFuente::add($factory, $in); $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; } } else { $tipo_fuente = TipoFuente::add($factory, $input); $results []= ['tipo_fuente' => $tipo_fuente?->toArray(), 'agregado' => $tipo_fuente?->save()]; } $output = [ 'input' => $input, 'tipo_fuentes' => $results ]; return $this->withJson($response, $output); } public function edit(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); $output = [ 'input' => $tipo_fuente_id, 'old' => $tipo_fuente->toArray() ]; $input = json_decode($request->getBody()); $tipo_fuente->edit($input); $output['tipo_fuente'] = $tipo_fuente->toArray(); return $this->withJson($response, $output); } public function delete(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); $output = [ 'input' => $tipo_fuente_id, 'tipo_fuente' => $tipo_fuente->toArray(), 'eliminado' => $tipo_fuente->delete() ]; return $this->withJson($response, $output); } public function fuentes(Request $request, Response $response, Factory $factory, $tipo_fuente_id): Response { $tipo_fuente = $factory->find(TipoFuente::class)->one($tipo_fuente_id); $fuentes = null; if ($tipo_fuente !== null) { $fuentes = $tipo_fuente->fuentes(); if ($fuentes !== null) { array_walk($fuentes, function(&$item) { $item = $item->toArray(); }); } } $output = [ 'input' => $tipo_fuente_id, 'tipo_fuente' => $tipo_fuente?->toArray(), 'fuentes' => $fuentes ]; return $this->withJson($response, $output); } }