From 119664ecb6636c1b7b83efa0d3c16d3bbdefdff2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 30 Jul 2019 22:18:37 +0300 Subject: [PATCH] Added a `builders()` method. --- README.md | 10 ++++++++ .../IncorrectBuilderTypeException.php | 16 +++++++++++++ src/Services/LastModified.php | 23 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/Exceptions/IncorrectBuilderTypeException.php diff --git a/README.md b/README.md index 3850601..d330116 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,10 @@ public function handle() { $collection_1 = Foo::whereIsActive(true)->get(); $collection_2 = Bar::where('id', '>', 50)->get(); $collection_3 = Baz::query()->get(); + + $builder_1 = Foo::whereIsActive(true); + $builder_2 = Bar::where('id', '>', 50); + $builder_3 = Baz::query(); $model_1 = Foo::whereIsActive(true)->first(); $model_2 = Bar::where('id', '>', 50)->first(); @@ -88,6 +92,7 @@ public function handle() { (new LastModified) ->collections($collection_1, $collection_2, $collection_3) + ->builders($builder_1, $builder_2, $builder_3) ->models($model_1, $model_2, $model_3) ->manuals($item_1, $item_2, $item_3) ->update(bool $force = false); @@ -102,6 +107,10 @@ use Helldar\LastModified\Services\LastItem; public function handle() { $collection_1 = Foo::whereIsActive(false)->get(); $collection_2 = Bar::whereIn('id', [50, 60, 62, 73])->get(); + + $builder_1 = Foo::whereIsActive(true); + $builder_2 = Bar::where('id', '>', 50); + $builder_3 = Baz::query(); $model_1 = Foo::whereIsActive(false)->first(); $model_2 = Bar::whereIn('id', [50, 60, 62, 73])->first(); @@ -111,6 +120,7 @@ public function handle() { (new LastModified) ->collections($collection_1, $collection_2, $collection_3) + ->builders($builder_1, $builder_2, $builder_3) ->models($model_1, $model_2, $model_3) ->manuals($item_1, $item_2, $item_3) ->delete(bool $force = false); diff --git a/src/Exceptions/IncorrectBuilderTypeException.php b/src/Exceptions/IncorrectBuilderTypeException.php new file mode 100644 index 0000000..dfdb8ba --- /dev/null +++ b/src/Exceptions/IncorrectBuilderTypeException.php @@ -0,0 +1,16 @@ +builders = (array) $builders; + + return $this; + } + public function models(...$models) { $this->models = (array) $models; @@ -51,6 +62,18 @@ public function update(bool $force = false) }); } + foreach ($this->builders as $builder) { + if ($builder instanceof Builder) { + $builder + ->get() + ->each(function ($item) { + $this->store($item); + }); + } else { + throw new IncorrectBuilderTypeException(\get_class($builder)); + } + } + foreach ($this->models as $model) { $this->store($model); }