[ 'required', function (string $attribute, $value, callable $fail) { return $this->validatePicture($attribute, $value, $fail); } ] ]; } /** * Images rules * * @return array */ public function pictureRules(): array { return [ 'pictures' => 'sometimes|array', 'pictureValidate' => 'required_with:pictures|array', 'pictureValidate.*' => [ 'required', function (string $attribute, $value, callable $fail) { return $this->validatePicture($attribute, $value, $fail); } ] ]; } /** * Validate picture * * @param string $attribute * @param mixed $value * @param callback $fail * @return void * @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed */ private function validatePicture(string $attribute, $value, callable $fail) { if (!File::exists($value)) { $fail(trans(":attribute n'existe pas dans le système de fichier")); return; } if (!\in_array(File::mimeType($value), ['image/jpeg', 'image/jpg', 'image/png'])) { $fail(trans(":attribute doit être une image de type 'image/jpeg,image/jpg,image/png'")); } if (!($dimensions = \getimagesize($value)) or !isset($dimensions[0]) or !isset($dimensions[1])) { $fail(trans(":attribute impossible de valider les dimensions")); } else { $minWith = 400; $maxWith = 1800; $minHeight = 300; $maxHeight = 1800; $width = intval($dimensions[0]); $height = intval($dimensions[1]); if (($width < $minWith) or ($width > $maxWith)) { $fail(trans(":attribute la largeur doit être comprise entre $minWith et $maxWith")); } if (($height < $minHeight) or ($height > $maxHeight)) { $fail(trans(":attribute la hauteur doit être comprise entre $minHeight et $maxHeight")); } } } /** * Merges products pictures fields with request data * * @return void */ protected function mergePicture(): void { $picturePath = $this->picture; if (is_string($picturePath)) { $this->merge(['pictureValidate' => []]); // ! Empty picture. if ($picturePath === 'false') { // * Empty picture and exit $this->merge(['pictures' => []]); return; } $decodedPath = \urldecode($picturePath); $this->merge([ 'pictureValidate' => array_merge( $this->pictureValidate, [\public_path(ltrim($decodedPath, '/'))] ) ]); $changedPicture = $this->picture; $changedPicture = ltrim($decodedPath, '/'); $this->merge([ 'picture' => $changedPicture ]); } //end if } /** * Merges products pictures fields with request data * * @return void */ protected function mergePictures(): void { $pictures = $this->pictures; if (is_array($pictures)) { $this->merge(['pictureValidate' => []]); foreach ($pictures as $key => $picturePath) { // ! Empty picture. if ($picturePath === 'false') { // * Empty picture and exit $this->merge(['pictures' => []]); return; } $decodedPath = \urldecode($picturePath); $this->merge([ 'pictureValidate' => array_merge( $this->pictureValidate, [\public_path(ltrim($decodedPath, '/'))] ) ]); $changedPicture = $this->pictures; $changedPicture[$key] = ltrim($decodedPath, '/'); $this->merge([ 'pictures' => $changedPicture ]); } } //end if } }