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

wol-soft / php-json-schema-model-generator-production / 18283358178

06 Oct 2025 01:53PM UTC coverage: 19.081% (+0.1%) from 18.962%
18283358178

push

github

web-flow
Update SerializableTrait.php

Serialize multidimensional nested arrays

6 of 11 new or added lines in 1 file covered. (54.55%)

108 of 566 relevant lines covered (19.08%)

0.55 hits per line

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

66.1
/src/Traits/SerializableTrait.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace PHPModelGenerator\Traits;
6

7
/**
8
 * Provide methods to serialize generated models
9
 *
10
 * Trait SerializableTrait
11
 *
12
 * @package PHPModelGenerator\Traits
13
 */
14
trait SerializableTrait
15
{
16
    private static $_customSerializer = [];
17

18
    /**
19
     * Get a JSON representation of the current state
20
     *
21
     * @param array $except provide a list of properties which shouldn't be contained in the resulting JSON.
22
     *                      eg. if you want to return an user model and don't want the password to be included
23
     * @param int $options  Bitmask for json_encode
24
     * @param int $depth    the maximum level of object nesting. Must be greater than 0
25
     *
26
     * @return string|false
27
     */
28
    public function toJSON(array $except = [], int $options = 0, int $depth = 512)
×
29
    {
30
        if ($depth < 1) {
×
31
            return false;
×
32
        }
33

34
        return json_encode($this->toArray($except, $depth), $options, $depth);
×
35
    }
36

37
    /**
38
     * Return a JSON serializable representation of the current state
39
     */
40
    #[\ReturnTypeWillChange]
×
41
    public function jsonSerialize()
42
    {
43
        return $this->toArray();
×
44
    }
45

46
    /**
47
     * Get an array representation of the current state
48
     *
49
     * @param array $except provide a list of properties which shouldn't be contained in the resulting JSON.
50
     *                      eg. if you want to return an user model and don't want the password to be included
51
     * @param int $depth    the maximum level of object nesting. Must be greater than 0
52
     *
53
     * @return array|false
54
     */
55
    public function toArray(array $except = [], int $depth = 512)
1✔
56
    {
57
        if ($depth < 1) {
1✔
58
            return false;
×
59
        }
60

61
        $depth--;
1✔
62
        $modelData = [];
1✔
63

64
        if (isset($this->_skipNotProvidedPropertiesMap)) {
1✔
65
            $except = array_merge(
×
66
                $except,
×
67
                array_diff($this->_skipNotProvidedPropertiesMap, array_keys($this->_rawModelDataInput))
×
68
            );
×
69
        }
70

71
        foreach (get_class_vars(get_class($this)) as $key => $value) {
1✔
72
            if (in_array($key, $except) || strstr($key, '_') !== false) {
1✔
73
                continue;
1✔
74
            }
75

76
            if ($customSerializer = $this->_getCustomSerializerMethod($key)) {
1✔
77
                $modelData[$key] = $this->_getSerializedValue($this->{$customSerializer}(), $depth, $except);
×
78
                continue;
×
79
            }
80

81
            $modelData[$key] = $this->_getSerializedValue($this->$key, $depth, $except);
1✔
82
        }
83

84
        return $this->resolveSerializationHook($modelData, $depth, $except);
1✔
85
    }
86

87
    /**
88
     * Function can be overwritten by classes using the trait to hook into serialization
89
     */
90
    protected function resolveSerializationHook(array $data, int $depth, array $except): array
1✔
91
    {
92
        return $data;
1✔
93
    }
94

95
    private function _getSerializedValue($value, int $depth, array $except) {
1✔
96
        if (is_array($value)) {
1✔
97
            return array_map(
1✔
98
                fn (mixed $element): mixed => $this->evaluateAttribute($element, $depth - 1, $except),
1✔
99
                $value,
1✔
100
            );
1✔
101
        }
102

103
        return $this->evaluateAttribute($value, $depth, $except);
1✔
104
    }
105

106
    private function evaluateAttribute($attribute, int $depth, array $except)
1✔
107
    {
108
        if ($depth < 0) {
1✔
NEW
109
            return null;
×
110
        }
111

112
        if (is_array($attribute)) {
1✔
NEW
113
            return array_map(
×
NEW
114
                fn (mixed $element): mixed => $this->evaluateAttribute($element, $depth - 1, $except),
×
NEW
115
                $attribute,
×
NEW
116
            );
×
117
        }
118

119
        if (!is_object($attribute)) {
1✔
120
            return $attribute;
1✔
121
        }
122

123
        if ($depth === 0 && method_exists($attribute, '__toString')) {
1✔
124
            return (string) $attribute;
×
125
        }
126

127
        return (0 >= $depth)
1✔
128
            ? null
×
129
            : (
1✔
130
            method_exists($attribute, 'toArray')
1✔
131
                ? $attribute->toArray($except, $depth - 1)
1✔
132
                : get_object_vars($attribute)
1✔
133
            );
1✔
134
    }
135

136
    private function _getCustomSerializerMethod(string $property) {
1✔
137
        if (isset(self::$_customSerializer[$property])) {
1✔
138
            return self::$_customSerializer[$property];
1✔
139
        }
140

141
        $customSerializer = 'serialize' . ucfirst($property);
1✔
142
        if (!method_exists($this, $customSerializer)) {
1✔
143
            $customSerializer = false;
1✔
144
        }
145

146
        return self::$_customSerializer[$property] = $customSerializer;
1✔
147
    }
148
}
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