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

contributte / thepay-api / 4023857024

pending completion
4023857024

push

github

kodiakhq[bot]
Bump shivammathur/setup-php from 2.23.0 to 2.24.0

275 of 477 relevant lines covered (57.65%)

0.58 hits per line

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

75.0
/src/DataApi/DataApiObject.php
1
<?php declare(strict_types = 1);
2

3
namespace Tp\DataApi;
4

5
use ArrayAccess;
6
use ReflectionClass;
7
use ReflectionMethod;
8
use ReflectionProperty;
9
use ReturnTypeWillChange;
10
use Tp\Utils;
11

12
abstract class DataApiObject implements ArrayAccess
13
{
14

15
        public function __construct(array $data = [])
16
        {
17
                $keys = static::keys();
1✔
18
                $filtered = Utils::filterKeys($data, $keys);
1✔
19

20
                foreach ($filtered as $key => $value) {
1✔
21
                        $this[$key] = $value;
1✔
22
                }
23
        }
1✔
24

25
        public function toArray(): array
26
        {
27
                $data = [];
1✔
28
                $keys = self::keys();
1✔
29
                foreach ($keys as $name) {
1✔
30
                        $data[$name] = static::demodelizeRecursive($this->{$name});
1✔
31
                }
32

33
                return $data;
1✔
34
        }
35

36
        /**
37
         * @return string[]
38
         */
39
        public static function keys(): array
40
        {
41
                $calledClass = static::class;
1✔
42
                $reflection = new ReflectionClass($calledClass);
1✔
43

44
                // Filter out static properties and those beginning with an underscore.
45
                $allProperties = $reflection->getProperties();
1✔
46
                $dataProperties = array_filter(
1✔
47
                        $allProperties,
48
                        static function (ReflectionProperty $property): bool {
1✔
49
                                return self::filterDataProperties($property);
1✔
50
                        }
1✔
51
                );
52
                $sortedDataProperties = static::sortDataProperties($dataProperties);
1✔
53

54
                $propertyNames = [];
1✔
55
                foreach ($sortedDataProperties as $property) {
1✔
56
                        $propertyNames[] = $property->getName();
1✔
57
                }
58

59
                return $propertyNames;
1✔
60
        }
61

62
        private static function filterDataProperties(
63
                ReflectionProperty $property
64
        ): bool
65
        {
66
                $underscored = strpos($property->getName(), '_') === 0;
1✔
67

68
                return !$underscored && !$property->isStatic();
1✔
69
        }
70

71
        /**
72
         * Prepend inherited properties.
73
         *
74
         * @param ReflectionProperty[] $dataProperties
75
         * @return ReflectionProperty[]
76
         */
77
        private static function sortDataProperties(array $dataProperties): array
78
        {
79
                $inherited = [];
1✔
80
                $own = [];
1✔
81

82
                $calledClassName = static::class;
1✔
83

84
                foreach ($dataProperties as $property) {
1✔
85
                        $propertyClass = $property->getDeclaringClass();
1✔
86
                        $propertyClassName = $propertyClass->getName();
1✔
87

88
                        if ($propertyClassName === $calledClassName) {
1✔
89
                                $own[] = $property;
1✔
90
                        } else {
91
                                $inherited[] = $property;
1✔
92
                        }
93
                }
94

95
                return array_merge($inherited, $own);
1✔
96
        }
97

98
        protected static function demodelizeRecursive($value)
99
        {
100
                if ($value instanceof self) {
1✔
101
                        $demodelized = $value->toArray();
×
102
                } else {
103
                        if (is_array($value)) {
1✔
104
                                $demodelized = [];
×
105
                                foreach ($value as $k => $v) {
×
106
                                        $demodelized[$k] = static::demodelizeRecursive($v);
×
107
                                }
108
                        } else {
109
                                $demodelized = $value;
1✔
110
                        }
111
                }
112

113
                return $demodelized;
1✔
114
        }
115

116
        /* *** ArrayAccess *** */
117

118
        /**
119
         * @param string $offset
120
         * @return bool
121
         */
122
        public function offsetExists($offset): bool
123
        {
124
                $keys = static::keys();
×
125
                return in_array($offset, $keys, true);
×
126
        }
127

128
        /**
129
         * @param string $offset
130
         * @return mixed
131
         */
132
        #[ReturnTypeWillChange]
133
        public function offsetGet($offset)
134
        {
135
                $getterName = 'get' . ucfirst($offset);
×
136

137
                return $this->{$getterName}();
×
138
        }
139

140
        /**
141
         * @param string $offset
142
         * @param mixed  $value
143
         */
144
        public function offsetSet($offset, $value): void
145
        {
146
                $setterName = 'set' . ucfirst($offset);
1✔
147

148
                if ($value !== null && is_string($value)) {
1✔
149
                        $reflectionMethod = new ReflectionMethod($this, $setterName);
1✔
150
                        $parameterType = $reflectionMethod->getParameters()[0]->getType();
1✔
151

152
                        assert($parameterType !== null);
153

154
                        switch ($parameterType->getName()) {
1✔
155
                                case 'int':
1✔
156
                                        $value = intval($value);
×
157

158
                                        break;
×
159
                                case 'float':
1✔
160
                                        $value = floatval($value);
×
161

162
                                        break;
×
163
                                case 'bool':
1✔
164
                                        $value = $value === '1';
×
165

166
                                        break;
×
167
                        }
168
                }
169

170
                $this->{$setterName}($value);
1✔
171
        }
1✔
172

173
        /**
174
         * @param string $offset
175
         */
176
        public function offsetUnset($offset): void
177
        {
178
                $this->offsetSet($offset, null);
×
179
        }
×
180

181
}
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