Skip to content

Commit 0829bd0

Browse files
committed
refactors validation
1 parent a093dcd commit 0829bd0

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/Exceptions/Column.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static function enumNotFound(string $enum)
4141
public static function invalidEnum(string $enum)
4242
{
4343
return new static(__(
44-
'Provided enum: ":enum" must use the "Select" trait, or must be a subclass of "Enum" class',
44+
'Provided enum: ":enum" must implement the "Select" interface, or must be a subclass of "Enum" class',
4545
['enum' => $enum]
4646
));
4747
}

src/Services/Template/Validators/Columns/Column.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace LaravelEnso\Tables\Services\Template\Validators\Columns;
44

55
use Illuminate\Support\Collection;
6+
use LaravelEnso\Enums\Contracts\Select as Contract;
67
use LaravelEnso\Enums\Services\Enum;
7-
use LaravelEnso\Enums\Traits\Select;
88
use LaravelEnso\Helpers\Services\Obj;
99
use LaravelEnso\Tables\Attributes\Column as Attributes;
1010
use LaravelEnso\Tables\Attributes\Number;
@@ -112,44 +112,40 @@ private function tooltip(): void
112112
private function missingClass(string $attribute): bool
113113
{
114114
return $this->column->has($attribute)
115-
&& !class_exists($this->column->get($attribute));
115+
&& ! class_exists($this->column->get($attribute));
116116
}
117117

118118
public function enumNotFound(): bool
119119
{
120-
return !class_exists($this->column->get('enum'))
121-
&& !enum_exists($this->column->get('enum'));
120+
return ! class_exists($this->column->get('enum'))
121+
&& ! enum_exists($this->column->get('enum'));
122122
}
123123

124124
public function invalidEnum(): bool
125125
{
126-
$enum = $this->column->get('enum');
127-
128-
if (enum_exists($enum)) {
129-
$traits = (new ReflectionEnum($enum))->getTraits();
130-
131-
return !in_array(Select::class, array_keys($traits));
132-
} else {
133-
return !(new ReflectionClass($enum))->isSubclassOf(Enum::class);
134-
}
126+
return enum_exists($this->column->get('enum'))
127+
? ! (new ReflectionEnum($this->column->get('enum')))
128+
->implementsInterface(Contract::class)
129+
: ! (new ReflectionClass($$this->column->get('enum')))
130+
->isSubclassOf(Enum::class);
135131
}
136132

137133
private function invalidString(string $attribute): bool
138134
{
139135
return $this->column->has($attribute)
140-
&& !is_string($this->column->get($attribute));
136+
&& ! is_string($this->column->get($attribute));
141137
}
142138

143139
private function invalidObject(string $attribute): bool //TODO can be aggregated with invalidAttributes
144140
{
145141
return $this->column->has($attribute)
146-
&& !is_object($this->column->get($attribute));
142+
&& ! is_object($this->column->get($attribute));
147143
}
148144

149145
private function invalidAttribute(string $attribute, array $allowed): bool
150146
{
151147
return $this->column->has($attribute)
152-
&& !in_array($this->column->get($attribute), $allowed);
148+
&& ! in_array($this->column->get($attribute), $allowed);
153149
}
154150

155151
private function invalidAttributes(string $attribute, array $allowed): bool

0 commit comments

Comments
 (0)