1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faithgen\Testimonies\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Faithgen\Testimonies\Testimonies; |
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
7
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
8
|
|
|
|
9
|
|
|
class CreateRequest extends FormRequest |
10
|
|
|
{ |
11
|
|
|
private $primaryRules = [ |
12
|
|
|
'title' => 'required|string', |
13
|
|
|
'testimony' => 'required|string', |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Gets the rules for a ministry with a premium subscription. |
18
|
|
|
* |
19
|
|
|
* @return array an set of premium attributes |
20
|
|
|
*/ |
21
|
|
|
private function getPremiumRules(): array |
22
|
|
|
{ |
23
|
|
|
return array_merge($this->primaryRules, [ |
24
|
|
|
'images' => 'array|max:'.Testimonies::$premiumImageCount, |
25
|
|
|
'images.*' => 'base64image', |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Gets the rules for a ministry with a premuim+ subscription. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
private function getPremiumPlusRules(): array |
35
|
|
|
{ |
36
|
|
|
return array_merge($this->getPremiumRules(), [ |
37
|
|
|
'images' => 'array|max:'.Testimonies::$premiumPlusImageCount, |
38
|
|
|
'resource' => 'url', |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the user is authorized to make this request. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function authorize() |
48
|
|
|
{ |
49
|
|
|
$user = auth('web')->user(); |
|
|
|
|
50
|
|
|
if ($user && $user->active) { |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the validation rules that apply to the request. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function rules() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$subscriptionLevel = auth()->user()->account->level; |
|
|
|
|
65
|
|
|
if ($subscriptionLevel === 'Free') { |
66
|
|
|
return $this->primaryRules; |
67
|
|
|
} |
68
|
|
|
if ($subscriptionLevel === 'Premium') { |
69
|
|
|
return $this->getPremiumRules(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->getPremiumPlusRules(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function failedAuthorization() |
76
|
|
|
{ |
77
|
|
|
throw new AuthorizationException('You are not allowed to be posting testimonies here!'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Converts image string array to usable string in the validation. |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
public function prepareForValidation() |
86
|
|
|
{ |
87
|
|
|
if (is_string($this->images)) { |
|
|
|
|
88
|
|
|
$this->merge([ |
89
|
|
|
'images' => json_decode($this->images, true), |
|
|
|
|
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: