Skip to content

Commit 0646ef6

Browse files
committed
Formatted code; Added style ci config file and editorconfig
1 parent 3dea241 commit 0646ef6

File tree

8 files changed

+406
-384
lines changed

8 files changed

+406
-384
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.styleci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

src/ModelValidationServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public function register()
2121
*/
2222
public function boot()
2323
{
24-
$this->publishes([
25-
__DIR__.'/../resources/lang' => resource_path('lang/vendor/modelValidationRules'),
26-
]);
27-
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'modelValidationRules');
24+
$this->publishes([
25+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/modelValidationRules'),
26+
]);
27+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'modelValidationRules');
2828
}
2929
}

src/Rules/ExistsEloquent.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Korridor\LaravelModelValidationRules\Rules;
44

55
use Closure;
6-
use Illuminate\Database\Eloquent\Model;
76
use Illuminate\Contracts\Validation\Rule;
7+
use Illuminate\Database\Eloquent\Model;
88

99
class ExistsEloquent implements Rule
1010
{
@@ -44,7 +44,7 @@ public function __construct(string $model, ?string $key = null, ?Closure $builde
4444
{
4545
$this->model = $model;
4646
$this->key = $key;
47-
$this->setBuilderClosure($builderClosure);
47+
$this->setBuilderClosure($builderClosure);
4848
}
4949

5050
/**
@@ -80,27 +80,29 @@ public function passes($attribute, $value): bool
8080
*/
8181
public function message(): string
8282
{
83-
return trans('modelValidationRules::validation.exists_model', [
84-
'attribute' => $this->attribute,
85-
'model' => class_basename($this->model),
86-
'value' => $this->value,
87-
]);
83+
return trans('modelValidationRules::validation.exists_model', [
84+
'attribute' => $this->attribute,
85+
'model' => class_basename($this->model),
86+
'value' => $this->value,
87+
]);
88+
}
89+
90+
/**
91+
* @param Closure|null $builderClosure
92+
*/
93+
public function setBuilderClosure(?Closure $builderClosure)
94+
{
95+
$this->builderClosure = $builderClosure;
8896
}
8997

90-
/**
91-
* @param Closure|null $builderClosure
92-
*/
93-
public function setBuilderClosure(?Closure $builderClosure) {
94-
$this->builderClosure = $builderClosure;
95-
}
98+
/**
99+
* @param Closure $builderClosure
100+
* @return $this
101+
*/
102+
public function query(Closure $builderClosure): ExistsEloquent
103+
{
104+
$this->setBuilderClosure($builderClosure);
96105

97-
/**
98-
* @param Closure $builderClosure
99-
* @return $this
100-
*/
101-
public function query(Closure $builderClosure): ExistsEloquent
102-
{
103-
$this->setBuilderClosure($builderClosure);
104-
return $this;
105-
}
106+
return $this;
107+
}
106108
}

src/Rules/UniqueEloquent.php

Lines changed: 132 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -6,131 +6,136 @@
66
use Illuminate\Contracts\Validation\Rule;
77
use Illuminate\Database\Eloquent\Model;
88

9-
class UniqueEloquent implements Rule {
10-
11-
/**
12-
* @var string
13-
*/
14-
private $model;
15-
16-
/**
17-
* @var string|null
18-
*/
19-
private $key;
20-
21-
/**
22-
* @var Closure|null
23-
*/
24-
private $builderClosure;
25-
26-
/**
27-
* @var string
28-
*/
29-
private $attribute;
30-
31-
/**
32-
* @var mixed
33-
*/
34-
private $value;
35-
36-
/**
37-
* @var mixed
38-
*/
39-
private $ignoreId;
40-
41-
/**
42-
* @var string
43-
*/
44-
private $ignoreColumn;
45-
46-
/**
47-
* UniqueEloquent constructor.
48-
* @param string $model
49-
* @param string|null $key
50-
* @param Closure|null $builderClosure
51-
*/
52-
public function __construct(string $model, ?string $key = null, ?Closure $builderClosure = null) {
53-
$this->model = $model;
54-
$this->key = $key;
55-
$this->setBuilderClosure($builderClosure);
56-
}
57-
58-
/**
59-
* Determine if the validation rule passes.
60-
*
61-
* @param string $attribute
62-
* @param mixed $value
63-
* @return bool
64-
*/
65-
public function passes($attribute, $value): bool {
66-
$this->attribute = $attribute;
67-
$this->value = $value;
68-
/** @var Model $builder */
69-
$builder = new $this->model();
70-
$builder = $builder->where($this->key === null ? (new $this->model())->getKeyName() : $this->key, $value);
71-
if (null !== $this->builderClosure) {
72-
$builderClosure = $this->builderClosure;
73-
$builder = $builderClosure($builder);
74-
}
75-
if($this->ignoreId !== null) {
76-
$builder = $builder->whereNot(
77-
$this->ignoreColumn === null ? (new $this->model())->getKeyName() : $this->ignoreColumn,
78-
$this->ignoreId
79-
);
80-
}
81-
82-
return $builder->count() === 0;
83-
}
84-
85-
/**
86-
* Get the validation error message.
87-
*
88-
* @return string|array
89-
*/
90-
public function message(): string {
91-
return trans('modelValidationRules::validation.unique_model', [
92-
'attribute' => $this->attribute,
93-
'model' => class_basename($this->model),
94-
'value' => $this->value,
95-
]);
96-
}
97-
98-
/**
99-
* @param Closure|null $builderClosure
100-
*/
101-
public function setBuilderClosure(?Closure $builderClosure): void {
102-
$this->builderClosure = $builderClosure;
103-
}
104-
105-
/**
106-
* @param Closure $builderClosure
107-
* @return $this
108-
*/
109-
public function query(Closure $builderClosure): UniqueEloquent
110-
{
111-
$this->setBuilderClosure($builderClosure);
112-
return $this;
113-
}
114-
115-
/**
116-
* @param mixed $id
117-
* @param string|null $column
118-
*/
119-
public function setIgnore($id, ?string $column = null): void
120-
{
121-
$this->ignoreId = $id;
122-
$this->ignoreColumn = $column;
123-
}
124-
125-
/**
126-
* @param $id
127-
* @param string|null $column
128-
* @return UniqueEloquent
129-
*/
130-
public function ignore($id, ?string $column = null): UniqueEloquent
131-
{
132-
$this->setIgnore($id, $column);
133-
134-
return $this;
135-
}
9+
class UniqueEloquent implements Rule
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $model;
15+
16+
/**
17+
* @var string|null
18+
*/
19+
private $key;
20+
21+
/**
22+
* @var Closure|null
23+
*/
24+
private $builderClosure;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $attribute;
30+
31+
/**
32+
* @var mixed
33+
*/
34+
private $value;
35+
36+
/**
37+
* @var mixed
38+
*/
39+
private $ignoreId;
40+
41+
/**
42+
* @var string
43+
*/
44+
private $ignoreColumn;
45+
46+
/**
47+
* UniqueEloquent constructor.
48+
* @param string $model
49+
* @param string|null $key
50+
* @param Closure|null $builderClosure
51+
*/
52+
public function __construct(string $model, ?string $key = null, ?Closure $builderClosure = null)
53+
{
54+
$this->model = $model;
55+
$this->key = $key;
56+
$this->setBuilderClosure($builderClosure);
57+
}
58+
59+
/**
60+
* Determine if the validation rule passes.
61+
*
62+
* @param string $attribute
63+
* @param mixed $value
64+
* @return bool
65+
*/
66+
public function passes($attribute, $value): bool
67+
{
68+
$this->attribute = $attribute;
69+
$this->value = $value;
70+
/** @var Model $builder */
71+
$builder = new $this->model();
72+
$builder = $builder->where(null === $this->key ? (new $this->model())->getKeyName() : $this->key, $value);
73+
if (null !== $this->builderClosure) {
74+
$builderClosure = $this->builderClosure;
75+
$builder = $builderClosure($builder);
76+
}
77+
if (null !== $this->ignoreId) {
78+
$builder = $builder->whereNot(
79+
null === $this->ignoreColumn ? (new $this->model())->getKeyName() : $this->ignoreColumn,
80+
$this->ignoreId
81+
);
82+
}
83+
84+
return 0 === $builder->count();
85+
}
86+
87+
/**
88+
* Get the validation error message.
89+
*
90+
* @return string|array
91+
*/
92+
public function message(): string
93+
{
94+
return trans('modelValidationRules::validation.unique_model', [
95+
'attribute' => $this->attribute,
96+
'model' => class_basename($this->model),
97+
'value' => $this->value,
98+
]);
99+
}
100+
101+
/**
102+
* @param Closure|null $builderClosure
103+
*/
104+
public function setBuilderClosure(?Closure $builderClosure): void
105+
{
106+
$this->builderClosure = $builderClosure;
107+
}
108+
109+
/**
110+
* @param Closure $builderClosure
111+
* @return $this
112+
*/
113+
public function query(Closure $builderClosure): UniqueEloquent
114+
{
115+
$this->setBuilderClosure($builderClosure);
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* @param mixed $id
122+
* @param string|null $column
123+
*/
124+
public function setIgnore($id, ?string $column = null): void
125+
{
126+
$this->ignoreId = $id;
127+
$this->ignoreColumn = $column;
128+
}
129+
130+
/**
131+
* @param $id
132+
* @param string|null $column
133+
* @return UniqueEloquent
134+
*/
135+
public function ignore($id, ?string $column = null): UniqueEloquent
136+
{
137+
$this->setIgnore($id, $column);
138+
139+
return $this;
140+
}
136141
}

0 commit comments

Comments
 (0)