This commit is contained in:
2021-10-25 18:25:14 +08:00
parent 6605aae410
commit a9d106a1cf
48 changed files with 5090 additions and 0 deletions
@@ -0,0 +1,50 @@
<?php
namespace Http\Handler\Formatter;
use Exception;
use Http\Handler\Abstracts\HttpService;
use Swoole\Http\Response;
/**
*
*/
class FileFormatter extends HttpService implements IFormatter
{
public mixed $data;
/** @var Response */
public Response $status;
public array $header = [];
/**
* @param $context
* @return $this
* @throws Exception
*/
public function send($context): static
{
$this->data = $context;
return $this;
}
/**
* @return mixed
*/
public function getData(): mixed
{
$data = $this->data;
$this->clear();
return $data;
}
public function clear(): void
{
$this->data = null;
unset($this->data);
}
}
@@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 17:51
*/
declare(strict_types=1);
namespace Http\Handler\Formatter;
use Exception;
use Http\Handler\Abstracts\HttpService;
use Kiri\Core\Json;
use Swoole\Http\Response;
/**
* Class HtmlFormatter
* @package Kiri\Kiri\Http\Formatter
*/
class HtmlFormatter extends HttpService implements IFormatter
{
public mixed $data;
/** @var Response */
public Response $status;
public array $header = [];
/**
* @param $context
* @return $this
* @throws Exception
*/
public function send($context): static
{
if (!is_string($context)) {
$context = Json::encode($context);
}
$this->data = $context;
return $this;
}
/**
* @return mixed
*/
public function getData(): mixed
{
$data = $this->data;
$this->clear();
return $data;
}
public function clear(): void
{
$this->data = null;
unset($this->data);
}
}
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 17:29
*/
namespace Http\Handler\Formatter;
/**
* Interface IFormatter
* @package Kiri\Kiri\Http\Formatter
*/
interface IFormatter
{
/**
* @param $context
* @return static
*/
public function send($context): static;
/**
* @return mixed
*/
public function getData(): mixed;
public function clear(): void;
}
@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 17:18
*/
declare(strict_types=1);
namespace Http\Handler\Formatter;
use Http\Handler\Abstracts\HttpService;
/**
* Class JsonFormatter
* @package Kiri\Kiri\Http\Formatter
*/
class JsonFormatter extends HttpService implements IFormatter
{
public mixed $data;
public int $status = 200;
public array $header = [];
/**
* @param $context
* @return JsonFormatter
*/
public function send($context): static
{
if (!is_string($context)) {
$context = json_encode($context);
}
$this->data = $context;
return $this;
}
/**
* @return mixed
*/
public function getData(): mixed
{
$data = $this->data;
$this->clear();
return $data;
}
public function clear(): void
{
$this->data = null;
unset($this->data);
}
}
@@ -0,0 +1,89 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/8 0008
* Time: 17:29
*/
declare(strict_types=1);
namespace Http\Handler\Formatter;
use Exception;
use Http\Handler\Abstracts\HttpService;
use SimpleXMLElement;
use Swoole\Http\Response;
/**
* Class XmlFormatter
* @package Kiri\Kiri\Http\Formatter
*/
class XmlFormatter extends HttpService implements IFormatter
{
public ?string $data = '';
/** @var Response */
public Response $status;
public array $header = [];
/**
* @param $context
* @return $this
* @throws Exception
*/
public function send($context): static
{
if (!is_string($context)) {
// TODO: Implement send() method.
$dom = new SimpleXMLElement('<xml/>');
$this->toXml($dom, $context);
$this->data = $dom->saveXML();
}
return $this;
}
/**
* @return string|null
*/
public function getData(): ?string
{
$data = $this->data;
$this->clear();
return $data;
}
/**
* @param SimpleXMLElement $dom
* @param $data
*/
public function toXml(SimpleXMLElement $dom, $data)
{
foreach ($data as $key => $val) {
if (is_numeric($key)) {
$key = 'item' . $key;
}
if (is_array($val)) {
$node = $dom->addChild($key);
$this->toXml($node, $val);
} else if (is_object($val)) {
$val = get_object_vars($val);
$node = $dom->addChild($key);
$this->toXml($node, $val);
} else {
$dom->addChild($key, htmlspecialchars((string)$val));
}
}
}
public function clear(): void
{
$this->data = null;
unset($this->data);
}
}