♻️ Replace App\Model with App\Data

This commit is contained in:
Dan Jones 2022-08-19 19:52:56 -05:00
commit 7f4f005302
12 changed files with 393 additions and 222 deletions

View file

@ -2,8 +2,8 @@
namespace App\Commands;
use App\Data\InputData;
use App\Exceptions\Quit;
use App\Models\Input;
use App\Services\ProcessInput;
use Symfony\Component\Console\Output\OutputInterface;
@ -31,8 +31,8 @@ class GetShow extends Command
return static::SUCCESS;
}
protected function getInput(): Input
protected function getInput(): InputData
{
return (new Input($this->options()))->assertValid();
return InputData::from($this->options())->assertValid();
}
}

19
app/Data/Casts/File.php Normal file
View 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
View 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
View 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
View 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
View 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();
}
}

View file

@ -1,55 +0,0 @@
<?php
namespace App\Models;
use App\Exceptions\Quit;
class Input extends Model
{
protected ?array $files = null;
protected ?string $url = null;
protected ?string $destination = null;
protected ?string $title = null;
protected bool $low = false;
protected bool $sync = false;
public function assertValid(): self
{
// @todo probably should use a validator for this
throw_if(
empty($this->files) && empty($this->url),
Quit::class,
'Must have either a valid file or URL'
);
return $this;
}
public function hasURL(): bool
{
return !empty($this->url);
}
public function setInput(?array $files): void
{
$this->setFiles($files);
}
public function setFiles(?array $files): void
{
$this->files = array_map(function (string $file) {
if (!file_exists($file)) {
throw new Quit("$file is not a valid file");
}
return realpath($file);
}, $files ?? []);
}
public function setDestination(?string $dir): void
{
throw_if(!is_dir($dir), Quit::class, "$dir is not a valid directory");
$this->destination = $dir;
// @todo get title from .nfo file, if available
}
}

View file

@ -1,57 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
abstract class Model implements Arrayable, JsonSerializable, Jsonable
{
public function __construct(array $data = [])
{
foreach ($data as $k => $v) {
$this->__set($k, $v);
}
}
public function __set(string $key, $value)
{
$setter = 'set' . Str::studly($key);
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (property_exists($this, $key)) {
$this->$key = $value;
}
}
public function __get(string $key)
{
$getter = 'get' . Str::studly($key);
if (method_exists($this, $getter)) {
return $this->$getter($this->$key ?? null);
} elseif (property_exists($this, $key)) {
return $this->$key;
}
throw new \OutOfBoundsException(sprintf(
'Undefined property %s::%s', static::class, $key
));
}
public function toArray()
{
return get_object_vars($this);
}
public function jsonSerialize()
{
return $this->toArray();
}
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options);
}
}

View file

@ -2,11 +2,11 @@
namespace App\Services;
use App\Models\Input;
use App\Data\InputData;
class ProcessInput
{
public function getJob(Input $input)
public function getJob(InputData $input)
{
return null;
}