check()) { $this->merge([ 'status' => $this->status === null ? 0 : 1 ]); return true; } return false; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = [ 'title' => 'required|string|min:7' ]; if ($this->engagement->type == EngagementType::header()) { $rules = array_merge($rules, [ 'content' => 'required|string|min:40' ]); } else { $rules = array_merge($rules, [ 'metaTitle' => 'required|string|min:20|max:60', 'metaDescription' => 'required|string|min:130|max:160' ]); foreach ($this->engagement->content as $i => $block) { foreach ($block['content'] as $iptName => $input) { switch ($input['inputType']['value']) { case InputType::string()->value: $rules = array_merge($rules, [ "content.$i.content.$iptName.value" => 'required|string|min:4' ]); break; case InputType::text()->value: $rules = array_merge($rules, [ "content.$i.content.$iptName.value" => 'required|string|min:15' ]); break; case InputType::image()->value: $ipt = $this->engagement->content[$i]['content'][$iptName]; $max = isset($ipt['dimensionsMax']); $dim = collect(explode( ',', $ipt['dimensions'] ?? $ipt['dimensionsMax'] ))->mapWithKeys(function ($v) { $v = explode(' ', $v); return [$v[0] => $v[1]]; }); $rules = array_merge($rules, [ "content.$i.content.$iptName.value" => [ 'sometimes', 'image', 'mimes:png,jpg,svg', sprintf( 'dimensions:%s=%d,%s=%d', $max ? 'max_height' : 'height', $dim['hauteur'], $max ? 'max_width' : 'width', $dim['largeur'] ) ] ]); break; case InputType::imageArray()->value: $ipt = $this->engagement->content[$i]['content'][$iptName]; $max = isset($ipt['dimensionsMax']); $dim = collect(explode( ',', $ipt['dimensions'] ?? $ipt['dimensionsMax'] ))->mapWithKeys(function ($v) { $v = explode(' ', $v); return [$v[0] => $v[1]]; }); $rules = array_merge($rules, [ "content.$i.content.$iptName.value.*" => 'sometimes|array' ]); if (is_array($this->input("content.$i.content.$iptName.value.*"))) { $all = $this->all(); $images = $this->input("content.$i.content.$iptName.value"); if (is_array($images)) { ksort($images); $all['content'][$i]['content'][$iptName]['value'] = $images; $this->merge($all); } foreach ($this->input("content.$i.content.$iptName.value.*") as $imageK => $image) { if (!is_string($image)) { $rules = array_merge($rules, [ "content.$i.content.$iptName.value.$imageK" => [ 'image', 'mimes:png,jpg,svg', sprintf( 'dimensions:%s=%d,%s=%d', $max ? 'max_height' : 'height', $dim['hauteur'], $max ? 'max_width' : 'width', $dim['largeur'] ) ] ]); } else { $rules = array_merge($rules, [ "content.$i.content.$iptName.value.$imageK" => 'string' ]); } } } break; case InputType::url()->value: $rules = array_merge($rules, [ "content.$i.content.$iptName.value" => [ 'required', 'url', // @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed function ($attribute, $value, $fail) { try { Utils::assertNoInternalUrls($attribute, $value); } catch (ValidationException $e) { $fail(collect($e->errors())->first()[0]); } } ] ]); break; case InputType::labelArray()->value: $all = $this->all(); try { foreach ($this->input("content.$i.content.$iptName.value") as $labelIndex => $label) { if ($labelIndex === 'X') { unset($all['content'][$i]['content'][$iptName]['value'][$labelIndex]); continue; } $rules = array_merge($rules, [ "content.$i.content.$iptName.value.$labelIndex.*" => 'string' ]); } } catch (\Exception $e) { dd($this->input("content.$i.content.$iptName.value")); } $this->merge($all); break; case InputType::boolean()->value: $all = $this->all(); $nested = Utils::dotStringToNestedArray( "content.$i.content.$iptName.value", $this->input("content.$i.content.$iptName.value") ? true : false ); $all = array_merge_recursive_distinct($all, $nested); $this->merge($all); $rules = array_merge($rules, [ "content.$i.content.$iptName.value" => 'boolean' ]); break; } } } } return $rules; } }