2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/8 0008
|
|
|
|
|
* Time: 17:29
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-12-14 17:35:35 +08:00
|
|
|
|
2021-09-18 16:54:39 +08:00
|
|
|
namespace Http\Handler\Formatter;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2020-12-25 15:57:52 +08:00
|
|
|
use Exception;
|
2021-09-18 16:54:39 +08:00
|
|
|
use Http\Handler\Abstracts\HttpService;
|
2020-08-31 01:27:08 +08:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
use Swoole\Http\Response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class XmlFormatter
|
2021-08-11 01:04:57 +08:00
|
|
|
* @package Kiri\Kiri\Http\Formatter
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-02-20 17:33:28 +08:00
|
|
|
class XmlFormatter extends HttpService implements IFormatter
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
public ?string $data = '';
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/** @var Response */
|
2020-10-29 18:17:25 +08:00
|
|
|
public Response $status;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-10-29 18:17:25 +08:00
|
|
|
public array $header = [];
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
2020-12-14 17:35:35 +08:00
|
|
|
* @param $context
|
2020-08-31 01:27:08 +08:00
|
|
|
* @return $this
|
2020-12-25 15:57:52 +08:00
|
|
|
* @throws Exception
|
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)) {
|
2020-08-31 01:27:08 +08:00
|
|
|
// TODO: Implement send() method.
|
|
|
|
|
$dom = new SimpleXMLElement('<xml/>');
|
|
|
|
|
|
2020-12-14 17:35:35 +08:00
|
|
|
$this->toXml($dom, $context);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
$this->data = $dom->saveXML();
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return string|null
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public function getData(): ?string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$data = $this->data;
|
|
|
|
|
$this->clear();
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param SimpleXMLElement $dom
|
|
|
|
|
* @param $data
|
|
|
|
|
*/
|
2020-10-29 18:17:25 +08:00
|
|
|
public function toXml(SimpleXMLElement $dom, $data)
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
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 {
|
2020-11-17 16:47:12 +08:00
|
|
|
$dom->addChild($key, htmlspecialchars((string)$val));
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|