Skip to content

Commit 40c4bc8

Browse files
committed
Fix isArrayable for stdClass
1 parent 0bab599 commit 40c4bc8

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88

9-
## [Unreleased](https://github.com/inspirum/arrayable-php/compare/v1.2.0...master)
9+
## [Unreleased](https://github.com/inspirum/arrayable-php/compare/v1.2.1...master)
1010

1111

12+
## [v1.2.1 (2023-09-10)](https://github.com/inspirum/balikobot-php/compare/v1.2.0...v1.2.1)
13+
### Fixed
14+
- Fixed `isArrayable` for `stdClass`
15+
1216
## [v1.2.0 (2023-03-19)](https://github.com/inspirum/balikobot-php/compare/v1.1.0...v1.2.0)
1317
### Added
1418
- Added support for **PHP 8.2**

src/Convertor.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
namespace Inspirum\Arrayable;
66

77
use RuntimeException;
8-
use Traversable;
98
use UnexpectedValueException;
109
use stdClass;
11-
use function is_array;
1210
use function is_iterable;
13-
use function iterator_to_array;
1411
use const PHP_INT_MAX;
1512

1613
final class Convertor
@@ -20,7 +17,7 @@ final class Convertor
2017
*/
2118
public static function isArrayable(mixed $data): bool
2219
{
23-
return is_iterable($data) || $data instanceof Arrayable;
20+
return is_iterable($data) || $data instanceof Arrayable || $data instanceof stdClass;
2421
}
2522

2623
/**
@@ -50,19 +47,22 @@ private static function toArrayWithDepth(mixed $data, int $limit, int $depth): m
5047
return $data;
5148
}
5249

53-
if ($data instanceof Traversable) {
54-
$data = iterator_to_array($data);
55-
} elseif ($data instanceof Arrayable) {
50+
if ($data instanceof Arrayable) {
5651
$data = $data->__toArray();
5752
} elseif ($data instanceof stdClass) {
5853
$data = (array) $data;
5954
}
6055

61-
if (is_array($data)) {
56+
if (is_iterable($data)) {
57+
$arrayData = [];
6258
foreach ($data as $k => $v) {
63-
$data[$k] = self::toArrayWithDepth($v, $limit, $depth + 1);
59+
$arrayData[$k] = self::toArrayWithDepth($v, $limit, $depth + 1);
6460
}
65-
} elseif ($depth === 1) {
61+
62+
return $arrayData;
63+
}
64+
65+
if ($depth === 1) {
6666
throw new RuntimeException('Cannot cast to array');
6767
}
6868

tests/ConvertorTest.php

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ public static function providesToArray(): iterable
4848
];
4949

5050
yield [
51-
'data' => [
51+
'data' => (static function () {
52+
$o = new stdClass();
53+
$o->foo = 'bar';
54+
$o->debug = true;
55+
56+
return $o;
57+
})(),
58+
'result' => [
59+
'foo' => 'bar',
60+
'debug' => true,
61+
],
62+
];
63+
64+
yield [
65+
'data' => [
5266
2,
5367
(static function () {
5468
$o = new stdClass();
@@ -61,14 +75,14 @@ public static function providesToArray(): iterable
6175
'result' => [
6276
2,
6377
[
64-
'foo' => 'bar',
78+
'foo' => 'bar',
6579
'debug' => true,
6680
],
6781
],
6882
];
6983

7084
yield [
71-
'data' => new class implements Arrayable {
85+
'data' => new class implements Arrayable {
7286
/**
7387
* @return array<mixed>
7488
*/
@@ -81,7 +95,7 @@ public function __toArray(): array
8195
];
8296

8397
yield [
84-
'data' => new class implements CoreArrayable {
98+
'data' => new class implements CoreArrayable {
8599
/**
86100
* @return array<mixed>
87101
*/
@@ -94,20 +108,20 @@ public function __toArray(): array
94108
];
95109

96110
yield [
97-
'data' => new ArrayIterator([3, 4, 5]),
111+
'data' => new ArrayIterator([3, 4, 5]),
98112
'result' => [3, 4, 5],
99113
];
100114

101115
yield [
102-
'data' => (static function () {
116+
'data' => (static function () {
103117
yield 2.3;
104118
yield 8.10;
105119
})(),
106120
'result' => [2.3, 8.10],
107121
];
108122

109123
yield [
110-
'data' => [
124+
'data' => [
111125
1,
112126
3,
113127
[
@@ -134,32 +148,41 @@ public function __toArray(): array
134148
];
135149

136150
yield [
137-
'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
151+
'data' => new ArrayIterator([
152+
1,
153+
new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])]),
154+
]),
138155
'result' => [1, [3, [4, [3, 4, 5]]]],
139156
];
140157

141158
yield [
142-
'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
159+
'data' => new ArrayIterator([
160+
1,
161+
new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])]),
162+
]),
143163
'result' => [1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])],
144-
'limit' => 1,
164+
'limit' => 1,
145165
];
146166

147167
yield [
148-
'data' => new ArrayIterator([1, new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])])]),
168+
'data' => new ArrayIterator([
169+
1,
170+
new ArrayIterator([3, new ArrayIterator([4, new ArrayIterator([3, 4, 5])])]),
171+
]),
149172
'result' => [1, [3, [4, new ArrayIterator([3, 4, 5])]]],
150-
'limit' => 3,
173+
'limit' => 3,
151174
];
152175

153176
yield [
154-
'data' => '1',
177+
'data' => '1',
155178
'result' => new UnexpectedValueException('Limit value should be positive number'),
156-
'limit' => -1,
179+
'limit' => -1,
157180
];
158181

159182
yield [
160-
'data' => '1',
183+
'data' => '1',
161184
'result' => new UnexpectedValueException('Limit value should be positive number'),
162-
'limit' => 0,
185+
'limit' => 0,
163186
];
164187
}
165188
}

0 commit comments

Comments
 (0)