♻️ Replace App\Model with App\Data
This commit is contained in:
parent
70e4404c22
commit
7f4f005302
12 changed files with 393 additions and 222 deletions
19
app/Data/Casts/File.php
Normal file
19
app/Data/Casts/File.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Data\Casts;
|
||||
|
||||
use App\Exceptions\Quit;
|
||||
use Spatie\LaravelData\Casts\Cast;
|
||||
use Spatie\LaravelData\Support\DataProperty;
|
||||
|
||||
class File implements Cast
|
||||
{
|
||||
public function cast(DataProperty $property, mixed $file, array $context): mixed
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
throw new Quit("$file is not a valid file");
|
||||
}
|
||||
|
||||
return realpath($file);
|
||||
}
|
||||
}
|
||||
19
app/Data/Casts/Folder.php
Normal file
19
app/Data/Casts/Folder.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Data\Casts;
|
||||
|
||||
use App\Exceptions\Quit;
|
||||
use Spatie\LaravelData\Casts\Cast;
|
||||
use Spatie\LaravelData\Support\DataProperty;
|
||||
|
||||
class Folder implements Cast
|
||||
{
|
||||
public function cast(DataProperty $property, mixed $file, array $context): mixed
|
||||
{
|
||||
if (!is_dir($file)) {
|
||||
throw new Quit("$file is not a valid folder");
|
||||
}
|
||||
|
||||
return realpath($file);
|
||||
}
|
||||
}
|
||||
48
app/Data/Data.php
Normal file
48
app/Data/Data.php
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Data;
|
||||
|
||||
use Spatie\LaravelData\Data as BaseData;
|
||||
use Spatie\LaravelData\Support\DataConfig;
|
||||
use Spatie\LaravelData\Support\DataProperty;
|
||||
use Spatie\LaravelData\DataPipeline;
|
||||
use Spatie\LaravelData\DataPipes\AuthorizedDataPipe;
|
||||
use Spatie\LaravelData\DataPipes\CastPropertiesDataPipe;
|
||||
use Spatie\LaravelData\DataPipes\MapPropertiesDataPipe;
|
||||
use Spatie\LaravelData\DataPipes\ValidatePropertiesDataPipe;
|
||||
|
||||
abstract class Data extends BaseData
|
||||
{
|
||||
public static function fillPipeline(): DataPipeline
|
||||
{
|
||||
return DataPipeline::create()
|
||||
->into(static::class)
|
||||
->through(AuthorizedDataPipe::class)
|
||||
->through(MapPropertiesDataPipe::class)
|
||||
->through(ValidatePropertiesDataPipe::class)
|
||||
->through(CastPropertiesDataPipe::class);
|
||||
}
|
||||
|
||||
public function fill(iterable $payload): static
|
||||
{
|
||||
$pipeline = static::fillPipeline();
|
||||
|
||||
foreach (static::normalizers() as $normalizer) {
|
||||
$pipeline->normalizer($normalizer);
|
||||
}
|
||||
|
||||
$properties = $pipeline->using($payload)->execute();
|
||||
$dataClass = app(DataConfig::class)->getDataClass(static::class);
|
||||
|
||||
$dataClass
|
||||
->properties
|
||||
->filter(
|
||||
fn (DataProperty $property) => $properties->has($property->name)
|
||||
)
|
||||
->each(
|
||||
fn (DataProperty $property) => $this->{$property->name} = $properties->get($property->name)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
16
app/Data/FileData.php
Normal file
16
app/Data/FileData.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Data;
|
||||
|
||||
use Spatie\LaravelData\Attributes\WithCast;
|
||||
|
||||
class FileData extends Data
|
||||
{
|
||||
#[WithCast(Casts\File::class)]
|
||||
public string $path;
|
||||
|
||||
public static function fromString(string $path): static
|
||||
{
|
||||
return static::from(['path' => $path]);
|
||||
}
|
||||
}
|
||||
42
app/Data/InputData.php
Normal file
42
app/Data/InputData.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Data;
|
||||
|
||||
use App\Exceptions\Quit;
|
||||
use Spatie\LaravelData\Attributes\DataCollectionOf;
|
||||
use Spatie\LaravelData\Attributes\MapInputName;
|
||||
use Spatie\LaravelData\DataCollection;
|
||||
use Spatie\LaravelData\Attributes\WithCast;
|
||||
|
||||
class InputData extends Data
|
||||
{
|
||||
#[DataCollectionOf(FileData::class)]
|
||||
#[MapInputName('input')]
|
||||
public DataCollection $files;
|
||||
public ?string $url = null;
|
||||
#[WithCast(Casts\Folder::class)]
|
||||
public ?string $destination = null;
|
||||
public ?string $title = null;
|
||||
public bool $sync = false;
|
||||
|
||||
public function assertValid(): self
|
||||
{
|
||||
throw_if(
|
||||
!$this->hasFiles() && !$this->hasURL(),
|
||||
Quit::class,
|
||||
'Must have either a valid file or URL'
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasURL(): bool
|
||||
{
|
||||
return !empty($this->url);
|
||||
}
|
||||
|
||||
public function hasFiles(): bool
|
||||
{
|
||||
return !empty($this->files) && !!$this->files->count();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue