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

mattiabasone / PagOnline / 9799875710

04 Jul 2024 09:05PM UTC coverage: 81.25% (-0.9%) from 82.111%
9799875710

push

github

web-flow
Update deps and PHP requirements (#9)

* update deps and fix files

* update deps and fix files

* update scrutinizer CI step and minor changes

* update CI and phpstan stuff

* update CI

* add coveralls package

* bump coveralls

27 of 35 new or added lines in 10 files covered. (77.14%)

119 existing lines in 17 files now uncovered.

1079 of 1328 relevant lines covered (81.25%)

3.78 hits per line

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

89.09
/src/XmlEntities/BaseXmlEntity.php
1
<?php
2

3
namespace PagOnline\XmlEntities;
4

5
use PagOnline\IgfsUtils;
6
use PagOnline\XmlEntities\Traits\CastProperties;
7
use PagOnline\XmlEntities\Traits\EntityAttributes;
8

9
abstract class BaseXmlEntity implements XmlEntityInterface
10
{
11
    use CastProperties, EntityAttributes;
12

13
    protected array $attributes = [];
14

15
    public function __construct()
16
    {
17
        $this->loadAttributes();
19✔
18
    }
19

20
    /**
21
     * Get object attributes.
22
     */
23
    public function getAttributes(): array
24
    {
25
        return $this->attributes;
7✔
26
    }
27

28
    /**
29
     * Format entity to Xml.
30
     *
31
     * @param string $rootNodeName
32
     *
33
     * @return string
34
     */
35
    public function toXml(string $rootNodeName): string
36
    {
37
        $body = "<{$rootNodeName}>";
7✔
38
        foreach ($this->attributes as $attribute) {
7✔
39
            if (!empty($this->{$attribute})) {
7✔
40
                if (!$this->isEntityAttribute($attribute)) {
7✔
41
                    $value = $this->castAttribute($attribute);
7✔
42
                    if (\is_array($value)) {
7✔
43
                        foreach ($value as $valueEntry) {
1✔
44
                            $body .= $this->attributeValueToTagString($attribute, $valueEntry);
1✔
45
                        }
46
                    } else {
47
                        $body .= $this->attributeValueToTagString($attribute, $value);
7✔
48
                    }
49
                } else {
50
                    $body .= $this->getCustomAttributeXml($attribute);
3✔
51
                }
52
            }
53
        }
54
        $body .= "</{$rootNodeName}>";
7✔
55

56
        return $body;
7✔
57
    }
58

59
    /**
60
     * @param array  $response
61
     * @param string $attribute
62
     */
63
    public function setAttributeFromResponse($response, $attribute): void
64
    {
65
        $value = (string) IgfsUtils::getValue($response, $attribute);
5✔
66
        if ($this->isDateAttribute($attribute)) {
5✔
67
            $tmpValue = IgfsUtils::parseXMLGregorianCalendar($value);
2✔
68
            if ($tmpValue !== null) {
2✔
69
                $value = $tmpValue->getTimestamp();
2✔
70
            } else {
UNCOV
71
                $value = null;
×
72
            }
73
        }
74
        $this->{$attribute} = $value;
5✔
75
    }
76

77
    /**
78
     * Generate BaseXmlEntity.
79
     *
80
     * @param string $xml
81
     * @throws \Exception
82
     */
83
    public static function fromXml($xml): ?XmlEntityInterface
84
    {
85
        if (empty($xml)) {
5✔
86
            return null;
1✔
87
        }
88

89
        $dom = new \SimpleXMLElement($xml, LIBXML_NOERROR, false);
5✔
90
        if ($dom->children()->count() === 0) {
5✔
UNCOV
91
            return null;
×
92
        }
93

94
        $xmlArray = IgfsUtils::parseResponseFields($dom);
5✔
95
        $object = null;
5✔
96
        if (\count($xmlArray) > 0) {
5✔
97
            /** @phpstan-ignore-next-line */
98
            $object = new static();
5✔
99
            foreach ($object->getAttributes() as $attribute) {
5✔
100
                if (!$object->isEntityAttribute($attribute)) {
5✔
101
                    if ($object->getAttributeCastType($attribute) !== 'array') {
5✔
102
                        $object->setAttributeFromResponse($xmlArray, $attribute);
5✔
103
                    } else {
104
                        foreach ($dom->xpath($attribute) as $entry) {
2✔
105
                            $object->{$attribute}[] = $entry->__toString();
2✔
106
                        }
107
                    }
108
                } else {
109
                    $object->setCustomAttributeFromDom($dom, $attribute);
2✔
110
                }
111
            }
112
        }
113

114
        return $object;
5✔
115
    }
116

117
    public function toArray(): array
118
    {
119
        $returnArray = [];
2✔
120
        foreach ($this->attributes as $attribute) {
2✔
121
            $returnArray[$attribute] = $this->{$attribute};
2✔
122
        }
123

124
        return $returnArray;
2✔
125
    }
126

127
    /**
128
     * Load attributes from public properties.
129
     */
130
    protected function loadAttributes(): void
131
    {
132
        $publicProperties = (new \ReflectionObject($this))->getProperties(\ReflectionProperty::IS_PUBLIC);
19✔
133
        foreach ($publicProperties as $publicProperty) {
19✔
134
            $this->attributes[] = $publicProperty->getName();
19✔
135
        }
136
    }
137

138
    /**
139
     * @param string $attribute
140
     * @param string $value
141
     *
142
     * @return string
143
     */
144
    protected function attributeValueToTagString(string $attribute, string $value): string
145
    {
146
        return "<{$attribute}><![CDATA[{$value}]]></{$attribute}>";
7✔
147
    }
148

149
    /**
150
     * @param string $attribute
151
     */
152
    protected function setCustomAttributeFromDom(\SimpleXMLElement $dom, $attribute): void
153
    {
154
        if ($this->entityAttributes[$attribute]['type'] === 'array') {
2✔
155
            $value = [];
2✔
156
            foreach ($dom->xpath($attribute) as $item) {
2✔
157
                $value[] = $this->entityAttributes[$attribute]['namespace']::fromXml($item->asXML());
2✔
158
            }
159
        } else {
UNCOV
160
            $element = $dom->xpath($attribute);
×
UNCOV
161
            $value = null;
×
UNCOV
162
            if (\count($element) > 0) {
×
163
                $value = $this->entityAttributes[$attribute]['namespace']::fromXml($element[0]->asXML());
×
164
            }
165
        }
166
        $this->{$attribute} = $value;
2✔
167
    }
168
}
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

© 2025 Coveralls, Inc