Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation\Rules;
4
 
5
use Closure;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Database\Eloquent\Model;
8
 
9
trait DatabaseRule
10
{
11
    /**
12
     * The table to run the query against.
13
     *
14
     * @var string
15
     */
16
    protected $table;
17
 
18
    /**
19
     * The column to check on.
20
     *
21
     * @var string
22
     */
23
    protected $column;
24
 
25
    /**
26
     * The extra where clauses for the query.
27
     *
28
     * @var array
29
     */
30
    protected $wheres = [];
31
 
32
    /**
33
     * The array of custom query callbacks.
34
     *
35
     * @var array
36
     */
37
    protected $using = [];
38
 
39
    /**
40
     * Create a new rule instance.
41
     *
42
     * @param  string  $table
43
     * @param  string  $column
44
     * @return void
45
     */
46
    public function __construct($table, $column = 'NULL')
47
    {
48
        $this->column = $column;
49
 
50
        $this->table = $this->resolveTableName($table);
51
    }
52
 
53
    /**
54
     * Resolves the name of the table from the given string.
55
     *
56
     * @param  string  $table
57
     * @return string
58
     */
59
    public function resolveTableName($table)
60
    {
61
        if (! str_contains($table, '\\') || ! class_exists($table)) {
62
            return $table;
63
        }
64
 
65
        if (is_subclass_of($table, Model::class)) {
66
            $model = new $table;
67
 
68
            if (str_contains($model->getTable(), '.')) {
69
                return $table;
70
            }
71
 
72
            return implode('.', array_map(function (string $part) {
73
                return trim($part, '.');
74
            }, array_filter([$model->getConnectionName(), $model->getTable()])));
75
        }
76
 
77
        return $table;
78
    }
79
 
80
    /**
81
     * Set a "where" constraint on the query.
82
     *
83
     * @param  \Closure|string  $column
84
     * @param  \Illuminate\Contracts\Support\Arrayable|array|string|int|null  $value
85
     * @return $this
86
     */
87
    public function where($column, $value = null)
88
    {
89
        if ($value instanceof Arrayable || is_array($value)) {
90
            return $this->whereIn($column, $value);
91
        }
92
 
93
        if ($column instanceof Closure) {
94
            return $this->using($column);
95
        }
96
 
97
        if (is_null($value)) {
98
            return $this->whereNull($column);
99
        }
100
 
101
        $this->wheres[] = compact('column', 'value');
102
 
103
        return $this;
104
    }
105
 
106
    /**
107
     * Set a "where not" constraint on the query.
108
     *
109
     * @param  string  $column
110
     * @param  \Illuminate\Contracts\Support\Arrayable|array|string  $value
111
     * @return $this
112
     */
113
    public function whereNot($column, $value)
114
    {
115
        if ($value instanceof Arrayable || is_array($value)) {
116
            return $this->whereNotIn($column, $value);
117
        }
118
 
119
        return $this->where($column, '!'.$value);
120
    }
121
 
122
    /**
123
     * Set a "where null" constraint on the query.
124
     *
125
     * @param  string  $column
126
     * @return $this
127
     */
128
    public function whereNull($column)
129
    {
130
        return $this->where($column, 'NULL');
131
    }
132
 
133
    /**
134
     * Set a "where not null" constraint on the query.
135
     *
136
     * @param  string  $column
137
     * @return $this
138
     */
139
    public function whereNotNull($column)
140
    {
141
        return $this->where($column, 'NOT_NULL');
142
    }
143
 
144
    /**
145
     * Set a "where in" constraint on the query.
146
     *
147
     * @param  string  $column
148
     * @param  \Illuminate\Contracts\Support\Arrayable|array  $values
149
     * @return $this
150
     */
151
    public function whereIn($column, $values)
152
    {
153
        return $this->where(function ($query) use ($column, $values) {
154
            $query->whereIn($column, $values);
155
        });
156
    }
157
 
158
    /**
159
     * Set a "where not in" constraint on the query.
160
     *
161
     * @param  string  $column
162
     * @param  \Illuminate\Contracts\Support\Arrayable|array  $values
163
     * @return $this
164
     */
165
    public function whereNotIn($column, $values)
166
    {
167
        return $this->where(function ($query) use ($column, $values) {
168
            $query->whereNotIn($column, $values);
169
        });
170
    }
171
 
172
    /**
173
     * Ignore soft deleted models during the existence check.
174
     *
175
     * @param  string  $deletedAtColumn
176
     * @return $this
177
     */
178
    public function withoutTrashed($deletedAtColumn = 'deleted_at')
179
    {
180
        $this->whereNull($deletedAtColumn);
181
 
182
        return $this;
183
    }
184
 
185
    /**
186
     * Only include soft deleted models during the existence check.
187
     *
188
     * @param  string  $deletedAtColumn
189
     * @return $this
190
     */
191
    public function onlyTrashed($deletedAtColumn = 'deleted_at')
192
    {
193
        $this->whereNotNull($deletedAtColumn);
194
 
195
        return $this;
196
    }
197
 
198
    /**
199
     * Register a custom query callback.
200
     *
201
     * @param  \Closure  $callback
202
     * @return $this
203
     */
204
    public function using(Closure $callback)
205
    {
206
        $this->using[] = $callback;
207
 
208
        return $this;
209
    }
210
 
211
    /**
212
     * Get the custom query callbacks for the rule.
213
     *
214
     * @return array
215
     */
216
    public function queryCallbacks()
217
    {
218
        return $this->using;
219
    }
220
 
221
    /**
222
     * Format the where clauses.
223
     *
224
     * @return string
225
     */
226
    protected function formatWheres()
227
    {
228
        return collect($this->wheres)->map(function ($where) {
229
            return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"';
230
        })->implode(',');
231
    }
232
}