• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 8677009716

13 Apr 2024 11:45PM UTC coverage: 84.44% (-2.2%) from 86.607%
8677009716

push

github

web-flow
Merge pull request #8776 from kenjis/fix-findQualifiedNameFromPath-Cannot-declare-class-v3

fix: Cannot declare class CodeIgniter\Config\Services, because the name is already in use

0 of 3 new or added lines in 1 file covered. (0.0%)

478 existing lines in 72 files now uncovered.

20318 of 24062 relevant lines covered (84.44%)

188.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.83
/system/Format/XMLFormatter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Format;
15

16
use CodeIgniter\Format\Exceptions\FormatException;
17
use Config\Format;
18
use SimpleXMLElement;
19

20
/**
21
 * XML data formatter
22
 *
23
 * @see \CodeIgniter\Format\XMLFormatterTest
24
 */
25
class XMLFormatter implements FormatterInterface
26
{
27
    /**
28
     * Takes the given data and formats it.
29
     *
30
     * @param array|bool|float|int|object|string|null $data
31
     *
32
     * @return false|string (XML string | false)
33
     */
34
    public function format($data)
35
    {
36
        $config = new Format();
19✔
37

38
        // SimpleXML is installed but default
39
        // but best to check, and then provide a fallback.
40
        if (! extension_loaded('simplexml')) {
19✔
UNCOV
41
            throw FormatException::forMissingExtension(); // @codeCoverageIgnore
×
42
        }
43

44
        $options = $config->formatterOptions['application/xml'] ?? 0;
19✔
45
        $output  = new SimpleXMLElement('<?xml version="1.0"?><response></response>', $options);
19✔
46

47
        $this->arrayToXML((array) $data, $output);
19✔
48

49
        return $output->asXML();
19✔
50
    }
51

52
    /**
53
     * A recursive method to convert an array into a valid XML string.
54
     *
55
     * Written by CodexWorld. Received permission by email on Nov 24, 2016 to use this code.
56
     *
57
     * @see http://www.codexworld.com/convert-array-to-xml-in-php/
58
     *
59
     * @param SimpleXMLElement $output
60
     *
61
     * @return void
62
     */
63
    protected function arrayToXML(array $data, &$output)
64
    {
65
        foreach ($data as $key => $value) {
19✔
66
            $key = $this->normalizeXMLTag($key);
19✔
67

68
            if (is_array($value)) {
19✔
69
                $subnode = $output->addChild("{$key}");
5✔
70
                $this->arrayToXML($value, $subnode);
5✔
71
            } else {
72
                $output->addChild("{$key}", htmlspecialchars("{$value}"));
19✔
73
            }
74
        }
75
    }
76

77
    /**
78
     * Normalizes tags into the allowed by W3C.
79
     * Regex adopted from this StackOverflow answer.
80
     *
81
     * @param int|string $key
82
     *
83
     * @return string
84
     *
85
     * @see https://stackoverflow.com/questions/60001029/invalid-characters-in-xml-tag-name
86
     */
87
    protected function normalizeXMLTag($key)
88
    {
89
        $startChar = 'A-Z_a-z' .
19✔
90
            '\\x{C0}-\\x{D6}\\x{D8}-\\x{F6}\\x{F8}-\\x{2FF}\\x{370}-\\x{37D}' .
19✔
91
            '\\x{37F}-\\x{1FFF}\\x{200C}-\\x{200D}\\x{2070}-\\x{218F}' .
19✔
92
            '\\x{2C00}-\\x{2FEF}\\x{3001}-\\x{D7FF}\\x{F900}-\\x{FDCF}' .
19✔
93
            '\\x{FDF0}-\\x{FFFD}\\x{10000}-\\x{EFFFF}';
19✔
94
        $validName = $startChar . '\\.\\d\\x{B7}\\x{300}-\\x{36F}\\x{203F}-\\x{2040}';
19✔
95

96
        $key = (string) $key;
19✔
97

98
        $key = trim($key);
19✔
99
        $key = preg_replace("/[^{$validName}-]+/u", '', $key);
19✔
100
        $key = preg_replace("/^[^{$startChar}]+/u", 'item$0', $key);
19✔
101

102
        return preg_replace('/^(xml).*/iu', 'item$0', $key); // XML is a reserved starting word
19✔
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc