Completed
Push — master ( 37a5ac...cf46bb )
by
unknown
07:36
created

Validable::getUpdatingRules()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * Copyright (c) Padosoft.com 2018.
4
 */
5
6
namespace Padosoft\Laravel\Validable;
7
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Validation\Factory as ValidatorFactory;
11
12
/**
13
 * Trait Validable
14
 * @package Padosoft\Laravel\Validable
15
 * @property Array $rules Validation rules
16
 * @property Array $messages Validation messages
17
 */
18
trait Validable
19
{
20
    /**
21
     * Error message bag
22
     *
23
     * @var Illuminate\Support\MessageBag
24
     */
25
    protected $errors;
26
    /**
27
     * Validator instance
28
     *
29
     * @var Illuminate\Validation\Factory
30
     */
31
    protected $validator = null;
32
33
    protected static function bootValidable()
34
    {
35
        static::saving(function (Model $model) {
36
            if (!$model->hasValidator()) {
37
                $model->setValidator(App::make('validator'));
38
            }
39
40
            return $model->validate();
41
        });
42
    }
43
44
    public function setValidator(ValidatorFactory $validator)
45
    {
46
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Illuminate\Validation\Factory> is incompatible with the declared type object<Padosoft\Laravel\...ate\Validation\Factory> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    public function hasValidator()
50
    {
51
        return $this->validator !== null;
52
    }
53
54
    /**
55
     * Validates current attributes against rules
56
     */
57
    public function validate()
58
    {
59
        $v = $this->validator->make($this->attributes,
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
            $this->exists ? static::getUpdatingRules($this) : static::getCreatingRules(), static::getMessages());
0 ignored issues
show
Bug introduced by
The property exists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
        if ($v->passes()) {
62
            return true;
63
        }
64
        $this->setErrors($v->messages());
65
66
        return false;
67
    }
68
69
    /**
70
     * Set error message bag
71
     *
72
     * @var Illuminate\Support\MessageBag
73
     */
74
    protected function setErrors($errors)
75
    {
76
        $this->errors = $errors;
77
    }
78
79
    /**
80
     * Retrieve error message bag
81
     */
82
    public function getErrors()
83
    {
84
        return $this->errors !== null ? $this->errors : [];
85
    }
86
87
    /**
88
     * Inverse of wasSaved
89
     */
90
    public function hasErrors()
91
    {
92
        return !empty($this->errors);
93
    }
94
95
    /**
96
     * Return true if the validation is passed and the model was saved on db
97
     * @return bool
98
     */
99
    public function wasSaved()
100
    {
101
        return empty($this->errors);
102
    }
103
104
105
    public static function getRules()
106
    {
107
        if (isset(static::$rules)) {
108
            return static::$rules;
109
        }
110
111
        return [];
112
    }
113
114
    public static function getCreatingRules()
115
    {
116
        if (isset(static::$rules)) {
117
            return static::$rules;
118
        }
119
120
        return [];
121
    }
122
123
    protected static function replacePlaceholders(Model $model, $rules)
124
    {
125
        $replaced = [];
126
        foreach ($rules as $key => $rule) {
127
            foreach ($model->attributes as $attr => $val) {
128
                $rule = str_replace('{' . $attr . '}', $val, $rule);
129
            }
130
            $replaced[$key] = $rule;
131
        }
132
133
        return $replaced;
134
    }
135
136
    public static function getUpdatingRules(Model $model)
137
    {
138
        $rules = [];
139
140
        if (isset(static::$rules)) {
141
            $rules = static::$rules;
142
        }
143
144
        if (isset(static::$updating_rules)) {
145
            $rules = static::$updating_rules;
146
        }
147
148
        return static::replacePlaceholders($model, $rules);
149
    }
150
151
    public static function getMessages()
152
    {
153
        if (isset(static::$messages)) {
154
            return static::$messages;
155
        }
156
157
        return [];
158
    }
159
}
160