-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMap.php
More file actions
135 lines (108 loc) · 3.17 KB
/
Map.php
File metadata and controls
135 lines (108 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace UniMapper\Entity\Reflection\Property\Option;
use UniMapper\Entity\Reflection;
use UniMapper\Exception\OptionException;
class Map implements Reflection\Property\IOption
{
const KEY = "map";
/** @var callable */
private $filterIn;
/** @var callable */
private $filterOut;
/** @var string */
private $unmapped;
public function __construct(
Reflection\Property $property,
$by = null,
callable $filterIn = null,
callable $filterOut = null
) {
$this->unmapped = empty($by) ? $property->getName() : $by;
$this->filterIn = $filterIn;
$this->filterOut = $filterOut;
}
/**
* Get unmapped name
*
* @return string
*/
public function getUnmapped()
{
return $this->unmapped;
}
/**
* @return callable
*/
public function getFilterIn()
{
return $this->filterIn;
}
/**
* @return callable
*/
public function getFilterOut()
{
return $this->filterOut;
}
public static function create(
Reflection\Property $property,
$value = null,
array $parameters = []
) {
$filterIn = null;
$filterOut = null;
$by = null;
$disabled = false;
// Mapping disabled
if (strtolower($value) === "false") {
$disabled = true;
}
// By
if (array_key_exists(self::KEY . "-by", $parameters)) {
$by = $parameters[self::KEY . "-by"];
}
// Filter
if (array_key_exists(self::KEY . "-filter", $parameters)) {
$filter = explode("|", $parameters[self::KEY . "-filter"]);
if (!isset($filter[0]) || !isset($filter[1])) {
throw new OptionException("You must define input/output filter!");
}
$class = $property->getReflection()->getClassName();
$filterIn = self::createCallback($class, $filter[0]);
if (!$filterIn) {
throw new OptionException("Invalid input filter definition!");
}
$filterOut = self::createCallback($class, $filter[1]);
if (!$filterOut) {
throw new OptionException("Invalid output filter definition!");
}
}
if ($disabled) {
// Mapping disabled
if ($by || $filterIn || $filterOut) {
throw new OptionException(
"Can not configure mapping if option disabled!"
);
}
return false;
}
return new self($property, $by, $filterIn, $filterOut);
}
public static function afterCreate(Reflection\Property $property, $option)
{
if ($property->hasOption(Primary::KEY) && !$option) {
throw new OptionException(
"Mapping can not be disabled on primary property!"
);
}
}
private static function createCallback($class, $method)
{
if (method_exists($class, $method)) {
return [$class, $method];
} elseif (is_callable($method)) {
return $method;
}
return false;
}
}