【调度系统】广东民航医疗快线调度系统源代码
wanglizhong
2025-04-22 72c0df785d4838d35dc694071c61e3f9a54e7e81
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php declare(strict_types=1);
 
namespace WeChatPay;
 
use const LIBXML_VERSION;
use const LIBXML_NONET;
use const LIBXML_COMPACT;
use const LIBXML_NOCDATA;
use const LIBXML_NOBLANKS;
 
use function array_walk;
use function is_array;
use function is_object;
use function is_string;
use function preg_replace;
use function strpos;
use function preg_match;
use function sprintf;
use function trigger_error;
use function libxml_clear_errors;
use function libxml_disable_entity_loader;
use function libxml_get_last_error;
use function libxml_use_internal_errors;
use function simplexml_load_string;
 
use SimpleXMLElement;
use Traversable;
use XMLWriter;
 
/**
 * Transform the `XML` to `Array` or `Array` to `XML`.
 */
class Transformer
{
    /**
     * Convert the $xml string to array.
     *
     * Always issue the `additional Libxml parameters` asof `LIBXML_NONET`
     *                                                    | `LIBXML_COMPACT`
     *                                                    | `LIBXML_NOCDATA`
     *                                                    | `LIBXML_NOBLANKS`
     *
     * @param string $xml - The xml string, default is `<xml/>` string
     *
     * @return array<string,string|array|mixed>
     */
    public static function toArray(string $xml = '<xml/>'): array
    {
        LIBXML_VERSION < 20900 && $previous = libxml_disable_entity_loader(true);
 
        libxml_use_internal_errors(true);
        $el = simplexml_load_string(static::sanitize($xml), SimpleXMLElement::class, LIBXML_NONET | LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS);
 
        LIBXML_VERSION < 20900 && isset($previous) && libxml_disable_entity_loader($previous);
 
        if (false === $el) {
            // while parsing failed, let's clean the internal buffer and
            // only leave the last error message which still can be fetched by the `error_get_last()` function.
            if (false !== ($err = libxml_get_last_error())) {
                libxml_clear_errors();
                @trigger_error(sprintf(
                    'Parsing the $xml failed with the last error(level=%d,code=%d,message=%s).',
                    $err->level, $err->code, $err->message
                ));
            }
 
            return [];
        }
 
        return static::cast($el);
    }
 
    /**
     * Recursive cast the $thing as array data structure.
     *
     * @param array<string,mixed>|object|\SimpleXMLElement $thing - The thing
     *
     * @return array<string,string|array|mixed>
     */
    protected static function cast($thing): array
    {
        $data = (array) $thing;
        array_walk($data, static function(&$value) { static::value($value); });
 
        return $data;
    }
 
    /**
     * Cast the value $thing, specially doing the `array`, `object`, `SimpleXMLElement` to `array`
     *
     * @param string|array<string,string|\SimpleXMLElement|mixed>|object|\SimpleXMLElement $thing - The value thing reference
     */
    protected static function value(&$thing): void
    {
        is_array($thing) && $thing = static::cast($thing);
        if (is_object($thing) && $thing instanceof SimpleXMLElement) {
            $thing = $thing->count() ? static::cast($thing) : (string) $thing;
        }
    }
 
    /**
     * Trim invalid characters from the $xml string
     *
     * @see https://github.com/w7corp/easywechat/pull/1419
     * @license https://github.com/w7corp/easywechat/blob/4.x/LICENSE
     *
     * @param string $xml - The xml string
     */
    public static function sanitize(string $xml): string
    {
        return preg_replace('#[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+#u', '', $xml) ?? '';
    }
 
    /**
     * Transform the given $data array as of an XML string.
     *
     * @param array<string,string|array|mixed> $data - The data array
     * @param boolean $headless - The headless flag, default `true` means without the `<?xml version="1.0" encoding="UTF-8" ?>` doctype
     * @param boolean $indent - Toggle indentation on/off, default is `false` off
     * @param string $root - The root node label, default is `xml` string
     * @param string $item - The nest array identify text, default is `item` string
     *
     * @return string - The xml string
     */
    public static function toXml(array $data, bool $headless = true, bool $indent = false, string $root = 'xml', string $item = 'item'): string
    {
        $writer = new XMLWriter();
        $writer->openMemory();
        $writer->setIndent($indent);
        $headless || $writer->startDocument('1.0', 'utf-8');
        $writer->startElement($root);
        static::walk($writer, $data, $item);
        $writer->endElement();
        $headless || $writer->endDocument();
        $xml = $writer->outputMemory();
        $writer = null;
 
        return $xml;
    }
 
    /**
     * Walk the given data array by the `XMLWriter` instance.
     *
     * @param \XMLWriter $writer - The `XMLWriter` instance reference
     * @param array<string,string|array|mixed> $data - The data array
     * @param string $item - The nest array identify tag text
     */
    protected static function walk(XMLWriter &$writer, array $data, string $item): void
    {
        foreach ($data as $key => $value) {
            $tag = is_string($key) && static::isElementNameValid($key) ? $key : $item;
            $writer->startElement($tag);
            if (is_array($value) || (is_object($value) && $value instanceof Traversable)) {
                static::walk($writer, (array) $value, $item);
            } else {
                static::content($writer, (string) $value);
            }
            $writer->endElement();
        }
    }
 
    /**
     * Write content text.
     *
     * The content text includes the characters `<`, `>`, `&` and `"` are written as CDATA references.
     * All others including `'` are written literally.
     *
     * @param \XMLWriter $writer - The `XMLWriter` instance reference
     * @param string $thing - The content text
     */
    protected static function content(XMLWriter &$writer, string $thing = ''): void
    {
        static::needsCdataWrapping($thing) && $writer->writeCdata($thing) || $writer->text($thing);
    }
 
    /**
     * Checks the name is a valid xml element name.
     *
     * @see \Symfony\Component\Serializer\Encoder\XmlEncoder::isElementNameValid
     * @license https://github.com/symfony/serializer/blob/5.3/LICENSE
     *
     * @param string $name - The name
     *
     * @return boolean - True means valid
     */
    protected static function isElementNameValid(string $name = ''): bool
    {
        return $name && false === strpos($name, ' ') && preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
    }
 
    /**
     * Checks if a value contains any characters which would require CDATA wrapping.
     *
     * Notes here: the `XMLWriter` shall been wrapped the `"` string as `&quot;` symbol string,
     *             it's strictly following the `XMLWriter` specification here.
     *
     * @see \Symfony\Component\Serializer\Encoder\XmlEncoder::needsCdataWrapping
     * @license https://github.com/symfony/serializer/blob/5.3/LICENSE
     *
     * @param string $value - The value
     *
     * @return boolean - True means need
     */
    protected static function needsCdataWrapping(string $value = ''): bool
    {
        return $value && 0 < preg_match('#[>&"<]#', $value);
    }
}