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

andreypostal / json-handler-php / 10464982044

20 Aug 2024 03:53AM UTC coverage: 94.845% (-2.1%) from 96.97%
10464982044

push

github

web-flow
Merge pull request #5 from andreypostal/enums-support

Enums support

43 of 46 new or added lines in 2 files covered. (93.48%)

92 of 97 relevant lines covered (94.85%)

6.19 hits per line

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

92.68
/src/JsonSerializerTrait.php
1
<?php
2
namespace Andrey\JsonHandler;
3

4
use Andrey\JsonHandler\Attributes\JsonItemAttribute;
5
use Andrey\JsonHandler\Attributes\JsonObjectAttribute;
6
use JsonException;
7
use ReflectionClass;
8
use ReflectionException;
9

10
trait JsonSerializerTrait
11
{
12
    /**
13
     * @throws ReflectionException
14
     * @throws JsonException
15
     */
16
    public function serialize(object $obj): array
8✔
17
    {
18
        $class = new ReflectionClass($obj);
8✔
19
        if ($class->isEnum()) {
8✔
NEW
20
            return $obj->value;
×
21
        }
22

23
        $skipAttributeCheck = ($class->getAttributes(JsonObjectAttribute::class)[0] ?? null) !== null;
8✔
24
        $output = [];
8✔
25
        $properties = $class->getProperties();
8✔
26
        foreach ($properties as $property) {
8✔
27
            $attributes = $property->getAttributes(JsonItemAttribute::class);
8✔
28
            $attr = $attributes[0] ?? null;
8✔
29
            if ($attr === null && !$skipAttributeCheck) {
8✔
30
                continue;
1✔
31
            }
32
            /** @var JsonItemAttribute $item */
33
            $item = $attr?->newInstance() ?? new JsonItemAttribute();
8✔
34
            $key = $item->key ?? $property->name;
8✔
35

36
            if ($property->getType()?->isBuiltin()) {
8✔
37
                $output[$key] = $this->handleArray($item, $property->getValue($obj));
8✔
38
                continue;
8✔
39
            }
40

41
            $class = new ReflectionClass($property->getValue($obj));
2✔
42
            if ($class->isEnum()) {
2✔
43
                $output[$key] = $property->getValue($obj)->value;
1✔
44
                continue;
1✔
45
            }
46
            $output[$key] = $this->serialize($property->getValue($obj));
1✔
47
        }
48
        return $output;
8✔
49
    }
50

51
    /**
52
     * @param JsonItemAttribute $item
53
     * @param mixed $value
54
     * @return mixed
55
     * @throws ReflectionException
56
     * @throws JsonException
57
     *
58
     * @noinspection GetTypeMissUseInspection
59
     */
60
    private function handleArray(JsonItemAttribute $item, mixed $value): mixed
8✔
61
    {
62
        if (gettype($value) !== 'array') {
8✔
63
            return $value;
8✔
64
        }
65

66
        if (!class_exists($item->type)) {
1✔
NEW
67
            return $value;
×
68
        }
69
        $class = new ReflectionClass($item->type);
1✔
70
        $isEnum = $class->isEnum();
1✔
71

72
        return array_reduce(
1✔
73
            array: $value,
1✔
74
            callback: function(array $l, mixed $c) use ($isEnum): array {
1✔
75
                if ($isEnum) {
1✔
76
                    $v = $c->value;
1✔
77
                } else {
NEW
78
                    $v = $this->serialize($c);
×
79
                }
80
                $l[] = $v;
1✔
81
                return $l;
1✔
82
            },
1✔
83
            initial: [],
1✔
84
        );
1✔
85
    }
86
}
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