Skip to content

Commit d7a888d

Browse files
committed
version 27-10-2022
0 parents  commit d7a888d

9 files changed

+1023
-0
lines changed

DatabaseRule.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
namespace Illuminate\Validation\Rules;
4+
5+
use Closure;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Support\Str;
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 array|string|int|null $value
85+
* @return $this
86+
*/
87+
public function where($column, $value = null)
88+
{
89+
if (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 array|string $value
111+
* @return $this
112+
*/
113+
public function whereNot($column, $value)
114+
{
115+
if (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 array $values
149+
* @return $this
150+
*/
151+
public function whereIn($column, array $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 array $values
163+
* @return $this
164+
*/
165+
public function whereNotIn($column, array $values)
166+
{
167+
return $this->where(function ($query) use ($column, $values) {
168+
$query->whereNotIn($column, $values);
169+
});
170+
}
171+
172+
/**
173+
* Register a custom query callback.
174+
*
175+
* @param \Closure $callback
176+
* @return $this
177+
*/
178+
public function using(Closure $callback)
179+
{
180+
$this->using[] = $callback;
181+
182+
return $this;
183+
}
184+
185+
/**
186+
* Get the custom query callbacks for the rule.
187+
*
188+
* @return array
189+
*/
190+
public function queryCallbacks()
191+
{
192+
return $this->using;
193+
}
194+
195+
/**
196+
* Format the where clauses.
197+
*
198+
* @return string
199+
*/
200+
protected function formatWheres()
201+
{
202+
return collect($this->wheres)->map(function ($where) {
203+
return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"';
204+
})->implode(',');
205+
}
206+
}

Dimensions.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Illuminate\Validation\Rules;
4+
5+
use Illuminate\Support\Traits\Conditionable;
6+
7+
class Dimensions
8+
{
9+
use Conditionable;
10+
11+
/**
12+
* The constraints for the dimensions rule.
13+
*
14+
* @var array
15+
*/
16+
protected $constraints = [];
17+
18+
/**
19+
* Create a new dimensions rule instance.
20+
*
21+
* @param array $constraints
22+
* @return void
23+
*/
24+
public function __construct(array $constraints = [])
25+
{
26+
$this->constraints = $constraints;
27+
}
28+
29+
/**
30+
* Set the "width" constraint.
31+
*
32+
* @param int $value
33+
* @return $this
34+
*/
35+
public function width($value)
36+
{
37+
$this->constraints['width'] = $value;
38+
39+
return $this;
40+
}
41+
42+
/**
43+
* Set the "height" constraint.
44+
*
45+
* @param int $value
46+
* @return $this
47+
*/
48+
public function height($value)
49+
{
50+
$this->constraints['height'] = $value;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* Set the "min width" constraint.
57+
*
58+
* @param int $value
59+
* @return $this
60+
*/
61+
public function minWidth($value)
62+
{
63+
$this->constraints['min_width'] = $value;
64+
65+
return $this;
66+
}
67+
68+
/**
69+
* Set the "min height" constraint.
70+
*
71+
* @param int $value
72+
* @return $this
73+
*/
74+
public function minHeight($value)
75+
{
76+
$this->constraints['min_height'] = $value;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* Set the "max width" constraint.
83+
*
84+
* @param int $value
85+
* @return $this
86+
*/
87+
public function maxWidth($value)
88+
{
89+
$this->constraints['max_width'] = $value;
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* Set the "max height" constraint.
96+
*
97+
* @param int $value
98+
* @return $this
99+
*/
100+
public function maxHeight($value)
101+
{
102+
$this->constraints['max_height'] = $value;
103+
104+
return $this;
105+
}
106+
107+
/**
108+
* Set the "ratio" constraint.
109+
*
110+
* @param float $value
111+
* @return $this
112+
*/
113+
public function ratio($value)
114+
{
115+
$this->constraints['ratio'] = $value;
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* Convert the rule to a validation string.
122+
*
123+
* @return string
124+
*/
125+
public function __toString()
126+
{
127+
$result = '';
128+
129+
foreach ($this->constraints as $key => $value) {
130+
$result .= "$key=$value,";
131+
}
132+
133+
return 'dimensions:'.substr($result, 0, -1);
134+
}
135+
}

0 commit comments

Comments
 (0)