Completed
Push — master ( b02197...088acd )
by Mahmoud
03:21
created

Request::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace App\Ship\Parents\Requests;
4
5
use App\Containers\Authorization\Traits\AuthorizationTrait;
6
use App\Ship\Engine\Traits\HashIdTrait;
7
use App\Ship\Features\Exceptions\ValidationFailedException;
8
use Illuminate\Contracts\Validation\Validator;
9
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
10
11
/**
12
 * Class Request
13
 *
14
 * A.K.A (app/Http/Requests/Request.php)
15
 *
16
 * @author  Mahmoud Zalt  <[email protected]>
17
 */
18
abstract class Request extends LaravelFormRequest
19
{
20
21
    use HashIdTrait;
22
    use AuthorizationTrait;
23
24
    /**
25
     * Overriding this function to modify the any user input before
26
     * applying the validation rules.
27
     *
28
     * @return  array
29
     */
30
    public function all()
31
    {
32
        $requestData = parent::all();
33
34
        $requestData = $this->applyValidationRulesToUrlParams($requestData);
35
36
        $requestData = $this->decodeHashedIdsBeforeApplyingValidationRules($requestData);
37
38
        return $requestData;
39
    }
40
41
    /**
42
     * apply validation rules to the ID's in the URL, since Laravel
43
     * doesn't validate them by default!
44
     *
45
     * Now you can use validation riles like this: `'id' => 'required|integer|exists:items,id'`
46
     *
47
     * @param array $requestData
48
     *
49
     * @return  array
50
     */
51
    private function applyValidationRulesToUrlParams(Array $requestData)
52
    {
53
        if (isset($this->urlParameters) && !empty($this->urlParameters)) {
0 ignored issues
show
Documentation introduced by
The property urlParameters does not exist on object<App\Ship\Parents\Requests\Request>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
54
            foreach ($this->urlParameters as $param) {
0 ignored issues
show
Documentation introduced by
The property urlParameters does not exist on object<App\Ship\Parents\Requests\Request>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
55
                $requestData[$param] = $this->route($param);
56
            }
57
        }
58
59
        return $requestData;
60
    }
61
62
    /**
63
     * Overriding this function to throw a custom
64
     * exception instead of the default Laravel exception.
65
     *
66
     * @param \Illuminate\Contracts\Validation\Validator $validator
67
     *
68
     * @return mixed|void
69
     */
70
    public function failedValidation(Validator $validator)
71
    {
72
        throw new ValidationFailedException($validator->getMessageBag());
73
    }
74
}
75