Skip to content

Commit 5e52c7a

Browse files
committed
Refactor more
1 parent 570f646 commit 5e52c7a

File tree

4 files changed

+287
-122
lines changed

4 files changed

+287
-122
lines changed

src/Kalnoy/Nestedset/Collection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function toTree($rootNodeId = null)
4444

4545
$rootNodeId = $this->getRootNodeId($rootNodeId);
4646

47-
if (!$dictionary->has($rootNodeId))
47+
if ( ! $dictionary->has($rootNodeId))
4848
{
4949
return $result;
5050
}
@@ -53,11 +53,7 @@ public function toTree($rootNodeId = null)
5353

5454
foreach ($this->items as $item)
5555
{
56-
$key = $item->getKey();
57-
58-
$children = $dictionary->has($key)
59-
? $dictionary->get($key)
60-
: array();
56+
$children = $dictionary->get($item->getKey(), []);
6157

6258
$item->setRelation('children', new BaseCollection($children));
6359
}

src/Kalnoy/Nestedset/Node.php

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ protected static function signOnEvents()
106106
{
107107
static::deleting(function ($model)
108108
{
109+
// We will need fresh data to delete node safely
109110
$model->refreshNode();
110111
});
111112

@@ -286,9 +287,7 @@ public function refreshNode()
286287
{
287288
if ( ! $this->exists || static::$actionsPerformed === 0) return;
288289

289-
$attributes = $this->newServiceQuery()
290-
->where($this->getKeyName(), '=', $this->getKey())
291-
->first([ static::LFT, static::RGT ]);
290+
$attributes = $this->newServiceQuery()->getNodeData($this->getKey());
292291

293292
$this->attributes = array_merge($this->attributes, $attributes);
294293
}
@@ -332,9 +331,7 @@ public function children()
332331
*/
333332
public function descendants()
334333
{
335-
$query = $this->newQuery();
336-
337-
return $query->whereBetween(static::LFT, array($this->getLft() + 1, $this->getRgt()));
334+
return $this->newQuery()->whereDescendantOf($this->getKey());
338335
}
339336

340337
/**
@@ -397,9 +394,7 @@ public function prevSiblings()
397394
*/
398395
public function next()
399396
{
400-
return $this->newQuery()
401-
->where(static::LFT, '>', $this->attributes[static::LFT])
402-
->defaultOrder();
397+
return $this->newQuery()->whereIsAfter($this->getKey())->defaultOrder();
403398
}
404399

405400
/**
@@ -409,9 +404,7 @@ public function next()
409404
*/
410405
public function prev()
411406
{
412-
return $this->newQuery()
413-
->where(static::LFT, '<', $this->attributes[static::LFT])
414-
->reversed();
407+
return $this->newQuery()->whereIsBefore($this->getKey())->reversed();
415408
}
416409

417410
/**
@@ -421,17 +414,7 @@ public function prev()
421414
*/
422415
public function ancestors()
423416
{
424-
$query = $this->newQuery();
425-
$grammar = $query->getQuery()->getGrammar();
426-
$table = $this->getTable();
427-
$lft = $grammar->wrap(static::LFT);
428-
$rgt = $grammar->wrap(static::RGT);
429-
430-
$lftValue = $this->getLft();
431-
432-
return $query
433-
->whereRaw("? between $lft and $rgt", array($lftValue))
434-
->where(static::LFT, "<>", $lftValue);
417+
return $this->newQuery()->whereAncestorOf($this->getKey());
435418
}
436419

437420
/**
@@ -639,7 +622,7 @@ protected function moveNode($pos)
639622

640623
$params = compact('lft', 'rgt', 'from', 'to', 'height', 'distance');
641624

642-
$query = $this->newServiceQuery()
625+
$query = $this->newServiceQuery()->getQuery()
643626
->whereBetween(static::LFT, array($from, $to))
644627
->orWhereBetween(static::RGT, array($from, $to));
645628

@@ -679,7 +662,7 @@ protected function makeGap($cut, $height)
679662
{
680663
$params = compact('cut', 'height');
681664

682-
$query = $this->newServiceQuery();
665+
$query = $this->newServiceQuery()->getQuery();
683666

684667
return $query
685668
->where(static::LFT, '>=', $cut)
@@ -743,7 +726,7 @@ protected function getColumnPatch($col, array $params)
743726
protected function deleteNode()
744727
{
745728
// DBMS with support of foreign keys will remove descendant nodes automatically
746-
$this->descendants()->delete();
729+
$this->newQuery()->whereNodeBetween([ $this->getLft(), $this->getRgt() ])->delete();
747730

748731
// In case if user wants to re-create the node
749732
$this->makeRoot();
@@ -752,17 +735,13 @@ protected function deleteNode()
752735
}
753736

754737
/**
755-
* Get a new base query builder instance.
756-
*
757-
* @return \Kalnoy\Nestedset\QueryBuilder
738+
* {@inheritdoc}
739+
*
740+
* @since 1.2
758741
*/
759-
protected function newBaseQueryBuilder()
742+
public function newEloquentBuilder($query)
760743
{
761-
$conn = $this->getConnection();
762-
763-
$grammar = $conn->getQueryGrammar();
764-
765-
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor(), $this);
744+
return new QueryBuilder($query);
766745
}
767746

768747
/**
@@ -772,15 +751,11 @@ protected function newBaseQueryBuilder()
772751
*/
773752
protected function newServiceQuery()
774753
{
775-
return with(static::$softDelete ? $this->withTrashed() : $this->newQuery())->getQuery();
754+
return static::$softDelete ? $this->withTrashed() : $this->newQuery();
776755
}
777756

778757
/**
779-
* Create a new NestedSet Collection instance.
780-
*
781-
* @param array $models
782-
*
783-
* @return \Kalnoy\Nestedset\Collection
758+
* {@inheritdoc}
784759
*/
785760
public function newCollection(array $models = array())
786761
{
@@ -941,7 +916,7 @@ public function getPrev(array $columns = array('*'))
941916
*/
942917
public function getAncestors(array $columns = array('*'))
943918
{
944-
return $this->ancestors()->get($columns);
919+
return $this->newQuery()->ancestorsOf($this->getKey(), $columns);
945920
}
946921

947922
/**
@@ -953,7 +928,7 @@ public function getAncestors(array $columns = array('*'))
953928
*/
954929
public function getDescendants(array $columns = array('*'))
955930
{
956-
return $this->descendants()->get($columns);
931+
return $this->newQuery()->descendantsOf($this->getKey(), $columns);
957932
}
958933

959934
/**
@@ -1015,4 +990,16 @@ public function getPrevSibling(array $columns = array('*'))
1015990
{
1016991
return $this->prevSiblings()->reversed()->first($columns);
1017992
}
993+
994+
/**
995+
* Get whether a node is a descendant of other node.
996+
*
997+
* @param \Kalnoy\Nestedset\Node $other
998+
*
999+
* @return bool
1000+
*/
1001+
public function isDescendantOf(Node $other)
1002+
{
1003+
return $this->getLft() > $other->getLft() and $this->getLft() < $other->getRgt();
1004+
}
10181005
}

0 commit comments

Comments
 (0)