1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Papper; |
4
|
|
|
|
5
|
|
|
use Papper\FluentSyntax\ExecuteMappingFluentSyntax; |
6
|
|
|
use Papper\FluentSyntax\MappingFluentSyntax; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Main entry point for Papper, for both creating maps and performing maps. |
10
|
|
|
* Static proxy to Engine. |
11
|
|
|
* |
12
|
|
|
* @author Vladimir Komissarov <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Papper |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Creates a TypeMap for the source's type and destination's type. |
18
|
|
|
* |
19
|
|
|
* @param string $sourceType Source type |
20
|
|
|
* @param string $destinationType Destination type |
21
|
|
|
* @return MappingFluentSyntax |
22
|
|
|
*/ |
23
|
|
|
public static function createMap($sourceType, $destinationType) |
24
|
|
|
{ |
25
|
|
|
return self::engine()->createMap($sourceType, $destinationType); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Configure mapping |
30
|
|
|
* |
31
|
|
|
* @param MappingConfigurationInterface $configuration |
32
|
|
|
*/ |
33
|
|
|
public static function configureMapping(MappingConfigurationInterface $configuration) |
34
|
|
|
{ |
35
|
|
|
self::engine()->configureMapping($configuration); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initialize a mapping from the source object. |
40
|
|
|
* The source type can be is inferred from the source object. |
41
|
|
|
* |
42
|
|
|
* @param object|object[] $source Source object or collection to map from |
43
|
|
|
* @param string|null $sourceType Source object type |
44
|
|
|
* @throws MappingException |
45
|
|
|
* @return ExecuteMappingFluentSyntax |
46
|
|
|
*/ |
47
|
|
|
public static function map($source, $sourceType = null) |
48
|
|
|
{ |
49
|
|
|
return self::engine()->map($source, $sourceType); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns mapping options |
54
|
|
|
* |
55
|
|
|
* @return MappingOptionsInterface |
56
|
|
|
*/ |
57
|
|
|
public static function mappingOptions() |
58
|
|
|
{ |
59
|
|
|
return self::engine()->getMappingOptions(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Validates that every top level destination property is mapped to source property. |
64
|
|
|
* If not, a ValidationException is thrown detailing any missing mappings. |
65
|
|
|
* |
66
|
|
|
* @throws ValidationException if any TypeMaps contain unmapped properties |
67
|
|
|
*/ |
68
|
|
|
public static function validate() |
69
|
|
|
{ |
70
|
|
|
self::engine()->validate(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Clear out all existing configuration |
75
|
|
|
*/ |
76
|
|
|
public static function reset() |
77
|
|
|
{ |
78
|
|
|
self::engine()->reset(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns engine singleton instance |
83
|
|
|
* |
84
|
|
|
* @return Engine |
85
|
|
|
*/ |
86
|
|
|
public static function engine() |
87
|
|
|
{ |
88
|
|
|
static $instance; |
89
|
|
|
return $instance ?: $instance = new Engine(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//<editor-fold desc="Singleton/Static"> |
93
|
|
|
private function __construct() |
94
|
|
|
{ |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function __clone() |
98
|
|
|
{ |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
102
|
|
|
private function __wakeup() |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
//</editor-fold> |
106
|
|
|
} |
107
|
|
|
|