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); } }