PHP 8.0 support
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\DataTransferObjects;
|
||||
|
||||
use App\DataTransferObjects\Support\Concerns\BelongsToUser;
|
||||
use App\DataTransferObjects\Support\Data;
|
||||
use App\DataTransferObjects\Support\Rules\CustomRule;
|
||||
use App\DataTransferObjects\Support\WithUser;
|
||||
use App\Models\Server;
|
||||
use App\Models\Site;
|
||||
@@ -13,7 +14,6 @@ use App\Rules\ValidateMaximumSites;
|
||||
use Illuminate\Support\Arr;
|
||||
use Spatie\LaravelData\Attributes\Validation\Exists;
|
||||
use Spatie\LaravelData\Attributes\Validation\IntegerType;
|
||||
use Spatie\LaravelData\Attributes\Validation\Rule;
|
||||
use Spatie\LaravelData\Attributes\Validation\StringType;
|
||||
|
||||
class SiteData extends Data
|
||||
@@ -25,7 +25,7 @@ class SiteData extends Data
|
||||
public ?string $status = null,
|
||||
#[Exists( Server::class, 'id' ), IntegerType]
|
||||
public ?int $server_id = null,
|
||||
#[StringType, Rule( [new Hostname(), new ValidateMaximumSites()] )]
|
||||
#[StringType, CustomRule(Hostname::class, ValidateMaximumSites::class)]
|
||||
public ?string $domain = null,
|
||||
#[Exists(User::class, 'id'), IntegerType]
|
||||
public ?int $user_id = null,
|
||||
|
||||
24
app/DataTransferObjects/Support/Rules/CustomRule.php
Normal file
24
app/DataTransferObjects/Support/Rules/CustomRule.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\DataTransferObjects\Support\Rules;
|
||||
|
||||
use Attribute;
|
||||
use Spatie\LaravelData\Attributes\Validation\ValidationAttribute;
|
||||
|
||||
#[Attribute( Attribute::TARGET_PROPERTY )]
|
||||
class CustomRule extends ValidationAttribute
|
||||
{
|
||||
protected array $rules = [];
|
||||
|
||||
public function __construct(...$rules)
|
||||
{
|
||||
$this->rules = $rules;
|
||||
}
|
||||
|
||||
public function getRules(): array
|
||||
{
|
||||
return collect($this->rules)
|
||||
->map(fn (string $rule) => new $rule())
|
||||
->all();
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ class SiteController extends Controller
|
||||
'domain' => ['required'],
|
||||
'user_id' => ['required'],
|
||||
]);
|
||||
|
||||
|
||||
$site = app(CreateSiteAction::class)->execute(
|
||||
SiteData::validate($data)
|
||||
);
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace App\Rules;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Illuminate\Contracts\Validation\DataAwareRule;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class ValidateMaximumSites implements Rule, DataAwareRule
|
||||
{
|
||||
|
||||
@@ -97,7 +97,7 @@ it('can create a site', function () {
|
||||
setting(['receive_email_on_site_creation' => true]);
|
||||
App::forgetInstance('settings');
|
||||
|
||||
$user = User::factory()->withPackage()->create();
|
||||
$user = User::factory()->create();
|
||||
|
||||
$server = Server::factory()
|
||||
->ploiId('12345')
|
||||
@@ -158,3 +158,15 @@ it('requires the user_id, server_id and domain', function () {
|
||||
->assertInvalid('server_id')
|
||||
->assertInvalid('domain');
|
||||
});
|
||||
|
||||
it('requires the domain to be a valid hostname', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
api()
|
||||
->post(route('api.site.store'), [
|
||||
'domain' => 'hello there',
|
||||
'user_id' => 1,
|
||||
'server_id' => 1,
|
||||
])
|
||||
->assertInvalid(['domain', 'server_id']);
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ it('can create a new site', function () {
|
||||
App::forgetInstance('settings');
|
||||
|
||||
actingAs(
|
||||
$user = User::factory()->withPackage()->create()
|
||||
$user = User::factory()->create()
|
||||
);
|
||||
|
||||
$server = Server::factory()
|
||||
|
||||
Reference in New Issue
Block a user