Skip to content

Commit

Permalink
Merge pull request #16 from CoopBelvedere/restored_lazy_collection_on…
Browse files Browse the repository at this point in the history
…_nodes_list

Restored lazy collection on nodes list
  • Loading branch information
pascalboucher authored Oct 6, 2019
2 parents 042fda0 + 8233c67 commit 46aac48
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 27 deletions.
7 changes: 3 additions & 4 deletions src/Contracts/Models/Rankings/RankerContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Belvedere\FormMaker\Contracts\Models\Rankings;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Relations\MorphOne;

interface RankerContract
Expand Down Expand Up @@ -119,10 +118,10 @@ public function shuffle(): void;
/**
* Order the list according to the items position in the ranking.
*
* @param \Illuminate\Support\Collection $nodes
* @return \Illuminate\Support\Collection
* @param \Illuminate\Support\LazyCollection|\Illuminate\Support\Collection $nodes
* @return mixed
*/
public function sortByRank(Collection $nodes);
public function sortByRank($nodes);

/**
* Toggle two nodes in the ranking.
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/Repositories/NodeRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Belvedere\FormMaker\Contracts\Repositories;

use Illuminate\Support\Collection;
use Belvedere\FormMaker\Models\Model;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Models\Nodes\Node;

interface NodeRepositoryContract
Expand All @@ -13,9 +13,9 @@ interface NodeRepositoryContract
*
* @param \Belvedere\FormMaker\Models\Model $parent
* @param string|null $type
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function all(Model $parent, ?string $type = null): Collection;
public function all(Model $parent, ?string $type = null): LazyCollection;

/**
* Add a node to the parent model.
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/Traits/Nodes/HasNodesContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Belvedere\FormMaker\Contracts\Traits\Nodes;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Models\Nodes\Node;
use Belvedere\FormMaker\Contracts\Traits\Rankings\HasRankingsContract;

Expand Down Expand Up @@ -59,7 +59,7 @@ public function node($key): ?Node;
* Get the nodes filtered by type or not and sorted by their position in the ranking.
*
* @param string|null $type
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function nodes(?string $type = null): Collection;
public function nodes(?string $type = null): LazyCollection;
}
6 changes: 3 additions & 3 deletions src/Contracts/Traits/Nodes/HasOptionsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Belvedere\FormMaker\Contracts\Traits\Nodes;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Contracts\Traits\Rankings\HasRankingsContract;
use Belvedere\FormMaker\Contracts\Models\Nodes\Inputs\Option\OptionerContract;

Expand Down Expand Up @@ -35,7 +35,7 @@ public function option($key): ?OptionerContract;
/**
* Get the options sorted by their position in the ranking.
*
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function options(): Collection;
public function options(): LazyCollection;
}
7 changes: 3 additions & 4 deletions src/Models/Rankings/Ranker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Belvedere\FormMaker\Models\Rankings;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Belvedere\FormMaker\Contracts\Models\Rankings\RankerContract;
Expand Down Expand Up @@ -358,10 +357,10 @@ public function shuffle(): void
/**
* Order the list according to the nodes position in the ranking.
*
* @param \Illuminate\Support\Collection $nodes
* @return \Illuminate\Support\Collection
* @param \Illuminate\Support\LazyCollection|\Illuminate\Support\Collection $nodes
* @return mixed
*/
public function sortByRank(Collection $nodes)
public function sortByRank($nodes)
{
return $nodes->sortBy(function ($node, $key) {
return $this->rank($node);
Expand Down
8 changes: 4 additions & 4 deletions src/Repositories/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Belvedere\FormMaker\Repositories;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Belvedere\FormMaker\Models\Model;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Models\Nodes\Node;
use Belvedere\FormMaker\Contracts\Repositories\NodeRepositoryContract;

Expand Down Expand Up @@ -48,9 +48,9 @@ class NodeRepository implements NodeRepositoryContract
*
* @param \Belvedere\FormMaker\Models\Model $parent
* @param string|null $type
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function all(Model $parent, ?string $type = null): Collection
public function all(Model $parent, ?string $type = null): LazyCollection
{
$query = DB::table(config('form-maker.database.form_nodes_table', 'form_nodes'))
->where('nodable_type', $parent->getMorphClass())
Expand All @@ -63,7 +63,7 @@ public function all(Model $parent, ?string $type = null): Collection
$query->where('type', $type);
}

return $query->get()->groupBy('type')->map(function ($nodes, $key) {
return $query->cursor()->groupBy('type')->map(function ($nodes, $key) {
return $this->resolve($nodes[0]->type)::hydrate($nodes->toArray());
})->flatten(1);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Nodes/HasNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Belvedere\FormMaker\Traits\Nodes;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Models\Nodes\Node;
use Belvedere\FormMaker\Contracts\Repositories\NodeRepositoryContract;

Expand Down Expand Up @@ -102,9 +102,9 @@ public function node($key): ?Node
* Get the nodes filtered by type or not and sorted by their position in the ranking.
*
* @param string|null $type
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function nodes(?string $type = null): Collection
public function nodes(?string $type = null): LazyCollection
{
$nodeRepository = resolve(NodeRepositoryContract::class);

Expand Down
6 changes: 3 additions & 3 deletions src/Traits/Nodes/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Belvedere\FormMaker\Traits\Nodes;

use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Belvedere\FormMaker\Contracts\Repositories\NodeRepositoryContract;
use Belvedere\FormMaker\Contracts\Models\Nodes\Inputs\Option\OptionerContract;

Expand Down Expand Up @@ -64,9 +64,9 @@ public function option($key): ?OptionerContract
/**
* Get the options sorted by their position in the ranking.
*
* @return \Illuminate\Support\Collection
* @return \Illuminate\Support\LazyCollection
*/
public function options(): Collection
public function options(): LazyCollection
{
$nodeRepository = resolve(NodeRepositoryContract::class);

Expand Down

0 comments on commit 46aac48

Please sign in to comment.