From 7f4f0053024d20f1c9c4724e467fffef68ec909f Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 19 Aug 2022 19:52:56 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Replace=20App\Model=20with?= =?UTF-8?q?=20App\Data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Commands/GetShow.php | 6 +- app/Data/Casts/File.php | 19 ++ app/Data/Casts/Folder.php | 19 ++ app/Data/Data.php | 48 +++++ app/Data/FileData.php | 16 ++ app/Data/InputData.php | 42 +++++ app/Models/Input.php | 55 ------ app/Models/Model.php | 57 ------ app/Services/ProcessInput.php | 4 +- composer.json | 3 +- composer.lock | 344 ++++++++++++++++++++++++---------- config/app.php | 2 + 12 files changed, 393 insertions(+), 222 deletions(-) create mode 100644 app/Data/Casts/File.php create mode 100644 app/Data/Casts/Folder.php create mode 100644 app/Data/Data.php create mode 100644 app/Data/FileData.php create mode 100644 app/Data/InputData.php delete mode 100644 app/Models/Input.php delete mode 100644 app/Models/Model.php diff --git a/app/Commands/GetShow.php b/app/Commands/GetShow.php index 399715e..343c3fd 100644 --- a/app/Commands/GetShow.php +++ b/app/Commands/GetShow.php @@ -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(); } } diff --git a/app/Data/Casts/File.php b/app/Data/Casts/File.php new file mode 100644 index 0000000..9a6cf1e --- /dev/null +++ b/app/Data/Casts/File.php @@ -0,0 +1,19 @@ +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; + } +} diff --git a/app/Data/FileData.php b/app/Data/FileData.php new file mode 100644 index 0000000..0045f50 --- /dev/null +++ b/app/Data/FileData.php @@ -0,0 +1,16 @@ + $path]); + } +} diff --git a/app/Data/InputData.php b/app/Data/InputData.php new file mode 100644 index 0000000..fc4a5b8 --- /dev/null +++ b/app/Data/InputData.php @@ -0,0 +1,42 @@ +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(); + } +} diff --git a/app/Models/Input.php b/app/Models/Input.php deleted file mode 100644 index 71e913f..0000000 --- a/app/Models/Input.php +++ /dev/null @@ -1,55 +0,0 @@ -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 - } -} diff --git a/app/Models/Model.php b/app/Models/Model.php deleted file mode 100644 index 55dd40f..0000000 --- a/app/Models/Model.php +++ /dev/null @@ -1,57 +0,0 @@ - $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); - } -} diff --git a/app/Services/ProcessInput.php b/app/Services/ProcessInput.php index 470b3ac..3195c5c 100644 --- a/app/Services/ProcessInput.php +++ b/app/Services/ProcessInput.php @@ -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; } diff --git a/composer.json b/composer.json index edfbdae..63e76d8 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index cb456e8..65280ff 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/config/app.php b/config/app.php index f3566ec..0dec1ba 100644 --- a/config/app.php +++ b/config/app.php @@ -1,5 +1,6 @@