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

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

19 Sep 2025 03:15PM UTC coverage: 18.962% (+5.0%) from 13.926%
17862408584

push

github

web-flow
Merge pull request #12 from wol-soft/BuilderClassPostProcessor

BuilderInterface

3 of 3 new or added lines in 1 file covered. (100.0%)

99 existing lines in 36 files now uncovered.

106 of 559 relevant lines covered (18.96%)

0.55 hits per line

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

71.15
/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
     */
UNCOV
28
    public function toJSON(array $except = [], int $options = 0, int $depth = 512)
×
29
    {
UNCOV
30
        if ($depth < 1) {
×
UNCOV
31
            return false;
×
32
        }
33

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

37
    /**
38
     * Return a JSON serializable representation of the current state
39
     */
UNCOV
40
    #[\ReturnTypeWillChange]
×
41
    public function jsonSerialize()
42
    {
UNCOV
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✔
UNCOV
58
            return false;
×
59
        }
60

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

64
        if (isset($this->_skipNotProvidedPropertiesMap)) {
1✔
UNCOV
65
            $except = array_merge(
×
UNCOV
66
                $except,
×
UNCOV
67
                array_diff($this->_skipNotProvidedPropertiesMap, array_keys($this->_rawModelDataInput))
×
UNCOV
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✔
UNCOV
77
                $modelData[$key] = $this->_getSerializedValue($this->{$customSerializer}(), $depth, $except);
×
UNCOV
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
            $subData = [];
1✔
98
            foreach ($value as $subKey => $element) {
1✔
99
                $subData[$subKey] = $this->evaluateAttribute($element, $depth, $except);
1✔
100
            }
101
            return $subData;
1✔
102
        }
103

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

107
    private function evaluateAttribute($attribute, int $depth, array $except)
1✔
108
    {
109
        if (!is_object($attribute)) {
1✔
110
            return $attribute;
1✔
111
        }
112

113
        if ($depth === 0 && method_exists($attribute, '__toString')) {
1✔
UNCOV
114
            return (string) $attribute;
×
115
        }
116

117
        return (0 >= $depth)
1✔
UNCOV
118
            ? null
×
119
            : (
1✔
120
            method_exists($attribute, 'toArray')
1✔
121
                ? $attribute->toArray($except, $depth - 1)
1✔
122
                : get_object_vars($attribute)
1✔
123
            );
1✔
124
    }
125

126
    private function _getCustomSerializerMethod(string $property) {
1✔
127
        if (isset(self::$_customSerializer[$property])) {
1✔
128
            return self::$_customSerializer[$property];
1✔
129
        }
130

131
        $customSerializer = 'serialize' . ucfirst($property);
1✔
132
        if (!method_exists($this, $customSerializer)) {
1✔
133
            $customSerializer = false;
1✔
134
        }
135

136
        return self::$_customSerializer[$property] = $customSerializer;
1✔
137
    }
138
}
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

© 2025 Coveralls, Inc