$fields environment variable name => submitted value * @return Collection * * @throws ValidationException */ public function validate(Egg $egg, array $fields = [], int $userLevel = User::USER_LEVEL_USER): Collection { $query = $egg->variables(); if ($userLevel !== User::USER_LEVEL_ADMIN) { $query = $query->where('user_editable', true)->where('user_viewable', true); } $variables = $query->get(); $data = []; $rules = []; $customAttributes = []; foreach ($variables as $variable) { $data['environment'][$variable->env_variable] = Arr::get($fields, $variable->env_variable); $rules['environment.'.$variable->env_variable] = $variable->rules; $customAttributes['environment.'.$variable->env_variable] = trans('validation.internal.variable_value', ['env' => $variable->name]); } Validator::make($data, $rules, [], $customAttributes)->validate(); return $variables->map(fn (EggVariable $item): ValidatedEggVariable => new ValidatedEggVariable( $item->id, $item->env_variable, $fields[$item->env_variable] ?? null, )); } }