1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 06.10.2015 06:34 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Core\LookUp; |
7
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
use PeekAndPoke\Component\Psi\Psi; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\PropertyMappingMarker; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\PropertyMarker; |
12
|
|
|
use PeekAndPoke\Component\Slumber\Core\Codec\Mapper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Karsten J. Gerber <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class PropertyMarkedForSlumber |
18
|
|
|
{ |
19
|
|
|
/** @var string The name of the property */ |
20
|
|
|
public $name; |
21
|
|
|
/** @var string The field name for the properties value in the storage. CAN BE equal to the propertyName */ |
22
|
|
|
public $alias; |
23
|
|
|
/** @var PropertyMappingMarker The property marker */ |
24
|
|
|
public $marker; |
25
|
|
|
/** @var PropertyMarker[] All additional markers on the property */ |
26
|
|
|
public $allMarkers; |
27
|
|
|
/** @var Mapper */ |
28
|
|
|
public $mapper; |
29
|
|
|
/** @var PropertyAccess Accessor for reading and writing the property */ |
30
|
|
|
public $propertyAccess; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* PropertyMarkedForSlumber constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $propertyName |
36
|
|
|
* @param string $alias |
37
|
|
|
* @param PropertyMappingMarker $marker |
38
|
|
|
* @param PropertyMarker[] $allMarkers |
39
|
|
|
* @param Mapper $mapper |
40
|
|
|
* @param PropertyAccess $propertyAccess |
41
|
|
|
* |
42
|
|
|
* @return PropertyMarkedForSlumber |
43
|
|
|
*/ |
44
|
|
|
public static function create( |
45
|
|
|
$propertyName, |
46
|
|
|
$alias, |
47
|
|
|
PropertyMappingMarker $marker, |
48
|
|
|
$allMarkers, |
49
|
|
|
Mapper $mapper, |
50
|
|
|
PropertyAccess $propertyAccess |
51
|
|
|
) { |
52
|
|
|
$ret = new self; |
53
|
|
|
|
54
|
|
|
$ret->name = $propertyName; |
55
|
|
|
$ret->alias = $alias; |
56
|
|
|
$ret->marker = $marker; |
57
|
|
|
$ret->allMarkers = Psi::it($allMarkers)->filter(new Psi\IsInstanceOf(PropertyMarker::class))->toArray(); |
58
|
|
|
$ret->mapper = $mapper; |
59
|
|
|
$ret->propertyAccess = $propertyAccess; |
60
|
|
|
|
61
|
|
|
return $ret; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $alias |
66
|
|
|
* |
67
|
|
|
* @return PropertyMarkedForSlumber |
68
|
|
|
*/ |
69
|
|
|
public function withAlias($alias) |
70
|
|
|
{ |
71
|
|
|
$clone = clone $this; |
72
|
|
|
$clone->alias = $alias; |
73
|
|
|
|
74
|
|
|
return $clone; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Mapper $mapper |
79
|
|
|
* |
80
|
|
|
* @return PropertyMarkedForSlumber |
81
|
|
|
*/ |
82
|
|
|
public function withMapper(Mapper $mapper) |
83
|
|
|
{ |
84
|
|
|
$clone = clone $this; |
85
|
|
|
$clone->mapper = $mapper; |
86
|
|
|
|
87
|
|
|
return $clone; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $type |
92
|
|
|
* |
93
|
|
|
* @return PropertyMarker|null |
94
|
|
|
*/ |
95
|
|
|
public function getFirstMarkerOf($type) |
96
|
|
|
{ |
97
|
|
|
$is = new Psi\IsInstanceOf($type); |
98
|
|
|
|
99
|
|
|
foreach ($this->allMarkers as $additionalMarker) { |
100
|
|
|
if ($is($additionalMarker)) { |
101
|
|
|
return $additionalMarker; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $type |
110
|
|
|
* |
111
|
|
|
* @return PropertyMarker[] |
112
|
|
|
*/ |
113
|
|
|
public function getMarkersOf($type) |
114
|
|
|
{ |
115
|
|
|
return Psi::it($this->allMarkers) |
116
|
|
|
->filter(new Psi\IsInstanceOf($type)) |
117
|
|
|
->toArray(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|