This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alphametric
committed
Apr 2, 2019
1 parent
a3cab43
commit 83f6b31
Showing
5 changed files
with
148 additions
and
0 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
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,84 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules; | ||
|
||
// Using directives | ||
use Illuminate\Http\UploadedFile; | ||
|
||
// Encoded image rule | ||
class EncodedImage extends Rule | ||
{ | ||
|
||
/** | ||
* Pointer to the temporary file. | ||
* | ||
**/ | ||
protected $file; | ||
|
||
|
||
|
||
/** | ||
* Write the given data to a temporary file. | ||
* | ||
* @param string $data. | ||
* @return UploadedFile. | ||
* | ||
**/ | ||
protected function createTemporaryFile($data) | ||
{ | ||
$this->file = tmpfile(); | ||
|
||
fwrite($this->file, base64_decode(str_after($data, 'base64,'))); | ||
|
||
return new UploadedFile( | ||
stream_get_meta_data($this->file)['uri'], 'image', | ||
'text/plain', null, null, true | ||
); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* The rule requires a single parameter, which is | ||
* the expected mime type of the file e.g. png, jpeg etc. | ||
* | ||
* @param string $attribute. | ||
* @param mixed $value. | ||
* @return bool. | ||
* | ||
**/ | ||
public function passes($attribute, $value) | ||
{ | ||
if (! starts_with($value, "data:image/{$this->parameters[0]};base64,")) { | ||
return false; | ||
} | ||
|
||
$result = validator(['file' => $this -> createTemporaryFile($value)], ['file' => 'image']) | ||
-> passes(); | ||
|
||
fclose($this->file); | ||
|
||
return $result; | ||
} | ||
|
||
|
||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @param none. | ||
* @return string. | ||
* | ||
**/ | ||
public function message() | ||
{ | ||
return Helper::getLocalizedErrorMessage( | ||
'encoded_image', | ||
"The :attribute must be a valid {$this->parameters[0]} image" | ||
); | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,63 @@ | ||
<?php | ||
|
||
// Namespace | ||
namespace Alphametric\Validation\Rules\Tests; | ||
|
||
// Using directives | ||
use Alphametric\Validation\Rules\EncodedImage; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
// Encoded image test | ||
class EncodedImageTest extends Orchestra | ||
{ | ||
|
||
/** | ||
* Retrieve the contents of a given file. | ||
* | ||
* @param string $file_name. | ||
* @return string. | ||
* | ||
**/ | ||
protected function getFile($file_name) | ||
{ | ||
$path = realpath(__DIR__ . '/..') . "/support/assets/$file_name"; | ||
|
||
$extension = pathinfo($path, PATHINFO_EXTENSION); | ||
|
||
return "data:image/{$extension};base64," . base64_encode(file_get_contents($path)); | ||
} | ||
|
||
|
||
|
||
/** | ||
* Generate an invalid image file with a given mime type. | ||
* | ||
* @param string $mime. | ||
* @return string. | ||
* | ||
**/ | ||
protected function invalidImage($mime = "") | ||
{ | ||
return "data:image/{$mime};base64," . base64_encode("not an image file"); | ||
} | ||
|
||
|
||
|
||
/** @test */ | ||
public function a_encoded_jpeg_image_can_be_validated() | ||
{ | ||
// Define the validation rule | ||
$png_rule = ['image' => [new EncodedImage('png')]]; | ||
$jpeg_rule = ['image' => [new EncodedImage('jpeg')]]; | ||
|
||
// Execute the tests | ||
$this->assertFalse(validator(['image' => $this -> getFile('image.jpeg')], $png_rule)->passes()); | ||
$this->assertTrue(validator(['image' => $this -> getFile('image.png')], $png_rule)->passes()); | ||
$this->assertTrue(validator(['image' => $this -> getFile('image.jpeg')], $jpeg_rule)->passes()); | ||
$this->assertFalse(validator(['image' => $this -> getFile('image.png')], $jpeg_rule)->passes()); | ||
$this->assertFalse(validator(['image' => $this -> invalidImage('image.jpeg')], $jpeg_rule)->passes()); | ||
$this->assertFalse(validator(['image' => $this -> invalidImage('image.png')], $jpeg_rule)->passes()); | ||
$this->assertFalse(validator(['image' => $this -> invalidImage()], $jpeg_rule)->passes()); | ||
} | ||
|
||
} |