• 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

73.68
/src/Mapping/Core/MappingEngine.php
1
<?php
2

3
namespace Ninja\Granite\Mapping\Core;
4

5
use Exception;
6
use Ninja\Granite\Exceptions\GraniteException;
7
use Ninja\Granite\Mapping\Exceptions\MappingException;
8

9
/**
10
 * Core mapping engine responsible for executing mapping operations.
11
 * Delegates specific tasks to specialized components.
12
 */
13
final class MappingEngine
14
{
15
    private SourceNormalizer $sourceNormalizer;
16
    private DataTransformer $dataTransformer;
17
    private ObjectFactory $objectFactory;
18
    private ConfigurationBuilder $configBuilder;
19

20
    public function __construct(ConfigurationBuilder $configBuilder)
10✔
21
    {
22
        $this->configBuilder = $configBuilder;
10✔
23
        $this->sourceNormalizer = new SourceNormalizer();
10✔
24
        $this->dataTransformer = new DataTransformer();
10✔
25
        $this->objectFactory = new ObjectFactory();
10✔
26
    }
27

28
    /**
29
     * Map source data to destination type.
30
     * @throws MappingException
31
     * @throws GraniteException
32
     */
33
    public function map(mixed $source, string $destinationType): object
22✔
34
    {
35
        $this->validateDestinationType($destinationType);
22✔
36

37
        try {
38
            $sourceData = $this->sourceNormalizer->normalize($source);
21✔
39
            $config = $this->configBuilder->getConfiguration($source, $destinationType);
21✔
40
            $transformedData = $this->dataTransformer->transform($sourceData, $config);
21✔
41

42
            if ( ! class_exists($destinationType)) {
20✔
NEW
43
                $sourceType = is_object($source) ? get_class($source) : 'array';
×
NEW
44
                throw new MappingException($sourceType, $destinationType, "Destination type '{$destinationType}' is not a valid class");
×
45
            }
46

47
            return $this->objectFactory->create($transformedData, $destinationType);
20✔
48

49
        } catch (GraniteException $e) {
1✔
50
            throw $e;
×
51
        } catch (Exception $e) {
1✔
52
            throw $this->createMappingException($source, $destinationType, $e);
1✔
53
        }
54
    }
55

56
    /**
57
     * Map source data to existing destination object.
58
     * @throws MappingException
59
     */
60
    public function mapTo(mixed $source, object $destination): object
×
61
    {
62
        try {
63
            $sourceData = $this->sourceNormalizer->normalize($source);
×
64
            $config = $this->configBuilder->getConfiguration($source, get_class($destination));
×
65
            $transformedData = $this->dataTransformer->transform($sourceData, $config);
×
66

67
            return $this->objectFactory->populate($destination, $transformedData);
×
68

69
        } catch (Exception $e) {
×
70
            throw $this->createMappingException($source, get_class($destination), $e);
×
71
        }
72
    }
73

74
    /**
75
     * Validate that destination type exists and is instantiable.
76
     * @throws MappingException
77
     */
78
    private function validateDestinationType(string $destinationType): void
22✔
79
    {
80
        if ( ! class_exists($destinationType)) {
22✔
81
            throw MappingException::destinationTypeNotFound($destinationType);
1✔
82
        }
83
    }
84

85
    /**
86
     * Create a mapping exception with context.
87
     */
88
    private function createMappingException(mixed $source, string $destinationType, Exception $previous): MappingException
1✔
89
    {
90
        $sourceType = is_object($source) ? get_class($source) : gettype($source);
1✔
91

92
        return new MappingException(
1✔
93
            $sourceType,
1✔
94
            $destinationType,
1✔
95
            "Mapping failed: " . $previous->getMessage(),
1✔
96
            null,
1✔
97
            0,
1✔
98
            $previous,
1✔
99
        );
1✔
100
    }
101
}
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