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

nextras / orm / 40780907175

28 Oct 2026 10:33PM UTC coverage: 92.01% (+0.01%) from 91.998%
40780907175

Pull #842

github

web-flow
Merge 6ce4bf857 into 10fe91984
Pull Request #842: Setup DressCode

253 of 306 new or added lines in 64 files covered. (82.68%)

13 existing lines in 9 files now uncovered.

4399 of 4781 relevant lines covered (92.01%)

5.4 hits per line

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

58.62
/src/Entity/ToArrayConverter.php
1
<?php declare(strict_types = 1);
2

3
/**
4
 * This file is part of the Nextras\Orm library.
5
 * This file was inspired by PetrP's ORM library https://github.com/PetrP/Orm/.
6
 * @link       https://github.com/nextras/orm
7
 */
8

9
namespace Nextras\Orm\Entity;
10

11

12
use Nextras\Orm\Relationships\IRelationshipCollection;
13

14

15
class ToArrayConverter
16
{
17
        /**
18
         * @const
19
         * IRelationshipContainer property is returned as IEntity entity.
20
         * IRelationshipCollection property is returned as array of its IEntity entities.
21
         * Other properties are not changed.
22
         */
23
        public const RELATIONSHIP_AS_IS = 1;
24

25
        /**
26
         * @const
27
         * IRelationshipContainer property is returned as entity id.
28
         * IRelationshipCollection property is returned as array of entity ids.
29
         * Other properties are not changed.
30
         */
31
        public const RELATIONSHIP_AS_ID = 2;
32

33
        /**
34
         * @const
35
         * IRelationshipContainer property is returned as array (entity tranformed to array).
36
         * IRelationshipCollection property is returned as array of array (entities tranformed to array).
37
         * Other properties are not changed.
38
         */
39
        public const RELATIONSHIP_AS_ARRAY = 3;
40

41
        /** @var int Maximum recursion level. */
42
        public static int $maxRecursionLevel = 3;
43

44

45
        /**
46
         * Converts IEntity to an array.
47
         * @param list<string> $skipProperties
48
         * @return array<string, mixed>
49
         */
50
        public static function toArray(
51
                IEntity $entity,
52
                int $type = self::RELATIONSHIP_AS_IS,
53
                int $recursionLevel = 0,
54
                array $skipProperties = [],
55
        ): array
56
        {
57
                $return = [];
6 ✔
58
                $metadata = $entity->getMetadata();
6 ✔
59

60
                foreach ($metadata->getProperties() as $name => $metadataProperty) {
6 ✔
61
                        if (in_array($name, $skipProperties, true)) {
6 ✔
62
                                continue;
6 ✔
63
                        }
64

65
                        if (!$entity->hasValue($name)) {
6 ✔
66
                                $value = null;
6 ✔
67
                        } else {
68
                                $value = $entity->getValue($name);
6 ✔
69
                        }
70

71
                        if ($value instanceof IEntity) {
6 ✔
72
                                if ($type === self::RELATIONSHIP_AS_ID) {
×
73
                                        $value = $value->getValue('id');
×
74
                                } elseif ($type === self::RELATIONSHIP_AS_ARRAY) {
×
75
                                        if ($recursionLevel + 1 >= static::$maxRecursionLevel) {
×
76
                                                $value = null;
×
77
                                        } else {
78
                                                $value = static::toArray($value, $type, $recursionLevel + 1);
×
79
                                        }
80
                                }
81
                        } elseif ($value instanceof IRelationshipCollection) {
6 ✔
82
                                if ($type === self::RELATIONSHIP_AS_ID) {
6 ✔
83
                                        $collection = [];
6 ✔
84
                                        foreach ($value as $collectionEntity) {
6 ✔
85
                                                $collection[] = $collectionEntity->getValue('id');
6 ✔
86
                                        }
87
                                        $value = $collection;
6 ✔
UNCOV
88
                                } elseif ($type === self::RELATIONSHIP_AS_ARRAY) {
×
89
                                        $collection = [];
×
90
                                        if ($recursionLevel + 1 < static::$maxRecursionLevel) {
×
91
                                                foreach ($value as $collectionEntity) {
×
92
                                                        $collection[] = static::toArray($collectionEntity, $type, $recursionLevel + 1);
×
93
                                                }
94
                                        }
95
                                        $value = $collection;
×
96
                                }
97
                        }
98

99
                        $return[$name] = $value;
6 ✔
100
                }
101

102
                return $return;
6 ✔
103
        }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc