|
25 | 25 | use MongoDB\Driver\Cursor; |
26 | 26 | use Override; |
27 | 27 | use RuntimeException; |
| 28 | +use stdClass; |
28 | 29 |
|
29 | 30 | use function array_fill_keys; |
30 | 31 | use function array_is_list; |
|
45 | 46 | use function func_get_args; |
46 | 47 | use function func_num_args; |
47 | 48 | use function get_debug_type; |
| 49 | +use function get_object_vars; |
48 | 50 | use function implode; |
49 | 51 | use function in_array; |
50 | 52 | use function is_array; |
51 | 53 | use function is_bool; |
52 | 54 | use function is_callable; |
53 | 55 | use function is_float; |
54 | 56 | use function is_int; |
| 57 | +use function is_object; |
55 | 58 | use function is_string; |
56 | 59 | use function md5; |
57 | 60 | use function preg_match; |
58 | 61 | use function preg_quote; |
59 | 62 | use function preg_replace; |
| 63 | +use function property_exists; |
60 | 64 | use function serialize; |
61 | 65 | use function sprintf; |
62 | 66 | use function str_ends_with; |
@@ -391,7 +395,7 @@ public function toMql(): array |
391 | 395 | } |
392 | 396 |
|
393 | 397 | $options = [ |
394 | | - 'typeMap' => ['root' => 'array', 'document' => 'array'], |
| 398 | + 'typeMap' => ['root' => 'object', 'document' => 'array'], |
395 | 399 | ]; |
396 | 400 |
|
397 | 401 | // Add custom query options |
@@ -450,8 +454,7 @@ public function toMql(): array |
450 | 454 | $options['projection'] = $projection; |
451 | 455 | } |
452 | 456 |
|
453 | | - // Fix for legacy support, converts the results to arrays instead of objects. |
454 | | - $options['typeMap'] = ['root' => 'array', 'document' => 'array']; |
| 457 | + $options['typeMap'] = ['root' => 'object', 'document' => 'array']; |
455 | 458 |
|
456 | 459 | // Add custom query options |
457 | 460 | if (count($this->options)) { |
@@ -516,7 +519,7 @@ public function getFresh($columns = [], $returnLazy = false) |
516 | 519 | } |
517 | 520 |
|
518 | 521 | foreach ($result as &$document) { |
519 | | - if (is_array($document)) { |
| 522 | + if (is_array($document) || is_object($document)) { |
520 | 523 | $document = $this->aliasIdForResult($document); |
521 | 524 | } |
522 | 525 | } |
@@ -1636,16 +1639,36 @@ private function aliasIdForQuery(array $values): array |
1636 | 1639 | return $values; |
1637 | 1640 | } |
1638 | 1641 |
|
1639 | | - private function aliasIdForResult(array $values): array |
| 1642 | + /** |
| 1643 | + * @template T of array|object |
| 1644 | + * @psalm-param T $values |
| 1645 | + * @psalm-return T |
| 1646 | + */ |
| 1647 | + private function aliasIdForResult(array|object $values): array|object |
1640 | 1648 | { |
1641 | | - if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) { |
1642 | | - $values['id'] = $values['_id']; |
1643 | | - //unset($values['_id']); |
| 1649 | + if (is_array($values)) { |
| 1650 | + if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) { |
| 1651 | + $values['id'] = $values['_id']; |
| 1652 | + //unset($values['_id']); |
| 1653 | + } |
| 1654 | + |
| 1655 | + foreach ($values as $key => $value) { |
| 1656 | + if (is_array($value) || is_object($value)) { |
| 1657 | + $values[$key] = $this->aliasIdForResult($value); |
| 1658 | + } |
| 1659 | + } |
1644 | 1660 | } |
1645 | 1661 |
|
1646 | | - foreach ($values as $key => $value) { |
1647 | | - if (is_array($value)) { |
1648 | | - $values[$key] = $this->aliasIdForResult($value); |
| 1662 | + if ($values instanceof stdClass) { |
| 1663 | + if (property_exists($values, '_id') && ! property_exists($values, 'id')) { |
| 1664 | + $values->id = $values->_id; |
| 1665 | + //unset($values->_id); |
| 1666 | + } |
| 1667 | + |
| 1668 | + foreach (get_object_vars($values) as $key => $value) { |
| 1669 | + if (is_array($value) || is_object($value)) { |
| 1670 | + $values->{$key} = $this->aliasIdForResult($value); |
| 1671 | + } |
1649 | 1672 | } |
1650 | 1673 | } |
1651 | 1674 |
|
|
0 commit comments