XML::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PlugHttp\Body;
4
5
use PlugHttp\Utils\ContentHelper;
6
7
class XML implements Handler, Advancer
8
{
9
    private Handler $handler;
10
11
    public function getBody($content): array
12
    {
13
        $xml = simplexml_load_string($content);
14
        $json = json_encode($xml);
15
16
        return json_decode($json, true);
17
    }
18
19
    public function next(Handler $handler): void
20
    {
21
        $this->handler = $handler;
22
    }
23
24
    private function isTextXml($server): bool
25
    {
26
        return ContentHelper::contentIs($server, 'text/xml');
27
    }
28
29
    private function isApplicationXml($server): bool
30
    {
31
        return ContentHelper::contentIs($server, 'application/xml');
32
    }
33
34
    public function handle($server): array
35
    {
36
        $isTextXML = $this->isTextXml($server);
37
        $isApplicationXML = $this->isApplicationXml($server);
38
39
        if ($isTextXML || $isApplicationXML) {
40
            return $this->getBody($server->getContent());
41
        }
42
43
        return $this->handler->handle($server);
44
    }
45
}