$minWith ?? $this->minWith, 'minHeight' => $minHeight ?? $this->minHeight, 'maxWith' => $maxWith ?? $this->maxWith, 'maxHeight' => $maxHeight ?? $this->maxHeight ]; return [ $name => [ !$nullable ? 'required' : 'nullable', function (string $attribute, $value, callable $fail) use ($sizes) { return ToolboxHelper::validatePicture($attribute, $value, $fail, $sizes); } ] ]; } /** * Images rules * * @param string $name * @param integer|null $minWith * @param integer|null $minHeight * @param integer|null $maxWith * @param integer|null $maxHeight * @return array */ public function picturesRules( string $name = 'pictures', ?int $minWith = null, ?int $minHeight = null, ?int $maxWith = null, ?int $maxHeight = null ): array { $sizes = (object)[ 'minWith' => $minWith ?? $this->minWith, 'minHeight' => $minHeight ?? $this->minHeight, 'maxWith' => $maxWith ?? $this->maxWith, 'maxHeight' => $maxHeight ?? $this->maxHeight ]; return [ $name => 'sometimes|array', "{$name}Validate" => "required_with:{$name}|array", "{$name}Validate.*" => [ 'required', function (string $attribute, $value, callable $fail) use ($sizes) { return $this->validatePicture($attribute, $value, $fail, $sizes); } ] ]; } /** * Merges products pictures fields with request data * * @param string $name * @return void */ protected function mergePicture(string $name = 'picture'): void { $picturePath = $this->{$name}; if (is_string($picturePath)) { $this->merge(["{$name}Validate" => []]); // ! Empty picture. if ($picturePath === 'false') { // * Empty picture and exit $this->merge([$name => []]); return; } $decodedPath = \urldecode($picturePath); $this->merge([ "{$name}Validate" => array_merge( $this->{"{$name}Validate"}, [\public_path(ltrim($decodedPath, '/'))] ) ]); $changedPicture = ltrim($decodedPath, '/'); $this->merge([ $name => $changedPicture ]); } //end if } /** * Merges products pictures fields with request data * * @param string $name * @return void */ protected function mergePictures(string $name = 'pictures'): void { $pictures = $this->{$name}; if (is_array($pictures)) { $this->merge(["{$name}Validate" => []]); foreach ($pictures as $key => $picturePath) { // ! Empty picture. if ($picturePath === 'false') { // * Empty picture and exit $this->merge([$name => []]); return; } $decodedPath = \urldecode($picturePath); $this->merge([ "{$name}Validate" => array_merge( $this->{"{$name}Validate"}, [\public_path(ltrim($decodedPath, '/'))] ) ]); $changedPicture = $this->{$name}; $changedPicture[$key] = ltrim($decodedPath, '/'); $this->merge([ $name => $changedPicture ]); } } //end if } }