2323
2424namespace bhexpress \api_client ;
2525
26+ use Psr \Http \Message \ResponseInterface ;
27+
2628/**
2729 * Cliente de la API de BHExpress
2830 * @author Esteban De La Fuente Rubio, DeLaF (esteban[at]sasco.cl)
@@ -111,7 +113,7 @@ public function __construct(
111113 * @param string $url URL base.
112114 * @return $this
113115 */
114- public function setUrl (string $ url )
116+ public function setUrl (string $ url ): static
115117 {
116118 $ this ->api_url = $ url ;
117119 return $ this ;
@@ -123,7 +125,7 @@ public function setUrl(string $url)
123125 * @param string $token Token de autenticación.
124126 * @return $this
125127 */
126- public function setToken (string $ token )
128+ public function setToken (string $ token ): static
127129 {
128130 $ this ->api_token = $ token ;
129131 return $ this ;
@@ -135,7 +137,7 @@ public function setToken(string $token)
135137 * @param string $rut RUT del emisor.
136138 * @return $this
137139 */
138- public function setRut (string $ rut ) # NUEVO MÉTODO
140+ public function setRut (string $ rut ): static # NUEVO MÉTODO
139141 {
140142 $ this ->rut_emisor = $ rut ;
141143 return $ this ;
@@ -146,7 +148,7 @@ public function setRut(string $rut) # NUEVO MÉTODO
146148 *
147149 * @return string|null
148150 */
149- public function getLastUrl ()
151+ public function getLastUrl (): string | null
150152 {
151153 return $ this ->last_url ;
152154 }
@@ -156,7 +158,7 @@ public function getLastUrl()
156158 *
157159 * @return \Psr\Http\Message\ResponseInterface|null
158160 */
159- public function getLastResponse ()
161+ public function getLastResponse (): ResponseInterface | null
160162 {
161163 return $ this ->last_response ;
162164 }
@@ -170,11 +172,11 @@ public function getLastResponse()
170172 * @return string El cuerpo de la respuesta HTTP.
171173 * @throws ApiException Si no hay respuesta previa o el cuerpo no se puede obtener.
172174 */
173- public function getBody ()
175+ public function getBody (): string
174176 {
175177 if (!$ this ->last_response ) {
176178 throw new ApiException (
177- 'No hay una respuesta HTTP previa para obtener el cuerpo. '
179+ message: 'No hay una respuesta HTTP previa para obtener el cuerpo. '
178180 );
179181 }
180182
@@ -191,13 +193,16 @@ public function getBody()
191193 * @return array El cuerpo de la respuesta HTTP decodificado como un arreglo.
192194 * @throws ApiException Si no hay respuesta previa o el cuerpo no se puede decodificar.
193195 */
194- public function getBodyDecoded ()
196+ public function getBodyDecoded (): mixed
195197 {
196- $ decodedBody = json_decode ($ this ->getBody (), true );
198+ $ decodedBody = json_decode (
199+ json: $ this ->getBody (),
200+ associative: true
201+ );
197202
198203 if (json_last_error () !== JSON_ERROR_NONE ) {
199204 throw new ApiException (
200- sprintf (
205+ message: sprintf (
201206 'Error al decodificar JSON: %s ' ,
202207 json_last_error_msg ()
203208 )
@@ -217,11 +222,11 @@ public function getBodyDecoded()
217222 * @return array Arreglo asociativo con la información de la respuesta.
218223 * @throws ApiException Si se encuentra un error en el proceso.
219224 */
220- public function toArray ()
225+ public function toArray (): array
221226 {
222227 if (!$ this ->last_response ) {
223228 throw new ApiException (
224- 'No hay una respuesta HTTP previa para procesar. '
229+ message: 'No hay una respuesta HTTP previa para procesar. '
225230 );
226231 }
227232
@@ -283,13 +288,13 @@ public function get(
283288 string $ resource ,
284289 array $ headers = [],
285290 array $ options = []
286- ) {
291+ ): ResponseInterface | null {
287292 return $ this ->consume (
288- $ resource ,
289- [],
290- $ headers ,
291- 'GET ' ,
292- $ options
293+ resource: $ resource ,
294+ data: [],
295+ headers: $ headers ,
296+ method: 'GET ' ,
297+ options: $ options
293298 )->getLastResponse ();
294299 }
295300
@@ -307,13 +312,13 @@ public function post(
307312 array $ data ,
308313 array $ headers = [],
309314 array $ options = []
310- ) {
315+ ): ResponseInterface | null {
311316 return $ this ->consume (
312- $ resource ,
313- $ data ,
314- $ headers ,
315- 'POST ' ,
316- $ options
317+ resource: $ resource ,
318+ data: $ data ,
319+ headers: $ headers ,
320+ method: 'POST ' ,
321+ options: $ options
317322 )->getLastResponse ();
318323 }
319324
@@ -331,13 +336,13 @@ public function put(
331336 array $ data ,
332337 array $ headers = [],
333338 array $ options = []
334- ) {
339+ ): ResponseInterface | null {
335340 return $ this ->consume (
336- $ resource ,
337- $ data ,
338- $ headers ,
339- 'PUT ' ,
340- $ options
341+ resource: $ resource ,
342+ data: $ data ,
343+ headers: $ headers ,
344+ method: 'PUT ' ,
345+ options: $ options
341346 )->getLastResponse ();
342347 }
343348
@@ -353,13 +358,13 @@ public function delete(
353358 string $ resource ,
354359 array $ headers = [],
355360 array $ options = []
356- ) {
361+ ): ResponseInterface | null {
357362 return $ this ->consume (
358- $ resource ,
359- [],
360- $ headers ,
361- 'DELETE ' ,
362- $ options
363+ resource: $ resource ,
364+ data: [],
365+ headers: $ headers ,
366+ method: 'DELETE ' ,
367+ options: $ options
363368 )->getLastResponse ();
364369 }
365370
@@ -383,18 +388,18 @@ public function consume(
383388 array $ headers = [],
384389 string $ method = null ,
385390 array $ options = []
386- ) {
391+ ): static {
387392 $ this ->last_response = null ;
388393 if (!$ this ->api_token ) {
389394 throw new ApiException (
390- 'Falta especificar token para autenticación. ' ,
391- 400
395+ message: 'Falta especificar token para autenticación. ' ,
396+ code: 400
392397 );
393398 }
394399 if (!$ this ->rut_emisor ) {
395400 throw new ApiException (
396- 'Falta especificar RUT del emisor. ' ,
397- 400
401+ message: 'Falta especificar RUT del emisor. ' ,
402+ code: 400
398403 )
399404 ; # NUEVA CONDICIONAL
400405 }
@@ -418,9 +423,9 @@ public function consume(
418423 // realizar consulta HTTP
419424 try {
420425 $ this ->last_response = $ client ->request (
421- $ method ,
422- $ this ->last_url ,
423- $ options
426+ method: $ method ,
427+ uri: $ this ->last_url ,
428+ options: $ options
424429 );
425430 } catch (\GuzzleHttp \Exception \GuzzleException $ e ) {
426431 $ this ->last_response = $ e ->getResponse ();
@@ -441,7 +446,7 @@ public function consume(
441446 *
442447 * @return object Detalles del error con propiedades 'code' y 'message'.
443448 */
444- private function getError ()
449+ private function getError (): object
445450 {
446451 $ data = $ this ->getBodyDecoded ();
447452 $ response = $ this ->getLastResponse ();
@@ -481,10 +486,10 @@ private function getError()
481486 *
482487 * @throws ApiException Lanza una excepción con detalles del error.
483488 */
484- private function throwException ()
489+ private function throwException (): ApiException
485490 {
486491 $ error = $ this ->getError ();
487- throw new ApiException ($ error ->message , $ error ->code );
492+ throw new ApiException (message: $ error ->message , code: $ error ->code );
488493 }
489494
490495 /**
@@ -493,7 +498,7 @@ private function throwException()
493498 * @param string $name Nombre de la variable de entorno.
494499 * @return string|null Valor de la variable de entorno o null si no está definida.
495500 */
496- private function env (string $ name )
501+ private function env (string $ name ): mixed
497502 {
498503 return function_exists ('env ' ) ? env ($ name ) : getenv ($ name );
499504 }
0 commit comments