|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Papper\FluentSyntax; |
|
4
|
|
|
|
|
5
|
|
|
use Papper\Internal\Access\PropertyAccessSetter; |
|
6
|
|
|
use Papper\Internal\Access\StdClassPropertySetter; |
|
7
|
|
|
use Papper\Internal\ClosureObjectCreator; |
|
8
|
|
|
use Papper\MemberOptionInterface; |
|
9
|
|
|
use Papper\ObjectCreatorInterface; |
|
10
|
|
|
use Papper\PapperConfigurationException; |
|
11
|
|
|
use Papper\PropertyMap; |
|
12
|
|
|
use Papper\TypeMap; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Mapping configuration options |
|
16
|
|
|
* |
|
17
|
|
|
* @author Vladimir Komissarov <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class MappingFluentSyntax |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var TypeMap |
|
23
|
|
|
*/ |
|
24
|
|
|
private $typeMap; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(TypeMap $typeMap) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->typeMap = $typeMap; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Supply a custom instantiation function for the destination type |
|
33
|
|
|
* |
|
34
|
|
|
* @param ObjectCreatorInterface|\Closure $objectCreator Callback to create the destination type given the source object |
|
35
|
|
|
* @throws PapperConfigurationException |
|
36
|
|
|
* @return MappingFluentSyntax |
|
|
|
|
|
|
37
|
|
|
*/ |
|
38
|
|
|
public function constructUsing($objectCreator) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($objectCreator instanceof \Closure) { |
|
41
|
|
|
$objectCreator = new ClosureObjectCreator($objectCreator); |
|
42
|
|
|
} |
|
43
|
|
|
if (!$objectCreator instanceof ObjectCreatorInterface) { |
|
44
|
|
|
throw new PapperConfigurationException('Argument objectCreator must be closure or instance of Papper\ObjectCreatorInterface'); |
|
45
|
|
|
} |
|
46
|
|
|
$this->typeMap->setObjectCreator($objectCreator); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Customize configuration for individual member |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name Destination member name |
|
53
|
|
|
* @param MemberOptionInterface|MemberOptionInterface[] $memberOptions Member option |
|
54
|
|
|
* @throws PapperConfigurationException |
|
55
|
|
|
* @return MappingFluentSyntax |
|
56
|
|
|
*/ |
|
57
|
|
|
public function forMember($name, $memberOptions) |
|
58
|
|
|
{ |
|
59
|
|
|
$propertyMap = $this->typeMap->getPropertyMap($name); |
|
60
|
|
|
if ($propertyMap === null) { |
|
61
|
|
|
throw new PapperConfigurationException(sprintf('Unable to find destination member %s on type %s', $name, $this->typeMap->getDestinationType())); |
|
62
|
|
|
} |
|
63
|
|
|
$memberOptions = is_array($memberOptions) ? $memberOptions : array($memberOptions); |
|
64
|
|
|
foreach ($memberOptions as $memberOption) { |
|
65
|
|
|
if (!$memberOption instanceof MemberOptionInterface) { |
|
66
|
|
|
throw new PapperConfigurationException('Member options must be array or instance of Papper\MemberOptionInterface'); |
|
67
|
|
|
} |
|
68
|
|
|
$memberOption->apply($this->typeMap, $propertyMap); |
|
69
|
|
|
} |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create configuration for individual dynamic member |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name Destination member name |
|
77
|
|
|
* @param MemberOptionInterface|MemberOptionInterface[] $memberOptions Member option |
|
|
|
|
|
|
78
|
|
|
* @throws PapperConfigurationException |
|
79
|
|
|
* @return MappingFluentSyntax |
|
80
|
|
|
*/ |
|
81
|
|
|
public function forDynamicMember($name, $memberOptions = null) |
|
82
|
|
|
{ |
|
83
|
|
|
$propertyMap = $this->typeMap->getPropertyMap($name); |
|
84
|
|
|
if ($propertyMap !== null) { |
|
85
|
|
|
throw new PapperConfigurationException(sprintf('Unable to create dynamic destination member %s on type %s because it already exists in type', $name, $this->typeMap->getDestinationType())); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$setter = $this->typeMap->getDestinationType() === 'stdClass' |
|
89
|
|
|
? new StdClassPropertySetter($name) |
|
90
|
|
|
: new PropertyAccessSetter($name); |
|
91
|
|
|
|
|
92
|
|
|
$this->typeMap->addPropertyMap($propertyMap = new PropertyMap($setter)); |
|
93
|
|
|
|
|
94
|
|
|
$memberOptions = is_array($memberOptions) ? $memberOptions : array($memberOptions); |
|
95
|
|
|
foreach ($memberOptions as $memberOption) { |
|
96
|
|
|
if (!$memberOption instanceof MemberOptionInterface) { |
|
97
|
|
|
throw new PapperConfigurationException('Member options must be array or instance of Papper\MemberOptionInterface'); |
|
98
|
|
|
} |
|
99
|
|
|
$memberOption->apply($this->typeMap, $propertyMap); |
|
100
|
|
|
} |
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Ignores all remaining unmapped members that do not exist on the destination. |
|
106
|
|
|
* |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function ignoreAllNonExisting() |
|
110
|
|
|
{ |
|
111
|
|
|
foreach ($this->typeMap->getUnmappedPropertyMaps() as $propertyMap) { |
|
112
|
|
|
$propertyMap->ignore(); |
|
113
|
|
|
} |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Execute a custom closure function to the source and/or destination types before member mapping |
|
119
|
|
|
* |
|
120
|
|
|
* @param \Closure $func Callback for the source/destination types |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function beforeMap(\Closure $func) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->typeMap->setBeforeMapFunc($func); |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Execute a custom function to the source and/or destination types after member mapping |
|
131
|
|
|
* |
|
132
|
|
|
* @param \Closure $func Callback for the source/destination types |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function afterMap(\Closure $func) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->typeMap->setAfterMapFunc($func); |
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.