DirektiveDesDons/Router/Response.php

48 lines
1013 B
PHP
Raw Permalink Normal View History

2022-12-08 14:43:05 +01:00
<?php namespace Router;
class Response
{
/**
* Send a response
* @param string $data
* @param int? $status
* @return void
2022-12-15 14:55:00 +01:00
* @author Johannes Kantz
2022-12-08 14:43:05 +01:00
*/
public function send(string $data, int $status = null)
{
if (isset($status)) {
http_response_code($status);
}
echo $data;
}
/**
* Send a json response
* @param array $data
* @param int? $status
* @return void
2022-12-15 14:55:00 +01:00
* @author Johannes Kantz
2022-12-08 14:43:05 +01:00
*/
public function json(array $data, int $status = null)
{
if (isset($status)) {
http_response_code($status);
}
header('Content-Type: application/json; charset=utf-8');
$data["status"] = http_response_code();
2022-12-08 14:43:05 +01:00
echo json_encode($data);
}
/**
* Set the status code
* @param int $status
* @return void
2022-12-15 14:55:00 +01:00
* @author Johannes Kantz
2022-12-08 14:43:05 +01:00
*/
public function status(int $status)
{
http_response_code($status);
}
}