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

mlocati / PayWay / 5212705470

pending completion
5212705470

push

github

mlocati
Initial version

1803 of 1803 new or added lines in 47 files covered. (100.0%)

1313 of 1803 relevant lines covered (72.82%)

1.53 hits per line

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

60.29
/src/Service/UnserializerTrait.php
1
<?php
2

3
namespace MLocati\PayWay\Service;
4

5
use DateTime;
6

7
use DateTimeImmutable;
8
use DOMDocument;
9
use DOMElement;
10
use DOMXPath;
11
use MLocati\PayWay\Exception;
12

13
trait UnserializerTrait
14
{
15
    /**
16
     * @param string|mixed $raw
17
     *
18
     * @throws \MLocati\PayWay\Exception\InvalidXml
19
     *
20
     * @return \DOMElement
21
     */
22
    protected function loadDocument($raw, $rootSelector)
23
    {
24
        if (!is_string($raw) || $raw === '') {
8✔
25
            throw new Exception\InvalidXml('');
×
26
        }
27
        $doc = new DOMDocument('1.0', 'UTF-8');
8✔
28
        $flags = LIBXML_NOCDATA | LIBXML_NOERROR | LIBXML_NOWARNING;
8✔
29
        if (defined('LIBXML_BIGLINES')) {
8✔
30
            $flags |= LIBXML_BIGLINES;
8✔
31
        }
32
        if (!$doc->loadXML($raw, $flags)) {
8✔
33
            throw new Exception\InvalidXml($raw);
2✔
34
        }
35
        $xpath = new DOMXPath($doc);
6✔
36
        $xpath->registerNamespace('soap', static::NS_SOAPENVELOPE);
6✔
37
        $xpath->registerNamespace('igfs', static::NS_IGFS);
6✔
38
        $nodes = $xpath->query($rootSelector);
6✔
39
        if ($nodes->length === 1) {
6✔
40
            return $nodes->item(0);
4✔
41
        }
42

43
        throw $this->buildException($raw, $xpath);
2✔
44
    }
45

46
    /**
47
     * @return \Generator|\DOMElement[]
48
     */
49
    protected function listDOMElements(DOMElement $parent)
50
    {
51
        for ($child = $parent->firstChild; $child = $child->nextSibling; $child) {
4✔
52
            if ($child instanceof DOMElement) {
4✔
53
                yield $child;
4✔
54
            }
55
        }
56
    }
57

58
    /**
59
     * @param string $raw
60
     *
61
     * @return \MLocati\PayWay\Exception\InvalidXml
62
     */
63
    protected function buildException($raw, DOMXPath $xpath)
64
    {
65
        $nodes = $xpath->query('/soap:Envelope/soap:Body/soap:Fault/faultstring');
2✔
66
        $description = $nodes->length === 1 ? trim((string) $nodes->item(0)->textContent) : '';
2✔
67
        $nodes = $xpath->query('/soap:Envelope/soap:Body/soap:Fault/faultcode');
2✔
68
        $code = $nodes->length === 1 ? trim((string) $nodes->item(0)->textContent) : '';
2✔
69
        if ($description === '' || $code === '') {
2✔
70
            $details = $description . $code;
×
71
        } elseif ($description === '' && $code === '') {
2✔
72
            $details = '';
×
73
        } else {
74
            $details = "{$description} (error code: {$code}}";
2✔
75
        }
76

77
        return new Exception\InvalidXml($raw, $details);
2✔
78
    }
79

80
    /**
81
     * @param string|null $value
82
     *
83
     * @throws \MLocati\PayWay\Exception\InvalidValue
84
     *
85
     * @return bool|null
86
     */
87
    protected function unserializeBoolean($value)
88
    {
89
        $value = (string) $value;
4✔
90
        if ($value === '') {
4✔
91
            return null;
×
92
        }
93
        switch (strtolower($value)) {
4✔
94
            case 'true':
4✔
95
            case '1':
2✔
96
            case 'on':
2✔
97
            case 'yes':
2✔
98
            case 't':
2✔
99
            case 'y':
2✔
100
                return true;
2✔
101
            case 'false':
2✔
102
            case '0':
×
103
            case 'off':
×
104
            case 'no':
×
105
            case 'f':
×
106
            case 'n':
×
107
                return false;
2✔
108
        }
109

110
        throw new Exception\InvalidValue("'{$value}' can't be converted to a boolean");
×
111
    }
112

113
    /**
114
     * @param string|null $value
115
     *
116
     * @throws \MLocati\PayWay\Exception\InvalidValue
117
     *
118
     * @return \DateTimeImmutable|null
119
     */
120
    protected function unserializeDateTime($value)
121
    {
122
        $value = (string) $value;
×
123
        if ($value === '') {
×
124
            return null;
×
125
        }
126
        if (defined('DateTime::RFC3339_EXTENDED')) {
×
127
            $result = DateTimeImmutable::createFromFormat(DateTime::RFC3339_EXTENDED, $value);
×
128
            if ($result !== false) {
×
129
                return $result;
×
130
            }
131
        }
132
        $result = DateTimeImmutable::createFromFormat(DateTime::RFC3339, $value);
×
133
        if ($result !== false) {
×
134
            return $result;
×
135
        }
136
        $fixed = preg_replace('/(T\d+:\d+:\d+)\.\d*/', '\1', $value);
×
137
        $result = DateTimeImmutable::createFromFormat(DateTime::RFC3339, $fixed);
×
138
        if ($result !== false) {
×
139
            return $value;
×
140
        }
141

142
        throw new Exception\InvalidValue("'{$value}' can't be converted to a date/time");
×
143
    }
144

145
    /**
146
     * @param string|null $value
147
     *
148
     * @throws \MLocati\PayWay\Exception\InvalidValue
149
     *
150
     * @return int|null
151
     */
152
    protected function unserializeInteger($value)
153
    {
154
        $value = (string) $value;
1✔
155
        if ($value === '') {
1✔
156
            return null;
×
157
        }
158
        if (preg_match('/^[+\-]?[0-9]+$/', $value)) {
1✔
159
            return (int) $value;
1✔
160
        }
161

162
        throw new Exception\InvalidValue("'{$value}' can't be converted to an integer number");
×
163
    }
164
}
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