diff --git a/src/Rules/StartsWithMimeType.php b/src/Rules/BaseTypeRule.php similarity index 60% rename from src/Rules/StartsWithMimeType.php rename to src/Rules/BaseTypeRule.php index fa22045..24def03 100644 --- a/src/Rules/StartsWithMimeType.php +++ b/src/Rules/BaseTypeRule.php @@ -4,25 +4,25 @@ use Owowagency\LaravelMedia\Rules\Concerns\ValidatesBase64; -class StartsWithMimeType extends IsBase64 +abstract class BaseTypeRule extends IsBase64 { use ValidatesBase64; /** - * The mime type to validate. + * The type to validate. * * @var string */ - protected $mimeType; + protected $type; /** * StartsWithMimeType constructor. * - * @param string $mimeType + * @param string $type */ - public function __construct(string $mimeType) + public function __construct(string $type) { - $this->mimeType = $mimeType; + $this->type = $type; } /** @@ -38,23 +38,16 @@ public function passes($attribute, $value) return false; } - return $this->startsWithMimeType($value); + return $this->validateType($this->getMimeType($value)); } /** - * Checks if base64 is image. + * Checks if base64 has valid type. * - * @param string $value + * @param string $mimeType * @return bool */ - protected function startsWithMimeType(string $value): bool - { - $mimeType = $this->getMimeType($value); - - $exploded = explode('/', $mimeType); - - return ($exploded[0] == $this->mimeType); - } + abstract protected function validateType(string $mimeType): bool; /** * Get the validation error message. @@ -63,6 +56,6 @@ protected function startsWithMimeType(string $value): bool */ public function message(): string { - return trans('validation.custom.is_base_64_type', ['type' => $this->mimeType]); + return trans('validation.custom.is_base_64_type', ['type' => $this->type]); } } diff --git a/src/Rules/IsBase64Audio.php b/src/Rules/IsBase64Audio.php deleted file mode 100644 index d5c3bdd..0000000 --- a/src/Rules/IsBase64Audio.php +++ /dev/null @@ -1,28 +0,0 @@ -type; + } +} diff --git a/src/Rules/IsBase64Subtype.php b/src/Rules/IsBase64Subtype.php new file mode 100644 index 0000000..26f6e8f --- /dev/null +++ b/src/Rules/IsBase64Subtype.php @@ -0,0 +1,19 @@ +type; + } +} diff --git a/src/Rules/IsBase64Type.php b/src/Rules/IsBase64Type.php index 04c70c2..6e580ef 100644 --- a/src/Rules/IsBase64Type.php +++ b/src/Rules/IsBase64Type.php @@ -2,54 +2,18 @@ namespace Owowagency\LaravelMedia\Rules; -use Illuminate\Support\Arr; -use Owowagency\LaravelMedia\Rules\Concerns\ValidatesBase64; - -class IsBase64Type extends IsBase64 +class IsBase64Type extends BaseTypeRule { - use ValidatesBase64; - - /** - * The base64 types to check in the test. - * - * @var array - */ - private $types; - - /** - * Create a notification instance. - * - * @param string|array $type - * @return void - */ - public function __construct($types) - { - $this->types = Arr::wrap($types); - } - /** - * Determine if the validation rule passes. + * Checks if base64 has a valid type. * - * @param string $attribute - * @param mixed $value + * @param string $mimeType * @return bool */ - public function passes($attribute, $value) + protected function validateType(string $mimeType): bool { - if (! $this->isBase64($value)) { - return false; - } - - return in_array($this->getMimeType($value), $this->types); - } + $exploded = explode('/', $mimeType); - /** - * Get the validation error message. - * - * @return string - */ - public function message(): string - { - return trans('validation.custom.is_base_64_type', ['type' => implode(',', $this->types)]); + return $exploded[0] == $this->type; } } diff --git a/tests/Unit/Rules/IsBase64AudioTest.php b/tests/Unit/Rules/IsBase64AudioTest.php index 000ab93..55237a6 100644 --- a/tests/Unit/Rules/IsBase64AudioTest.php +++ b/tests/Unit/Rules/IsBase64AudioTest.php @@ -3,14 +3,14 @@ namespace Owowagency\LaravelMedia\Tests\Unit\Rules; use Owowagency\LaravelMedia\Tests\TestCase; -use Owowagency\LaravelMedia\Rules\IsBase64Audio; +use Owowagency\LaravelMedia\Rules\IsBase64Type; class IsBase64AudioTest extends TestCase { /** - * The IsBase64Audio rule instance. + * The rule instance. * - * @var \Owowagency\LaravelMedia\Rules\IsBase64Audio + * @var \Owowagency\LaravelMedia\Rules\IsBase64Type */ private $rule; @@ -24,7 +24,7 @@ protected function setUp(): void parent::setup(); $this->base64 = file_get_contents(__DIR__ . '/../../Support/content/audio'); - $this->rule = new IsBase64Audio(); + $this->rule = new IsBase64Type('audio'); } /** @test */ diff --git a/tests/Unit/Rules/IsBase64MimeTypeTest.php b/tests/Unit/Rules/IsBase64MimeTypeTest.php new file mode 100644 index 0000000..82c8059 --- /dev/null +++ b/tests/Unit/Rules/IsBase64MimeTypeTest.php @@ -0,0 +1,59 @@ +rule = new IsBase64MimeType('image/png'); + } + + /** @test */ + public function passes_valid_mime_type() + { + $this->assertTrue($this->validate($this->base64)); + } + + /** @test */ + public function fails_invalid_mime_type() + { + $txt = 'SWsgaGViIGluIGVlbiBwbGFudGVuYmFrIGdla29zdHMK'; + + $this->assertFalse($this->validate($txt)); + } + + /** @test */ + public function fails_no_base64() + { + $this->assertFalse($this->validate('no_base_64')); + } + + /** + * Validates the rule. + * + * @param mixed $base64 + * @return bool + */ + private function validate($base64): bool + { + return $this->rule->passes('', $base64); + } +} diff --git a/tests/Unit/Rules/IsBase64ImageTest.php b/tests/Unit/Rules/IsBase64SubtypeTest.php similarity index 75% rename from tests/Unit/Rules/IsBase64ImageTest.php rename to tests/Unit/Rules/IsBase64SubtypeTest.php index 237c2c8..2d24720 100644 --- a/tests/Unit/Rules/IsBase64ImageTest.php +++ b/tests/Unit/Rules/IsBase64SubtypeTest.php @@ -1,16 +1,16 @@ rule = new IsBase64Image; + $this->rule = new IsBase64Subtype('png'); } /** @test */ diff --git a/tests/Unit/Rules/IsBase64TypeTest.php b/tests/Unit/Rules/IsBase64TypeTest.php index e392926..21fced1 100644 --- a/tests/Unit/Rules/IsBase64TypeTest.php +++ b/tests/Unit/Rules/IsBase64TypeTest.php @@ -30,11 +30,21 @@ protected function setUp(): void { parent::setup(); - $this->rule = new IsBase64Type(['text/css', 'text/plain']); + $this->rule = new IsBase64Type('text'); } /** @test */ - public function passes_text_css_or_text_plain_file() + public function passes_images() + { + $this->rule = new IsBase64Type('image'); + + $image = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk/g8AAQsBBD48D9kAAAAASUVORK5CYII='; + + $this->assertTrue($this->validate($image)); + } + + /** @test */ + public function passes_text_css_file() { $this->assertTrue($this->validate($this->base64)); } @@ -46,7 +56,7 @@ public function fails_no_base64() } /** @test */ - public function fails_no_text_css_or_text_plain_file() + public function fails_no_text_css_file() { $image = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk/g8AAQsBBD48D9kAAAAASUVORK5CYII=';