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

varhall / utilino / 9059483016

08 Mar 2024 02:31PM UTC coverage: 90.233% (-0.2%) from 90.421%
9059483016

push

github

feropeterko
XML select empty result

3 of 4 new or added lines in 1 file covered. (75.0%)

388 of 430 relevant lines covered (90.23%)

0.9 hits per line

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

87.88
/src/Utils/XmlElement.php
1
<?php
2

3
namespace Varhall\Utilino\Utils;
4

5
use Nette\Utils\DateTime;
6
use Varhall\Utilino\ISerializable;
7

8
class XmlElement implements \IteratorAggregate, ISerializable
9
{
10
    public \SimpleXMLElement|null $xml;
11

12
    public function __construct($xml)
13
    {
14
        if (is_string($xml)) {
1✔
15
            $xml = simplexml_load_string($xml);
1✔
16
        }
17

18
        $this->xml = $xml;
1✔
19
    }
1✔
20

21
    public function __get($name): static|XmlCollection
1✔
22
    {
23
        if ($this->xml && $this->xml->$name && $this->xml->$name->count() > 1) {
1✔
24
            return new XmlCollection($this->xml->$name);
1✔
25
        }
26

27
        return new static($this->xml ? $this->xml->$name : null);
1✔
28
    }
29

30
    public function getIterator(): \Traversable
31
    {
32
        $array = iterator_to_array($this->xml);
×
33

34
        return new \ArrayIterator(array_map(function($item) {
×
35
            return new static($item);
36
        }, $array));
×
37
    }
38

39
    public function value(): string
40
    {
41
        return $this->xml ? trim($this->xml->__toString()) : '';
1✔
42
    }
43

44
    public function number(): int|float|string
45
    {
46
        return is_numeric($this->value()) ? +$this->value() : $this->value();
1✔
47
    }
48

49
    public function date(): ?DateTime
50
    {
51
        return !empty($this->value()) ? new DateTime($this->value()) : null;
1✔
52
    }
53

54
    public function collection(): XmlCollection
55
    {
56
        return new XmlCollection($this->xml->count ? [ $this->xml ] : []);
1✔
57
    }
58

59
    public function select(string $xpath): static|XmlCollection|null
1✔
60
    {
61
        $result = $this->xml->xpath($xpath);
1✔
62

63
        if (empty($result)) {
1✔
NEW
64
            return null;
×
65
        }
66

67
        return count($result) > 1 ? new XmlCollection($result) : new static($result[0]);
1✔
68
    }
69

70
    public function attributes(): array
71
    {
72
        return json_decode(json_encode($this->xml->attributes()), true)['@attributes'] ?? [];
1✔
73
    }
74

75
    public function attribute(string $name): ?string
1✔
76
    {
77
        return $this->attributes()[$name] ?? null;
1✔
78
    }
79

80
    public function toXml(): string
81
    {
82
        return $this->xml->asXML();
1✔
83
    }
84

85
    public function toArray(): array
86
    {
87
        $array = json_decode(json_encode($this->xml), true);
1✔
88
        return $this->lowerKeys($array);
1✔
89
    }
90

91
    public function toJson(): string
92
    {
93
        return json_encode($this->toArray());
1✔
94
    }
95

96
    protected function lowerKeys(array $arr, int $case = CASE_LOWER): array
1✔
97
    {
98
        return array_map(function($item) use ($case){
1✔
99
            if (is_array($item)) {
1✔
100
                $item = $this->lowerKeys($item, $case);
1✔
101
            }
102

103
            return $item;
1✔
104
        }, array_change_key_case($arr, $case));
1✔
105
    }
106
}
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