diff --git a/src/Foundation/DataTransferObject.php b/src/Foundation/DataTransferObject.php index 9ae089b..7256c66 100644 --- a/src/Foundation/DataTransferObject.php +++ b/src/Foundation/DataTransferObject.php @@ -2,6 +2,7 @@ namespace KoalaFacade\DiamondConsole\Foundation; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Support\Traits\Tappable; @@ -25,7 +26,7 @@ protected function toExcludedPropertiesOnCreate(): array } /** - * Prevent properties to included on create + * Prevent properties to included on update * * @return array */ @@ -73,4 +74,24 @@ public function dd(): never { dd($this); } + + /** + * Abilities to orchestrate the Data + * + * @param mixed ...$values + * @return static + */ + public function recycle(mixed ...$values): static + { + $callback = Arr::first($values); + + if (Collection::make($values)->containsOneItem() && $callback instanceof \Closure) { + /** @var static $instance */ + $instance = call_user_func($callback, $this); + } else { + $instance = $this->with(...$values); + } + + return $instance; + } } diff --git a/tests/Feature/Commands/Domain/ModelMakeCommandTest.php b/tests/Feature/Commands/Domain/ModelMakeCommandTest.php index 258b5b6..9bd280e 100644 --- a/tests/Feature/Commands/Domain/ModelMakeCommandTest.php +++ b/tests/Feature/Commands/Domain/ModelMakeCommandTest.php @@ -8,7 +8,6 @@ use KoalaFacade\DiamondConsole\Exceptions\FileAlreadyExistException; it(description: 'can generate new Model class') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/User.php'; @@ -28,7 +27,6 @@ ->group(groups: 'commands'); it(description: 'can generate new Model class with separator') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/Foo/bar.php'; @@ -48,7 +46,6 @@ ->group(groups: 'commands'); it(description: 'can force generate exists Model class') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/User.php'; @@ -70,7 +67,6 @@ ->group(groups: 'commands'); it(description: 'can generate new Model class with Migration') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/User.php'; @@ -104,7 +100,6 @@ ->group(groups: 'commands'); it(description: 'can force generate exists Model class with Migration') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/User.php'; @@ -182,7 +177,6 @@ ->group(groups: 'commands'); it(description: 'cannot generate the Model, if the Model already exists') - ->skip() ->tap(function () { $fileName = '/Shared/User/Models/User.php'; diff --git a/tests/Unit/DataTransferObjects/DataTransferObjectTest.php b/tests/Unit/DataTransferObjects/DataTransferObjectTest.php index c434509..3f4022c 100644 --- a/tests/Unit/DataTransferObjects/DataTransferObjectTest.php +++ b/tests/Unit/DataTransferObjects/DataTransferObjectTest.php @@ -1,5 +1,6 @@ group('unit', 'dto'); + +it(description: 'can recycle the data directly') + ->group('unit', 'dto') + ->skip( + conditionOrMessage: InstalledVersions::getVersion(packageName: 'spatie/php-cloneable') === '1.0.0.0' + ) + ->tap(callable: function () { + $data = UserData::resolve(data: [ + 'name' => 'Kevin' + ]); + + $addresses = [ + 'main_address' => 'where', + 'main_address_1' => 'where', + ]; + + $data + ->recycle( + addresses: $addresses + ) + ->tap(callback: function (UserData $data) use ($addresses) { + expect($data->addresses)->toMatchArray(array: $addresses); + }); + }); + +it(description: 'can recycle the data with callback') + ->group('unit', 'dto') + ->skip( + conditionOrMessage: InstalledVersions::getVersion(packageName: 'spatie/php-cloneable') === '1.0.0.0' + ) + ->tap(callable: function () { + $data = UserData::resolve(data: [ + 'name' => 'Kevin' + ]); + + $addresses = [ + 'main_address' => 'where', + 'main_address_1' => 'where', + ]; + + $data + ->recycle(function (UserData $data) use ($addresses): UserData { + return $data->with(addresses: $addresses); + }) + ->tap(callback: function (UserData $data) use ($addresses) { + expect($data->addresses)->toMatchArray(array: $addresses); + }); + }); \ No newline at end of file