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

diego-ninja / granite / 34017810712

06 Sep 2026 06:56AM UTC coverage: 84.549% (+1.2%) from 83.371%
34017810712

Pull #22

github

web-flow
Merge cd8fd393e into f40f1b915
Pull Request #22: fix: harden object pipelines and improve performance

821 of 985 new or added lines in 48 files covered. (83.35%)

14 existing lines in 13 files now uncovered.

3513 of 4155 relevant lines covered (84.55%)

16.56 hits per line

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

38.89
/src/Serialization/ValueSerializer.php
1
<?php
2
// ABOUTME: Defines ValueSerializer as part of the serialization and date metadata pipeline.
3
// ABOUTME: Owns the ValueSerializer boundary between metadata and serialized values.
4

5
// ABOUTME: Normalizes values into arrays and scalars suitable for Granite serialization.
6
// ABOUTME: Recurses through arrays while preserving keys and reporting the complete value path on failure.
7

8
declare(strict_types=1);
9

10
namespace Ninja\Granite\Serialization;
11

12
use BackedEnum;
13
use DateTimeInterface;
14
use Ninja\Granite\Contracts\GraniteObject;
15
use Ninja\Granite\Exceptions\SerializationException;
16
use UnitEnum;
17

18
final class ValueSerializer
19
{
20
    /**
21
     * @param callable(DateTimeInterface): mixed|null $dateFormatter
22
     */
23
    public static function serialize(
1 ✔
24
        mixed $value,
25
        string $objectType,
26
        string $propertyPath,
27
        ?callable $dateFormatter = null,
28
    ): mixed {
29
        if (null === $value || is_scalar($value)) {
1 ✔
NEW
30
            return $value;
×
31
        }
32

33
        if (is_array($value)) {
1 ✔
NEW
34
            $result = [];
×
NEW
35
            foreach ($value as $key => $item) {
×
NEW
36
                $itemPath = $propertyPath . '.' . (string) $key;
×
NEW
37
                $result[$key] = self::serialize($item, $objectType, $itemPath, $dateFormatter);
×
38
            }
39

NEW
40
            return $result;
×
41
        }
42

43
        if ($value instanceof DateTimeInterface) {
1 ✔
NEW
44
            return null !== $dateFormatter
×
NEW
45
                ? $dateFormatter($value)
×
NEW
46
                : $value->format(DateTimeInterface::ATOM);
×
47
        }
48

49
        if ($value instanceof UnitEnum) {
1 ✔
NEW
50
            return $value instanceof BackedEnum ? $value->value : $value->name;
×
51
        }
52

53
        if ($value instanceof GraniteObject) {
1 ✔
54
            return $value->array();
1 ✔
55
        }
56

NEW
57
        throw SerializationException::unsupportedType($objectType, $propertyPath, get_debug_type($value));
×
58
    }
59
}
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