1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace TheSportsDb\PropertyMapper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of PropertyMap |
13
|
|
|
* |
14
|
|
|
* @author drupalpro |
15
|
|
|
*/ |
16
|
|
|
class PropertyMap { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The source property definition. |
20
|
|
|
* |
21
|
|
|
* @var \TheSportsDb\PropertyMapper\PropertyDefinition |
22
|
|
|
*/ |
23
|
|
|
protected $source; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The destination property definition. |
27
|
|
|
* |
28
|
|
|
* @var \TheSportsDb\PropertyMapper\PropertyDefinition |
29
|
|
|
*/ |
30
|
|
|
protected $destination; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The transform method. |
34
|
|
|
* |
35
|
|
|
* @var callable |
36
|
|
|
*/ |
37
|
|
|
protected $transform; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The reverse method. |
41
|
|
|
* |
42
|
|
|
* @var callable |
43
|
|
|
*/ |
44
|
|
|
protected $reverse; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates a new property map. |
48
|
|
|
* |
49
|
|
|
* @param \TheSportsDb\PropertyMapper\PropertyDefinition $source |
50
|
|
|
* The source property definition. |
51
|
|
|
* @param \TheSportsDb\PropertyMapper\PropertyDefinition $destination |
52
|
|
|
* The destination property definition. |
53
|
|
|
* @param callable|null $transform |
54
|
|
|
* The transform method. |
55
|
|
|
* @param callable|null $reverse |
56
|
|
|
* The reverse method |
57
|
|
|
* |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
* When either $transform or $reverse is null and the other isn't. |
60
|
|
|
*/ |
61
|
4 |
|
public function __construct(PropertyDefinition $source, PropertyDefinition $destination, callable $transform = NULL, callable $reverse = NULL) { |
62
|
4 |
|
$this->source = $source; |
63
|
4 |
|
$this->destination = $destination; |
64
|
4 |
|
if (is_null($transform) !== is_null($reverse)) { |
65
|
1 |
|
throw new \InvalidArgumentException('Each transform function should have a reverse function and vice versa.'); |
66
|
|
|
} |
67
|
3 |
|
$this->transform = $transform; |
68
|
3 |
|
$this->reverse = $reverse; |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Gets the source property definition. |
73
|
|
|
* |
74
|
|
|
* @return PropertyDefinition |
75
|
|
|
* The source property definition. |
76
|
|
|
*/ |
77
|
1 |
|
public function getSource() { |
78
|
1 |
|
return $this->source; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Gets the destination property definition. |
83
|
|
|
* |
84
|
|
|
* @return PropertyDefinition |
85
|
|
|
* The destination property definition. |
86
|
|
|
*/ |
87
|
1 |
|
public function getDestination() { |
88
|
1 |
|
return $this->destination; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Gets the transform method. |
93
|
|
|
* |
94
|
|
|
* @return callable|null |
95
|
|
|
* The transform method if set, NULL otherwise. |
96
|
|
|
*/ |
97
|
1 |
|
public function getTransform() { |
98
|
1 |
|
return $this->transform; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the reverse method. |
103
|
|
|
* |
104
|
|
|
* @return callable|null |
105
|
|
|
* The reverse method if set, NULL otherwise. |
106
|
|
|
*/ |
107
|
1 |
|
public function getReverse() { |
108
|
1 |
|
return $this->reverse; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|