-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support `Stringable` as rule entries * Implement `FileNode` * Docs for `FileNode`
- Loading branch information
1 parent
7c76a65
commit 99995aa
Showing
12 changed files
with
629 additions
and
8 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,37 @@ | ||
## File Upload Validation - Images | ||
|
||
Here are a few examples for how you can validate image uploads. | ||
|
||
### Validate dimensions | ||
|
||
```php | ||
|
||
$builder = Hyrule::create() | ||
->file('avatar') | ||
->required() | ||
->image() // Validates that upload is an image. | ||
->dimensions() // Starts dimension constraints... | ||
->ratio(1) | ||
->maxWidth(1000) | ||
->end() // Ends dimension rule-set. | ||
->end() // Ends the "avatar" field. | ||
// ... | ||
|
||
``` | ||
|
||
See [`Dimensions`](https://github.com/laravel/framework/blob/9.x/src/Illuminate/Validation/Rules/Dimensions.php) class for all available constraints. | ||
|
||
### Only accept subset of image types | ||
|
||
```php | ||
$builder = Hyrule::create() | ||
->file('avatar') | ||
->required() | ||
->mimeType() // Starts MIME-type constriants... | ||
->image('jpeg', 'gif', 'png') // Only accept image/{jpeg,gif,png} | ||
->end() // End MIME-Type constraints. | ||
->end() // End the "avatar" field. | ||
// ... | ||
``` | ||
|
||
See [File Upload Validation - MIME Types](./file-upload-validation-mime-types.md) for a comprehensive guide on MIME-Type rules. |
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,41 @@ | ||
## File Upload Validation - MIME Types | ||
|
||
It is considered best practice to validate the MIME type of uploaded files. Here are a few examples on how to do that with Hyrule: | ||
|
||
```php | ||
$builder = Hyrule::create() | ||
->file('attachment') | ||
->mimeType() // Starts MIME-Type contraints | ||
/* | ||
* All 5 top-level MIME type categories are supported | ||
*/ | ||
->application('pdf') // Allows application/pdf | ||
->image('jpg', 'png', ...) // Variadic. Enumerate sub-types e.g. image/jpeg, image/png, etc. | ||
->video('mp4', 'webm') | ||
->multipart(...) | ||
->message(...) | ||
->end() // Ends MIME Type constraint. | ||
->end() // Ends "attachment" field | ||
// ... | ||
``` | ||
|
||
Use `->allow(...)` to enumerate specific specific MIME-types: | ||
|
||
```php | ||
$builder = Hyrule::create() | ||
->array('attachments') | ||
->between(1, 10) | ||
->each('file') | ||
->mimeType() | ||
->allow('application/pdf') | ||
->allow('image/jpg') | ||
->allow('image/png') | ||
->allow('image/svg') | ||
->allow('video/mp4') | ||
// etc. | ||
->end() | ||
->end() | ||
->end() | ||
// ... | ||
|
||
``` |
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
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,48 @@ | ||
<?php | ||
|
||
namespace Square\Hyrule\Nodes; | ||
|
||
use Square\Hyrule\Rules\Dimensions; | ||
use Square\Hyrule\Rules\MIMEType; | ||
|
||
class FileNode extends AbstractNode | ||
{ | ||
protected ?Dimensions $dimensions; | ||
|
||
protected ?MIMEType $mimeType; | ||
|
||
/** | ||
* @return Dimensions | ||
*/ | ||
public function dimensions(): Dimensions | ||
{ | ||
if (!isset($this->dimensions)) { | ||
// Create a new Dimensions rule object, push it to the rules array, and | ||
// keep a reference so we can modify it when we need to in the future. | ||
$this->rule($this->dimensions = new Dimensions($this)); | ||
} | ||
return $this->dimensions; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function image(): FileNode | ||
{ | ||
$this->rule('image'); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return MIMEType | ||
*/ | ||
public function mimeType(): MIMEType | ||
{ | ||
if (!isset($this->mimeType)) { | ||
// Create a new MIMEType rule object, push it to the rules array, and | ||
// keep a reference so we can modify it when we need to in the future. | ||
$this->rule($this->mimeType = new MIMEType($this)); | ||
} | ||
return $this->mimeType; | ||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
|
||
namespace Square\Hyrule\Rules; | ||
|
||
use Illuminate\Validation\Rules\Dimensions as DimensionsRule; | ||
use Square\Hyrule\Nodes\FileNode; | ||
|
||
/** | ||
* Extends the Laravel's built-in Dimensions helper and adds methods to support Hyrule's fluent API. | ||
*/ | ||
class Dimensions extends DimensionsRule | ||
{ | ||
private FileNode $node; | ||
|
||
/** | ||
* @param FileNode $node | ||
* @param array<string,mixed> $constraints | ||
*/ | ||
public function __construct(FileNode $node, array $constraints = []) | ||
{ | ||
$this->node = $node; | ||
parent::__construct($constraints); | ||
} | ||
|
||
public function end(): FileNode | ||
{ | ||
return $this->node; | ||
} | ||
} |
Oops, something went wrong.