2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/8 0008
|
|
|
|
|
* Time: 17:18
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-12-14 17:35:35 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
namespace HttpServer\Http\Formatter;
|
|
|
|
|
|
2021-02-20 17:33:28 +08:00
|
|
|
use HttpServer\Abstracts\HttpService;
|
2020-08-31 01:27:08 +08:00
|
|
|
use HttpServer\IInterface\IFormatter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class JsonFormatter
|
2020-08-31 12:38:32 +08:00
|
|
|
* @package Snowflake\Snowflake\Http\Formatter
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-02-20 17:33:28 +08:00
|
|
|
class JsonFormatter extends HttpService implements IFormatter
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-07-17 02:16:49 +08:00
|
|
|
public mixed $data;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-11-27 14:19:07 +08:00
|
|
|
public int $status = 200;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-11-27 14:19:07 +08:00
|
|
|
public array $header = [];
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
2020-12-14 17:35:35 +08:00
|
|
|
* @param $context
|
|
|
|
|
* @return JsonFormatter
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-14 17:35:35 +08:00
|
|
|
public function send($context): static
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2020-12-14 17:35:35 +08:00
|
|
|
if (!is_string($context)) {
|
|
|
|
|
$context = json_encode($context);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
2020-12-14 17:35:35 +08:00
|
|
|
$this->data = $context;
|
2020-08-31 01:27:08 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-14 17:35:35 +08:00
|
|
|
public function getData(): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$data = $this->data;
|
|
|
|
|
$this->clear();
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-14 17:35:35 +08:00
|
|
|
public function clear(): void
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2020-09-16 10:37:47 +08:00
|
|
|
$this->data = null;
|
2020-08-31 01:27:08 +08:00
|
|
|
unset($this->data);
|
|
|
|
|
}
|
|
|
|
|
}
|