1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Port\Request\Abstracts; |
4
|
|
|
|
5
|
|
|
use App\Port\Exception\Exceptions\IncorrectIdException; |
6
|
|
|
use App\Port\Exception\Exceptions\ValidationFailedException; |
7
|
|
|
use App\Port\HashId\Traits\HashIdTrait; |
8
|
|
|
use App\Port\Request\Traits\RequestTrait; |
9
|
|
|
use Illuminate\Contracts\Validation\Validator; |
10
|
|
|
use Illuminate\Foundation\Http\FormRequest as LaravelFrameworkRequest; |
11
|
|
|
use Illuminate\Support\Facades\Config; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Request |
15
|
|
|
* |
16
|
|
|
* A.K.A (app/Http/Requests/Request.php) |
17
|
|
|
* |
18
|
|
|
* @author Mahmoud Zalt <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
abstract class Request extends LaravelFrameworkRequest |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use HashIdTrait; |
24
|
|
|
use RequestTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Overriding this function to throw a custom |
28
|
|
|
* exception instead of the default Laravel exception. |
29
|
|
|
* |
30
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator |
31
|
|
|
* |
32
|
|
|
* @return mixed|void |
33
|
|
|
*/ |
34
|
|
|
public function failedValidation(Validator $validator) |
35
|
|
|
{ |
36
|
|
|
throw new ValidationFailedException($validator->getMessageBag()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Overriding this function to modify the any user input before |
41
|
|
|
* applying the validation rules. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function all() |
46
|
|
|
{ |
47
|
|
|
$requestData = parent::all(); |
48
|
|
|
|
49
|
|
|
// the hash ID feature must be enabled to use this decoder feature. |
50
|
|
|
if (isset($this->decode) && !empty($this->decode) && Config::get('hello.hash-id')) { |
|
|
|
|
51
|
|
|
$requestData = $this->decode($requestData); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $requestData; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $requestData |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
private function decode(Array $requestData) |
63
|
|
|
{ |
64
|
|
|
// without decoding the encoded ID's you won't be able to use |
65
|
|
|
// validation features like `exists:table,id` |
66
|
|
|
foreach ($this->decode as $id) { |
|
|
|
|
67
|
|
|
|
68
|
|
|
if (isset($requestData[$id])) { |
69
|
|
|
// validate the user is not trying to pass real ID |
70
|
|
|
if (is_int($requestData[$id])) { |
71
|
|
|
throw new IncorrectIdException('Only Hashed ID\'s allowed to be passed.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// perform the decoding |
75
|
|
|
$requestData[$id] = $this->decodeThisId($requestData[$id]); |
76
|
|
|
} // do nothing if the input is incorrect, because what if it's not required! |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $requestData; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.