|
1
|
|
|
<?php |
|
2
|
|
|
namespace einfach\representer; |
|
3
|
|
|
|
|
4
|
|
|
use einfach\representer\serializer\ArraySerializer; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Trait Representer |
|
8
|
|
|
* |
|
9
|
|
|
* @package einfach\representer |
|
10
|
|
|
*/ |
|
11
|
|
|
trait Representer |
|
12
|
|
|
{ |
|
13
|
|
|
use ArraySerializer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Object that is being represented |
|
17
|
|
|
* or a collection handler |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $source; |
|
20
|
|
|
/** |
|
21
|
|
|
* Class name to be restored |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $targetClassName; |
|
26
|
|
|
/** |
|
27
|
|
|
* Strategy indicator |
|
28
|
|
|
* 1 = one |
|
29
|
|
|
* 2 = collection |
|
30
|
|
|
* 3 = restore one |
|
31
|
|
|
* 4 = restore collection |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $strategy; |
|
36
|
|
|
|
|
37
|
6 |
|
public function __construct($source = null, $strategy) |
|
38
|
|
|
{ |
|
39
|
6 |
|
if (is_null($strategy)) { |
|
40
|
|
|
throw new \Exception('Representer can not be initialized without a strategy param'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
$this->source = $source; |
|
44
|
6 |
|
$this->strategy = $strategy; |
|
45
|
6 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function rules() |
|
48
|
|
|
{ |
|
49
|
|
|
return []; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function setTargetClassName($name) |
|
53
|
|
|
{ |
|
54
|
3 |
|
$this->targetClassName = $name; |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $name |
|
59
|
|
|
* @return PropertyRule |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function property($name) |
|
62
|
|
|
{ |
|
63
|
6 |
|
return new PropertyRule($this->source, $name); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Represent one instance |
|
68
|
|
|
* |
|
69
|
|
|
* @param $source |
|
70
|
|
|
* @return static |
|
71
|
|
|
*/ |
|
72
|
4 |
|
public static function one($source) |
|
73
|
|
|
{ |
|
74
|
4 |
|
return new static($source, 1); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Represent collection of instances |
|
79
|
|
|
* |
|
80
|
|
|
* @param array $array |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function collection(array $array) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
//TBD |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getCollectionRepresentation() |
|
88
|
|
|
{ |
|
89
|
|
|
//TBD |
|
90
|
|
|
return ['collection', 'tbd']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
View Code Duplication |
protected function getOneRepresentation() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
4 |
|
$rules = $this->rules(); |
|
96
|
4 |
|
$represented = []; |
|
97
|
|
|
|
|
98
|
4 |
|
if (!empty($rules)) { |
|
99
|
4 |
|
foreach ($rules as $rule) { |
|
100
|
|
|
/** @var $rule PropertyRule */ |
|
101
|
4 |
|
$resultArray = $rule->compile(); |
|
102
|
|
|
|
|
103
|
4 |
|
reset($resultArray); |
|
104
|
4 |
|
$key = key($resultArray); |
|
105
|
4 |
|
$value = $resultArray[$key]; |
|
106
|
|
|
|
|
107
|
4 |
|
$represented[$key] = $value; |
|
108
|
4 |
|
} |
|
109
|
4 |
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
return $represented; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
protected function getRepresentation() |
|
115
|
|
|
{ |
|
116
|
4 |
|
switch ($this->strategy) { |
|
117
|
4 |
|
case 1: |
|
118
|
4 |
|
return $this->getOneRepresentation(); |
|
119
|
|
|
case 2: |
|
120
|
|
|
return $this->getCollectionRepresentation(); |
|
121
|
|
|
default: |
|
122
|
|
|
throw new \Exception('Representation strategy not defined'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param $className |
|
130
|
|
|
* @return static |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public static function restore($className) |
|
133
|
|
|
{ |
|
134
|
3 |
|
$instance = new static(null, 3); |
|
135
|
3 |
|
$instance->setTargetClassName($className); |
|
136
|
3 |
|
return $instance; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
protected function getReverseRepresentation($projection) |
|
140
|
|
|
{ |
|
141
|
3 |
|
switch ($this->strategy) { |
|
142
|
3 |
|
case 3: |
|
143
|
3 |
|
return $this->getOneReverseRepresentation($projection); |
|
144
|
|
|
case 4: |
|
145
|
|
|
return $this->getCollectionReverseRepresentation($projection); |
|
146
|
|
|
default: |
|
147
|
|
|
throw new \Exception('Reverse representation strategy not defined'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
3 |
View Code Duplication |
protected function getOneReverseRepresentation($projection) |
|
|
|
|
|
|
153
|
|
|
{ |
|
154
|
3 |
|
$rules = $this->rules(); |
|
155
|
3 |
|
$target = new $this->targetClassName(); |
|
156
|
|
|
|
|
157
|
3 |
|
if (!empty($rules)) { |
|
158
|
3 |
|
foreach ($rules as $rule) { |
|
159
|
|
|
/** @var $rule PropertyRule */ |
|
160
|
3 |
|
$resultArray = $rule->reverseCompile($projection); |
|
161
|
|
|
|
|
162
|
3 |
|
reset($resultArray); |
|
163
|
3 |
|
$key = key($resultArray); |
|
164
|
3 |
|
$value = $resultArray[$key]; |
|
165
|
|
|
|
|
166
|
3 |
|
$target->$key = $value; |
|
167
|
3 |
|
} |
|
168
|
3 |
|
} |
|
169
|
3 |
|
return $target; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
protected function getCollectionReverseRepresentation($projection) |
|
|
|
|
|
|
173
|
|
|
{ |
|
174
|
|
|
//TBD |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.