Guest User

Untitled

a guest
May 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <?php
  2. class atencion {
  3. public static function get($peticion){
  4. if ($peticion[0] == 'getAll') {
  5. return self::getAll();
  6. }
  7.  
  8. public static function getAll(){
  9.  
  10. $sql = "SELECT * FROM persona";
  11.  
  12. $sentencia = ConexionBD::obtenerInstancia()->obtenerBD()->prepare($sql);
  13.  
  14. try{
  15. if ($sentencia->execute()) {
  16. $resultado = $sentencia->fetchAll(PDO::FETCH_CLASS);
  17. return $resultado;
  18. }
  19. else {
  20. return null;
  21. }
  22. }catch(PDOException $e){
  23. throw new ExcepcionApi(self::ESTADO_FALLA_DESCONOCIDA, "Falla desconocida". $e->getMessage(), 400);
  24. }
  25. }
  26.  
  27. public static function post($peticion){
  28.  
  29. if ($peticion[0] == 'agregar') {
  30.  
  31. return self::agregar();
  32.  
  33. } else {
  34.  
  35. throw new ExcepcionApi(self::ESTADO_URL_INCORRECTA, "Url mal formada", 400);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. public static function agregar(){
  42.  
  43. $body = file_get_contents('php://input');
  44. $input = json_decode($body);
  45. $array = (array)$input;
  46.  
  47. $idPersona = $array['idPersona'];
  48.  
  49. $documento = $array['documento'];
  50.  
  51. $sql = "INSERT INTO persona (idPersona, documento) VALUES(:idPersona, :documento)";
  52.  
  53. try{
  54.  
  55. $pdo = ConexionBD::obtenerInstancia()->obtenerBD();
  56.  
  57. $stmt = $pdo->prepare($sql);
  58.  
  59. $stmt->bindParam(':idPersona', $idPersona);
  60.  
  61. $stmt->bindParam(':documento', $documento);
  62.  
  63. $stmt->execute();
  64.  
  65. $pdo = null;
  66.  
  67. echo '{"notice": {"text": "Persona Agregada"}';
  68.  
  69. } catch(PDOException $e){
  70.  
  71. echo '{"error": {"text": '.$e->getMessage(). '}';
  72.  
  73. }
  74.  
  75. }
  76. }
  77.  
  78. $array=array("idPersona"=>1, "documento"=>7);
  79. $array["strDocumento"]=(string)$array["documento"];
  80.  
  81. array(3) {
  82. ["idPersona"]=>
  83. int(1)
  84. ["documento"]=>
  85. int(7)
  86. ["idDocumento"]=>
  87. string(1) "7"
  88. }
  89.  
  90. $array=array(
  91. array("idPersona"=>1, "documento"=>7),
  92. array("idPersona"=>2, "documento"=>-9),
  93. );
  94.  
  95. foreach ($array as $k=>$row){
  96. $array[$k]["strDocumento"] = (string)$row["documento"];
  97. }
  98.  
  99. var_dump($array);
  100.  
  101. array(2) {
  102. [0]=>
  103. array(3) {
  104. ["idPersona"]=>
  105. int(1)
  106. ["documento"]=>
  107. int(7)
  108. ["strDocumento"]=>
  109. string(1) "7"
  110. }
  111. [1]=>
  112. array(3) {
  113. ["idPersona"]=>
  114. int(2)
  115. ["documento"]=>
  116. int(-9)
  117. ["strDocumento"]=>
  118. string(2) "-9"
  119. }
  120. }
Add Comment
Please, Sign In to add comment