1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Papper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Mapping context |
7
|
|
|
* |
8
|
|
|
* @author Vladimir Komissarov <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class MappingContext |
11
|
|
|
{ |
12
|
|
|
private $source; |
13
|
|
|
private $sourceType; |
14
|
|
|
private $destination; |
15
|
|
|
private $destinationType; |
16
|
|
|
|
17
|
|
|
public function getSource() |
18
|
|
|
{ |
19
|
|
|
return $this->source; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getSourceType() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
return $this->sourceType; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getDestination() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return $this->destination; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getDestinationType() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
return $this->destinationType; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setSource($source, $sourceType) |
38
|
|
|
{ |
39
|
|
|
if (!(is_object($source) || is_array($source))) { |
40
|
|
|
throw new MappingException('Source type must be object or array instead of ' . gettype($source)); |
41
|
|
|
} |
42
|
|
|
if (is_array($source) && empty($sourceType)) { |
43
|
|
|
throw new MappingException('Source type must explicitly specified for collection mapping'); |
44
|
|
|
} |
45
|
|
|
if (is_object($source) && empty($sourceType)) { |
46
|
|
|
$sourceType = get_class($source); |
47
|
|
|
} |
48
|
|
|
$this->source = $source; |
49
|
|
|
$this->sourceType = $sourceType; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setDestination($destination, $destinationType) |
53
|
|
|
{ |
54
|
|
|
if ($destination !== null && is_array($this->source)) { |
55
|
|
|
throw new NotSupportedException('Collection mapping to existing destination is not supported'); |
56
|
|
|
} |
57
|
|
|
if (is_object($destination) && empty($destinationType)) { |
58
|
|
|
$destinationType = get_class($destination); |
59
|
|
|
} |
60
|
|
|
if (empty($destinationType)) { |
61
|
|
|
throw new MappingException('Destination type must explicitly specified'); |
62
|
|
|
} |
63
|
|
|
$this->destination = $destination; |
64
|
|
|
$this->destinationType = $destinationType; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.