1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Observers; |
6
|
|
|
use App\Model; |
7
|
|
|
use App\Common\JsonException; |
8
|
|
|
use App\Requests\IRequest; |
9
|
|
|
|
10
|
|
|
use Slim\Container; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class BaseController |
14
|
|
|
* |
15
|
|
|
* @package App\Controller |
16
|
|
|
*/ |
17
|
|
|
abstract class BaseController |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \Illuminate\Validation\Factory |
21
|
|
|
*/ |
22
|
|
|
public $validation; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \App\Common\JsonApiEncoder |
26
|
|
|
*/ |
27
|
|
|
public $encoder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \App\Common\ApiRenderer |
31
|
|
|
*/ |
32
|
|
|
public $apiRenderer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
public $settings; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var \Swift_Mailer |
41
|
|
|
*/ |
42
|
|
|
public $mailer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var \App\Common\MailRenderer |
46
|
|
|
*/ |
47
|
|
|
public $mailRenderer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* BaseController constructor. |
51
|
|
|
* |
52
|
|
|
* @param \Slim\Container $container |
53
|
|
|
*/ |
54
|
|
|
public function __construct(Container $container) |
55
|
|
|
{ |
56
|
|
|
$this->settings = $container['settings']; |
57
|
|
|
|
58
|
|
|
// register providers |
59
|
|
|
$this->apiRenderer = $container['apiRenderer']; |
60
|
|
|
$this->validation = $container['validation']; |
61
|
|
|
$this->mailer = $container['mailer']; |
62
|
|
|
$this->mailRenderer = $container['mailRenderer']; |
63
|
|
|
$this->encoder = $container['encoder']; |
64
|
|
|
|
65
|
|
|
$this->registerModelObservers($this->settings['observers']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array|null|object $params |
70
|
|
|
* @param string $entity |
71
|
|
|
* @param IRequest $request |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
* @throws JsonException |
75
|
|
|
*/ |
76
|
|
|
public function validateRequestParams($params, $entity, $request) |
77
|
|
|
{ |
78
|
|
|
if (!isset($params['data']['attributes'])) { |
79
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', 'Do not see required attribute - data.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$validator = $this->validation->make($params['data']['attributes'], $request->rules()); |
83
|
|
|
|
84
|
|
|
if ($validator->fails()) { |
85
|
|
|
$messages = implode(' ', $validator->messages()->all()); |
86
|
|
|
throw new JsonException($entity, 400, 'Invalid Attribute', $messages); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Register model observers |
94
|
|
|
* @param $observers |
95
|
|
|
*/ |
96
|
|
|
private function registerModelObservers($observers) |
97
|
|
|
{ |
98
|
|
|
foreach ($observers as $observer => $models) { |
99
|
|
|
foreach ($models as $model) { |
100
|
|
|
call_user_func($model.'::observe', $observer); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|