♻️ 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;
}

View file

@ -21,7 +21,8 @@
"illuminate/log": "^9.0",
"illuminate/queue": "^9.2",
"laravel-zero/framework": "^9.0",
"nunomaduro/termwind": "^1.3"
"nunomaduro/termwind": "^1.3",
"spatie/laravel-data": "^2.0"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",

344
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c0c9ea11e0a1fedc54e0aba0db7a4a46",
"content-hash": "08da4b29527372e8ce8b67dc607498db",
"packages": [
{
"name": "brick/math",
@ -2314,6 +2314,109 @@
],
"time": "2022-02-24T17:35:42+00:00"
},
{
"name": "phpdocumentor/reflection-common",
"version": "2.2.0",
"source": {
"type": "git",
"url": "https://github.com/phpDocumentor/ReflectionCommon.git",
"reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b",
"reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-2.x": "2.x-dev"
}
},
"autoload": {
"psr-4": {
"phpDocumentor\\Reflection\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jaap van Otterdijk",
"email": "opensource@ijaap.nl"
}
],
"description": "Common reflection classes used by phpdocumentor to reflect the code structure",
"homepage": "http://www.phpdoc.org",
"keywords": [
"FQSEN",
"phpDocumentor",
"phpdoc",
"reflection",
"static analysis"
],
"support": {
"issues": "https://github.com/phpDocumentor/ReflectionCommon/issues",
"source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x"
},
"time": "2020-06-27T09:03:43+00:00"
},
{
"name": "phpdocumentor/type-resolver",
"version": "1.6.0",
"source": {
"type": "git",
"url": "https://github.com/phpDocumentor/TypeResolver.git",
"reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706",
"reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0",
"phpdocumentor/reflection-common": "^2.0"
},
"require-dev": {
"ext-tokenizer": "*",
"psalm/phar": "^4.8"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"phpDocumentor\\Reflection\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mike van Riel",
"email": "me@mikevanriel.com"
}
],
"description": "A PSR-5 based resolver of Class names, Types and Structural Element Names",
"support": {
"issues": "https://github.com/phpDocumentor/TypeResolver/issues",
"source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0"
},
"time": "2022-01-04T19:58:01+00:00"
},
{
"name": "phpoption/phpoption",
"version": "1.8.1",
@ -2716,6 +2819,142 @@
],
"time": "2021-09-25T23:10:38+00:00"
},
{
"name": "spatie/laravel-data",
"version": "2.0.2",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-data.git",
"reference": "17d1ddfc5170e85f69bf92498e4b7fc09cf30792"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-data/zipball/17d1ddfc5170e85f69bf92498e4b7fc09cf30792",
"reference": "17d1ddfc5170e85f69bf92498e4b7fc09cf30792",
"shasum": ""
},
"require": {
"illuminate/contracts": "^8.71|^9.0",
"php": "^8.1",
"phpdocumentor/type-resolver": "^1.5",
"spatie/laravel-package-tools": "^1.9.0"
},
"require-dev": {
"fakerphp/faker": "^1.14",
"friendsofphp/php-cs-fixer": "^3.0",
"inertiajs/inertia-laravel": "^0.6.3",
"nette/php-generator": "^3.5",
"nunomaduro/larastan": "^2.0|^1.0.3",
"orchestra/testbench": "^6.24|^7.5",
"phpstan/extension-installer": "^1.1",
"phpunit/phpunit": "^9.3",
"spatie/invade": "^1.0",
"spatie/laravel-typescript-transformer": "^2.0",
"spatie/phpunit-snapshot-assertions": "^4.2",
"spatie/test-time": "^1.2"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Spatie\\LaravelData\\LaravelDataServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Spatie\\LaravelData\\": "src",
"Spatie\\LaravelData\\Database\\Factories\\": "database/factories"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ruben Van Assche",
"email": "ruben@spatie.be",
"role": "Developer"
}
],
"description": "Create unified resources and data transfer objects",
"homepage": "https://github.com/spatie/laravel-data",
"keywords": [
"laravel",
"laravel-data",
"spatie"
],
"support": {
"source": "https://github.com/spatie/laravel-data/tree/2.0.2"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2022-07-29T09:32:53+00:00"
},
{
"name": "spatie/laravel-package-tools",
"version": "1.12.1",
"source": {
"type": "git",
"url": "https://github.com/spatie/laravel-package-tools.git",
"reference": "09f80fa240d44fafb1c70657c74ee44ffa929357"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/spatie/laravel-package-tools/zipball/09f80fa240d44fafb1c70657c74ee44ffa929357",
"reference": "09f80fa240d44fafb1c70657c74ee44ffa929357",
"shasum": ""
},
"require": {
"illuminate/contracts": "^7.0|^8.0|^9.0",
"php": "^7.4|^8.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.23|^7.0",
"phpunit/phpunit": "^9.4",
"spatie/test-time": "^1.2"
},
"type": "library",
"autoload": {
"psr-4": {
"Spatie\\LaravelPackageTools\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"role": "Developer"
}
],
"description": "Tools for creating Laravel packages",
"homepage": "https://github.com/spatie/laravel-package-tools",
"keywords": [
"laravel-package-tools",
"spatie"
],
"support": {
"issues": "https://github.com/spatie/laravel-package-tools/issues",
"source": "https://github.com/spatie/laravel-package-tools/tree/1.12.1"
},
"funding": [
{
"url": "https://github.com/spatie",
"type": "github"
}
],
"time": "2022-06-28T14:29:26+00:00"
},
{
"name": "symfony/console",
"version": "v6.0.3",
@ -4936,59 +5175,6 @@
},
"time": "2022-02-21T01:04:05+00:00"
},
{
"name": "phpdocumentor/reflection-common",
"version": "2.2.0",
"source": {
"type": "git",
"url": "https://github.com/phpDocumentor/ReflectionCommon.git",
"reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpDocumentor/ReflectionCommon/zipball/1d01c49d4ed62f25aa84a747ad35d5a16924662b",
"reference": "1d01c49d4ed62f25aa84a747ad35d5a16924662b",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-2.x": "2.x-dev"
}
},
"autoload": {
"psr-4": {
"phpDocumentor\\Reflection\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jaap van Otterdijk",
"email": "opensource@ijaap.nl"
}
],
"description": "Common reflection classes used by phpdocumentor to reflect the code structure",
"homepage": "http://www.phpdoc.org",
"keywords": [
"FQSEN",
"phpDocumentor",
"phpdoc",
"reflection",
"static analysis"
],
"support": {
"issues": "https://github.com/phpDocumentor/ReflectionCommon/issues",
"source": "https://github.com/phpDocumentor/ReflectionCommon/tree/2.x"
},
"time": "2020-06-27T09:03:43+00:00"
},
{
"name": "phpdocumentor/reflection-docblock",
"version": "5.3.0",
@ -5046,56 +5232,6 @@
},
"time": "2021-10-19T17:43:47+00:00"
},
{
"name": "phpdocumentor/type-resolver",
"version": "1.6.0",
"source": {
"type": "git",
"url": "https://github.com/phpDocumentor/TypeResolver.git",
"reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpDocumentor/TypeResolver/zipball/93ebd0014cab80c4ea9f5e297ea48672f1b87706",
"reference": "93ebd0014cab80c4ea9f5e297ea48672f1b87706",
"shasum": ""
},
"require": {
"php": "^7.2 || ^8.0",
"phpdocumentor/reflection-common": "^2.0"
},
"require-dev": {
"ext-tokenizer": "*",
"psalm/phar": "^4.8"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-1.x": "1.x-dev"
}
},
"autoload": {
"psr-4": {
"phpDocumentor\\Reflection\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mike van Riel",
"email": "me@mikevanriel.com"
}
],
"description": "A PSR-5 based resolver of Class names, Types and Structural Element Names",
"support": {
"issues": "https://github.com/phpDocumentor/TypeResolver/issues",
"source": "https://github.com/phpDocumentor/TypeResolver/tree/1.6.0"
},
"time": "2022-01-04T19:58:01+00:00"
},
{
"name": "phpspec/prophecy",
"version": "v1.15.0",

View file

@ -1,5 +1,6 @@
<?php
use Spatie\LaravelData\LaravelDataServiceProvider;
return [
/*
@ -57,6 +58,7 @@ return [
\App\Providers\AppServiceProvider::class,
\Illuminate\Queue\QueueServiceProvider::class,
\Intonate\TinkerZero\TinkerZeroServiceProvider::class,
LaravelDataServiceProvider::class,
],
];