-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Asset validation: piggyback on Laravel validation rules (#3922)
- Loading branch information
1 parent
33a3ae5
commit 8296080
Showing
6 changed files
with
95 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Statamic\Fieldtypes\Assets; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Validation\Concerns\ValidatesAttributes; | ||
use Statamic\Facades\Asset; | ||
use Statamic\Support\Str; | ||
use Symfony\Component\HttpFoundation\File\File; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
class AssetRule implements Rule | ||
{ | ||
use ValidatesAttributes; | ||
|
||
protected $name; | ||
protected $parameters; | ||
protected $message; | ||
|
||
public function __construct($name, $message, $parameters = null) | ||
{ | ||
$this->name = $name; | ||
$this->message = $message; | ||
$this->parameters = $parameters; | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
$method = 'validate'.Str::studly($this->name); | ||
|
||
return collect($value)->every(function ($file) use ($attribute, $method) { | ||
if (! ($file instanceof UploadedFile)) { | ||
if (! $asset = Asset::find($file)) { | ||
return false; | ||
} | ||
|
||
$file = new File($asset->resolvedPath()); | ||
} | ||
|
||
return $this->{$method}($attribute, $file, $this->parameters); | ||
}); | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message() | ||
{ | ||
return $this->message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters