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

diego-ninja / granite / 16608963009

29 Jul 2025 10:37PM UTC coverage: 50.423%. First build
16608963009

Pull #5

github

web-flow
Merge 43d8840d7 into 6a6caca51
Pull Request #5: code: adds phpstan level max, pint with per and github actions

321 of 632 new or added lines in 77 files covered. (50.79%)

1132 of 2245 relevant lines covered (50.42%)

9.88 hits per line

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

70.0
/src/Mapping/Core/DataTransformer.php
1
<?php
2

3
namespace Ninja\Granite\Mapping\Core;
4

5
final readonly class DataTransformer
6
{
7
    public function transform(array $sourceData, array $mappingConfig): array
21✔
8
    {
9
        $result = [];
21✔
10

11
        foreach ($mappingConfig as $destinationProperty => $config) {
21✔
12
            if ( ! is_array($config)) {
21✔
NEW
13
                continue;
×
14
            }
15

16
            if ($config['ignore'] ?? false) {
21✔
17
                continue;
×
18
            }
19

20
            if ( ! $this->shouldApplyMapping($config, $sourceData)) {
21✔
NEW
21
                continue;
×
22
            }
23

24
            $sourceKey = $config['source'] ?? null;
21✔
25
            if ( ! is_string($sourceKey)) {
21✔
26
                continue;
×
27
            }
28

29
            $sourceValue = $this->getSourceValue($sourceData, $sourceKey);
21✔
30
            $transformedValue = $this->applyTransformation($sourceValue, $config, $sourceData);
21✔
31
            $result[$destinationProperty] = $this->applyDefaultValue($transformedValue, $config);
20✔
32
        }
33

34
        return $result;
20✔
35
    }
36

37
    private function shouldApplyMapping(array $config, array $sourceData): bool
21✔
38
    {
39
        $condition = $config['condition'] ?? null;
21✔
40
        return null === $condition || (is_callable($condition) && $condition($sourceData));
21✔
41
    }
42

43
    private function getSourceValue(array $sourceData, string $key): mixed
21✔
44
    {
45
        if (str_contains($key, '.')) {
21✔
46
            return $this->getNestedValue($sourceData, $key);
2✔
47
        }
48

49
        return $sourceData[$key] ?? null;
21✔
50
    }
51

52
    private function getNestedValue(array $data, string $key): mixed
2✔
53
    {
54
        $keys = explode('.', $key);
2✔
55
        $value = $data;
2✔
56

57
        foreach ($keys as $key) {
2✔
58
            if ( ! is_array($value) || ! array_key_exists($key, $value)) {
2✔
59
                return null;
×
60
            }
61
            $value = $value[$key];
2✔
62
        }
63

64
        return $value;
2✔
65
    }
66

67
    private function applyTransformation(mixed $value, array $config, array $sourceData): mixed
21✔
68
    {
69
        $transformer = $config['transformer'] ?? null;
21✔
70

71
        if (null === $transformer) {
21✔
72
            return $value;
11✔
73
        }
74

75
        return match (true) {
76
            is_callable($transformer) => $transformer($value, $sourceData),
11✔
77
            is_object($transformer) && method_exists($transformer, 'transform') => $transformer->transform($value, $sourceData),
×
NEW
78
            is_array($transformer) && 2 === count($transformer) => $this->invokeArrayCallable($transformer, $value, $sourceData),
×
79
            default => $value,
10✔
80
        };
81
    }
82

83
    private function invokeArrayCallable(array $transformer, mixed $value, array $sourceData): mixed
×
84
    {
85
        [$class, $method] = $transformer;
×
86

87
        if (is_string($class) && class_exists($class)) {
×
88
            return $class::$method($value, $sourceData);
×
89
        }
90

91
        if (is_object($class)) {
×
NEW
92
            return $class->{$method}($value, $sourceData);
×
93
        }
94

95
        return $value;
×
96
    }
97

98
    private function applyDefaultValue(mixed $value, array $config): mixed
20✔
99
    {
100
        if (null !== $value || ! ($config['hasDefault'] ?? false)) {
20✔
101
            return $value;
20✔
102
        }
103

104
        return $config['default'];
×
105
    }
106
}
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