Skip to content

Commit

Permalink
Asset validation: piggyback on Laravel validation rules (#3922)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurperton authored Jun 30, 2021
1 parent 33a3ae5 commit 8296080
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 167 deletions.
60 changes: 60 additions & 0 deletions src/Fieldtypes/Assets/AssetRule.php
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;
}
}
23 changes: 12 additions & 11 deletions src/Fieldtypes/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,22 @@ public function fieldRules()
{
return collect(parent::fieldRules())->map(function ($rule) {
$name = Str::before($rule, ':');
$parameters = explode(',', Str::after($rule, ':'));

if ($name === 'image') {
return new ImageRule();
}

if ($name === 'mimes') {
return new MimesRule($parameters);
}
$parameters = Str::contains($rule, ':')
? explode(',', Str::after($rule, ':'))
: null;

if ($name === 'mimetypes') {
return new MimetypesRule($parameters);
if ($name === 'dimensions') {
$message = __('statamic::validation.dimensions');
} elseif ($name === 'image') {
$message = __('statamic::validation.image');
} elseif ($name === 'mimes') {
$message = str_replace(':values', join(', ', $parameters), __('statamic::validation.mimes'));
} elseif ($name === 'mimetypes') {
$message = str_replace(':values', join(', ', $parameters), __('statamic::validation.mimetypes'));
}

return $rule;
return $message ? new AssetRule($name, $message, $parameters) : $rule;
})->all();
}

Expand Down
51 changes: 0 additions & 51 deletions src/Fieldtypes/Assets/ImageRule.php

This file was deleted.

53 changes: 0 additions & 53 deletions src/Fieldtypes/Assets/MimesRule.php

This file was deleted.

48 changes: 0 additions & 48 deletions src/Fieldtypes/Assets/MimetypesRule.php

This file was deleted.

27 changes: 23 additions & 4 deletions tests/Fieldtypes/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use Statamic\Data\AugmentedCollection;
use Statamic\Facades\AssetContainer;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Assets\AssetRule;
use Statamic\Fieldtypes\Assets\Assets;
use Statamic\Fieldtypes\Assets\ImageRule;
use Statamic\Fieldtypes\Assets\MimesRule;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

Expand Down Expand Up @@ -87,14 +86,24 @@ public function it_shallow_augments_to_a_single_asset_when_max_files_is_one()
], $augmented->toArray());
}

/** @test */
public function it_replaces_dimensions_rule()
{
$replaced = $this->fieldtype(['validate' => ['dimensions:width=180,height=180']])->fieldRules();

$this->assertIsArray($replaced);
$this->assertCount(1, $replaced);
$this->assertInstanceOf(AssetRule::class, $replaced[0]);
}

/** @test */
public function it_replaces_image_rule()
{
$replaced = $this->fieldtype(['validate' => ['image']])->fieldRules();

$this->assertIsArray($replaced);
$this->assertCount(1, $replaced);
$this->assertInstanceOf(ImageRule::class, $replaced[0]);
$this->assertInstanceOf(AssetRule::class, $replaced[0]);
}

/** @test */
Expand All @@ -104,7 +113,17 @@ public function it_replaces_mimes_rule()

$this->assertIsArray($replaced);
$this->assertCount(1, $replaced);
$this->assertInstanceOf(MimesRule::class, $replaced[0]);
$this->assertInstanceOf(AssetRule::class, $replaced[0]);
}

/** @test */
public function it_replaces_mimestypes_rule()
{
$replaced = $this->fieldtype(['validate' => ['mimetypes:image/jpg,image/png']])->fieldRules();

$this->assertIsArray($replaced);
$this->assertCount(1, $replaced);
$this->assertInstanceOf(AssetRule::class, $replaced[0]);
}

public function fieldtype($config = [])
Expand Down

0 comments on commit 8296080

Please sign in to comment.