generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
8 changed files
with
92 additions
and
104 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
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
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,30 @@ | ||
<?php | ||
|
||
namespace Sowren\SvgAvatarGenerator\Validators; | ||
|
||
use Arr; | ||
use Str; | ||
use Validator; | ||
|
||
class ConfigValidator | ||
{ | ||
protected static array $rules = [ | ||
'corner_radius' => ['int', 'between:0,25'], | ||
'font_size' => ['int', 'between:10,50'], | ||
'gradiant_rotation' => ['int', 'between:0,360'], | ||
'gradiant_stops' => ['array'], | ||
'gradiant_stops.*' => ['numeric', 'between:0,1'], | ||
'svg_size' => ['int', 'between:16,512'], | ||
'custom_font_url' => ['nullable', 'url'], | ||
'svg_text' => ['required', 'string'] | ||
]; | ||
|
||
public static function validate(string $key, mixed $value): array | ||
{ | ||
$rules = Arr::where(static::$rules, function ($set, $property) use ($key) { | ||
return Str::startsWith($property, $key); | ||
}); | ||
|
||
return Validator::validate([$key => $value], $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 |
---|---|---|
@@ -1,59 +1,54 @@ | ||
<?php | ||
|
||
use Sowren\SvgAvatarGenerator\Exceptions\InvalidFontSizeException; | ||
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientRotationException; | ||
use Sowren\SvgAvatarGenerator\Exceptions\InvalidGradientStopException; | ||
use Sowren\SvgAvatarGenerator\Exceptions\InvalidSvgSizeException; | ||
use Sowren\SvgAvatarGenerator\Exceptions\InvalidUrlException; | ||
use Sowren\SvgAvatarGenerator\Exceptions\MissingTextException; | ||
use Illuminate\Validation\ValidationException; | ||
use Sowren\SvgAvatarGenerator\SvgAvatarGenerator; | ||
|
||
it('will throw exception if svg size less than minimum is provided', function () { | ||
config(['svg-avatar.size' => 8]); | ||
new SvgAvatarGenerator('Rob Stark'); | ||
})->throws(InvalidSvgSizeException::class); | ||
})->throws(ValidationException::class, 'The svg size field must be between 16 and 512.'); | ||
|
||
it('will throw exception if svg size greater than maximum is provided', function () { | ||
config(['svg-avatar.size' => 1024]); | ||
new SvgAvatarGenerator('Jon Snow'); | ||
})->throws(InvalidSvgSizeException::class); | ||
})->throws(ValidationException::class, 'The svg size field must be between 16 and 512.'); | ||
|
||
it('will throw exception if font size less than minimum is provided', function () { | ||
config(['svg-avatar.font_size' => 9]); | ||
new SvgAvatarGenerator('Sansa Stark'); | ||
})->throws(InvalidFontSizeException::class); | ||
})->throws(ValidationException::class, 'The font size field must be between 10 and 50.'); | ||
|
||
it('will throw exception if font size greater than maximum is provided', function () { | ||
config(['svg-avatar.font_size' => 51]); | ||
new SvgAvatarGenerator('Arya Stark'); | ||
})->throws(InvalidFontSizeException::class); | ||
})->throws(ValidationException::class, 'The font size field must be between 10 and 50.'); | ||
|
||
it('will throw exception if gradient rotation less than minimum is provided', function () { | ||
config(['svg-avatar.gradient_rotation' => -1]); | ||
new SvgAvatarGenerator('Brandon Stark'); | ||
})->throws(InvalidGradientRotationException::class); | ||
})->throws(ValidationException::class, 'The gradiant rotation field must be between 0 and 360.'); | ||
|
||
it('will throw exception if gradient rotation greater than maximum is provided', function () { | ||
config(['svg-avatar.gradient_rotation' => 361]); | ||
new SvgAvatarGenerator('Rickon Stark'); | ||
})->throws(InvalidGradientRotationException::class); | ||
})->throws(ValidationException::class, 'The gradiant rotation field must be between 0 and 360.'); | ||
|
||
it('will throw missing text exception if text is not set', function () { | ||
$generator = new SvgAvatarGenerator(); | ||
$generator->getInitials(); | ||
})->throws(MissingTextException::class); | ||
})->throws(ValidationException::class, 'The svg text field is required.'); | ||
|
||
it('will throw exception if gradient stop less than minimum is provided', function () { | ||
config(['svg-avatar.gradient_stops' => [-1, 1]]); | ||
config(['svg-avatar.gradient_stops' => [-1, 0.5]]); | ||
new SvgAvatarGenerator('Lyanna Stark'); | ||
})->throws(InvalidGradientStopException::class); | ||
})->throws(ValidationException::class, 'The gradiant_stops.0 field must be between 0 and 1.'); | ||
|
||
it('will throw exception if gradient stop greater than maximum is provided', function () { | ||
config(['svg-avatar.gradient_stops' => [0, 2]]); | ||
config(['svg-avatar.gradient_stops' => [0.2, 2]]); | ||
new SvgAvatarGenerator('Benjen Stark'); | ||
})->throws(InvalidGradientStopException::class); | ||
})->throws(ValidationException::class, 'The gradiant_stops.1 field must be between 0 and 1.'); | ||
|
||
it('will throw exception if invalid font url is provided', function () { | ||
config(['svg-avatar.custom_font_url' => 'invalid_url']); | ||
new SvgAvatarGenerator('Rickard Stark'); | ||
})->throws(InvalidUrlException::class); | ||
})->throws(ValidationException::class, 'The custom font url field must be a valid URL.'); |
Oops, something went wrong.