1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: benedikt |
6
|
|
|
* Date: 9/16/17 |
7
|
|
|
* Time: 2:04 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Tfboe\FmLib\Http\Controllers; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Laravel\Lumen\Routing\Controller; |
16
|
|
|
use Tfboe\FmLib\Entity\Helpers\BaseEntityInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Controllers |
20
|
|
|
* @package App\Http\Controllers |
21
|
|
|
*/ |
22
|
|
|
abstract class BaseController extends Controller |
23
|
|
|
{ |
24
|
|
|
//<editor-fold desc="Fields"> |
25
|
|
|
/** |
26
|
|
|
* @var EntityManagerInterface |
27
|
|
|
*/ |
28
|
|
|
private $entityManager; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $datetimetzFormat = 'Y-m-d H:i:s e'; |
33
|
|
|
//</editor-fold desc="Fields"> |
34
|
|
|
|
35
|
|
|
//<editor-fold desc="Constructor"> |
36
|
|
|
/** |
37
|
|
|
* Controllers constructor. |
38
|
|
|
* @param EntityManagerInterface $entityManager |
39
|
|
|
*/ |
40
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
41
|
|
|
{ |
42
|
|
|
$this->entityManager = $entityManager; |
43
|
|
|
} |
44
|
|
|
//</editor-fold desc="Fields"> |
45
|
|
|
//</editor-fold desc="Constructor"> |
46
|
|
|
|
47
|
|
|
//<editor-fold desc="Protected Final Methods"> |
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
protected final function getDatetimetzFormat(): string |
52
|
|
|
{ |
53
|
|
|
return $this->datetimetzFormat; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return EntityManagerInterface |
58
|
|
|
*/ |
59
|
|
|
protected final function getEntityManager(): EntityManagerInterface |
60
|
|
|
{ |
61
|
|
|
return $this->entityManager; |
62
|
|
|
} |
63
|
|
|
//</editor-fold desc="Protected Final Methods"> |
64
|
|
|
|
65
|
|
|
//<editor-fold desc="Protected Methods"> |
66
|
|
|
/** |
67
|
|
|
* Gets a transformation function which transforms a string in datetime format into a datetime with the given timezone |
68
|
|
|
* @return \Closure the function which transforms a string into a datetime |
69
|
|
|
*/ |
70
|
|
|
protected function datetimetzTransformer(): \Closure |
71
|
|
|
{ |
72
|
|
|
return function ($dateString) { |
73
|
|
|
return \DateTime::createFromFormat($this->datetimetzFormat, $dateString); |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Gets a transformation function which transforms an enum name into the corresponding value |
79
|
|
|
* @param string $enumName the name of the enum |
80
|
|
|
* @return \Closure the function which transforms a name into the enum value |
81
|
|
|
*/ |
82
|
|
|
protected function enumTransformer(string $enumName): \Closure |
83
|
|
|
{ |
84
|
|
|
return function ($name) use ($enumName) { |
85
|
|
|
return call_user_func([$enumName, "getValue"], $name); |
86
|
|
|
}; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Fills an object with the information of inputArray |
91
|
|
|
* @param BaseEntityInterface $object the object to fill |
92
|
|
|
* @param array $specification the specification how to fill the object |
93
|
|
|
* @param array $inputArray the input array |
94
|
|
|
* @return mixed the object |
95
|
|
|
*/ |
96
|
|
|
protected function setFromSpecification(BaseEntityInterface $object, array $specification, array $inputArray) |
97
|
|
|
{ |
98
|
|
|
foreach ($specification as $key => $values) { |
99
|
|
|
if (!array_key_exists('ignore', $values) || $values['ignore'] != true) { |
100
|
|
|
$matches = []; |
101
|
|
|
preg_match('/[^\.]*$/', $key, $matches); |
102
|
|
|
$arrKey = $matches[0]; |
103
|
|
|
if (array_key_exists('property', $values)) { |
104
|
|
|
$property = $values['property']; |
105
|
|
|
} else { |
106
|
|
|
$property = $arrKey; |
107
|
|
|
} |
108
|
|
|
$setter = 'set' . ucfirst($property); |
109
|
|
|
if (array_key_exists($arrKey, $inputArray)) { |
110
|
|
|
$value = $inputArray[$arrKey]; |
111
|
|
|
$this->transformValue($value, $values); |
112
|
|
|
$object->$setter($value); |
113
|
|
|
} else if (array_key_exists('default', $values) && $object->methodExists($setter)) { |
114
|
|
|
$object->$setter($values['default']); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return $object; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Transforms the given value based on different configurations in specification. |
123
|
|
|
* @param mixed $value the value to optionally transform |
124
|
|
|
* @param array $specification the specification for this value |
125
|
|
|
*/ |
126
|
|
|
protected function transformValue(&$value, array $specification) |
127
|
|
|
{ |
128
|
|
|
if (array_key_exists('reference', $specification)) { |
129
|
|
|
$value = $this->getEntityManager()->find($specification['reference'], $value); |
130
|
|
|
} |
131
|
|
|
if (array_key_exists('type', $specification)) { |
132
|
|
|
$value = self::transformByType($value, $specification['type']); |
133
|
|
|
} |
134
|
|
|
if (array_key_exists('transformer', $specification)) { |
135
|
|
|
$value = $specification['transformer']($value); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Validates the parameters of a request by the validate fields of the given specification |
141
|
|
|
* @param Request $request the request |
142
|
|
|
* @param array $specification the specification |
143
|
|
|
* @return $this|BaseController |
144
|
|
|
*/ |
145
|
|
|
protected function validateBySpecification(Request $request, array $specification): BaseController |
146
|
|
|
{ |
147
|
|
|
$arr = []; |
148
|
|
|
foreach ($specification as $key => $values) { |
149
|
|
|
if (array_key_exists('validation', $values)) { |
150
|
|
|
$arr[$key] = $values['validation']; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
$this->validate($request, $arr); |
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
//</editor-fold desc="Protected Methods"> |
157
|
|
|
|
158
|
|
|
//<editor-fold desc="Private Methods"> |
159
|
|
|
/** |
160
|
|
|
* Transforms a value from a standard json communication format to its original php format. Counter part of |
161
|
|
|
* valueToJson(). |
162
|
|
|
* @param string $value the json representation of the value |
163
|
|
|
* @param string $type the type of the value |
164
|
|
|
* @return mixed the real php representation of the value |
165
|
|
|
*/ |
166
|
|
|
private static function transformByType($value, $type) |
167
|
|
|
{ |
168
|
|
|
if (strtolower($type) === 'date' || strtolower($type) === 'datetime') { |
169
|
|
|
return new \DateTime($value); |
170
|
|
|
} |
171
|
|
|
return $value; |
172
|
|
|
} |
173
|
|
|
//</editor-fold desc="Private Methods"> |
174
|
|
|
} |