minWith = $minWith ?? $this->minWith; $this->minHeight = $minHeight ?? $this->minHeight; $this->maxWith = $maxWith ?? $this->maxWith; $this->maxHeight = $maxHeight ?? $this->maxHeight; return [ 'picture' => [ 'required', function (string $attribute, $value, callable $fail) { return $this->validatePicture($attribute, $value, $fail); } ] ]; } /** * Images rules * * @param integer|null $minWith * @param integer|null $minHeight * @param integer|null $maxWith * @param integer|null $maxHeight * @return array */ public function picturesRules( ?int $minWith = null, ?int $minHeight = null, ?int $maxWith = null, ?int $maxHeight = null ): array { $this->minWith = $minWith ?? $this->minWith; $this->minHeight = $minHeight ?? $this->minHeight; $this->maxWith = $maxWith ?? $this->maxWith; $this->maxHeight = $maxHeight ?? $this->maxHeight; 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', 'image/gif'])) { $fail(trans(":attribute doit être une image de type 'image/jpeg,image/jpg,image/png,image/gif'")); } if (!($dimensions = \getimagesize($value)) or !isset($dimensions[0]) or !isset($dimensions[1])) { $fail(trans(":attribute impossible de valider les dimensions")); } else { $width = intval($dimensions[0]); $height = intval($dimensions[1]); $xMessage = trans( ":attribute la largeur doit être comprise entre {$this->minWith}px et {$this->maxWith}px" ); if ($this->minWith === $this->maxWith) { $xMessage = trans(":attribute la largeur doit être de {$this->minWith}px"); } $yMessage = trans( ":attribute la hauteur doit être comprise entre {$this->minWith}px et {$this->maxWith}px" ); if ($this->minHeight === $this->maxHeight) { $yMessage = trans(":attribute la hauteur doit être de {$this->minWith}px"); } if (($width < $this->minWith) or ($width > $this->maxWith)) { $fail($xMessage); } if (($height < $this->minHeight) or ($height > $this->maxHeight)) { $fail($yMessage); } } //end if } /** * 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 = 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 } }