Ejemplo n.º 1
0
 /**
  * @param array $data
  * @return string
  */
 public static function treeToArray(\Kalnoy\Nestedset\Collection $tree)
 {
     if (!$tree->count()) {
         return null;
     } else {
         return array_map(function ($item) {
             $data = array();
             $data['name'] = $item->name;
             $data['name_ru'] = $item->name_ru;
             $data['level'] = $item->depth;
             if ($item->descendants()->count() > 0) {
                 $data['children'] = self::treeToArray($item->descendants()->withdepth()->get());
             }
             return $data;
         }, $tree->all());
     }
 }
Ejemplo n.º 2
0
 /**
  * Flatten a tree into a non recursive array.
  *
  * @param Collection $groupedNodes
  * @param mixed $parentId
  *
  * @return $this
  */
 protected function flattenTree(self $groupedNodes, $parentId)
 {
     foreach ($groupedNodes->get($parentId, []) as $node) {
         $this->push($node);
         $this->flattenTree($groupedNodes, $node->getKey());
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * @param Collection $models
  * @param int $fixed
  * @param $parentId
  * @param int $cut
  *
  * @return int
  */
 protected static function reorderNodes(Collection $models, &$fixed, $parentId = null, $cut = 1)
 {
     /** @var Node $model */
     foreach ($models->get($parentId, []) as $model) {
         $model->setLft($cut);
         $cut = self::reorderNodes($models, $fixed, $model->getKey(), $cut + 1);
         $model->setRgt($cut);
         if ($model->isDirty()) {
             $model->save();
             $fixed++;
         }
         ++$cut;
     }
     return $cut;
 }
Ejemplo n.º 4
0
 public function testToTreeWithSpecifiedRoot()
 {
     $node = $this->findCategory('mobile');
     $nodes = Category::whereBetween('_lft', array(8, 17))->get();
     $tree1 = \Kalnoy\Nestedset\Collection::make($nodes)->toTree(5);
     $tree2 = \Kalnoy\Nestedset\Collection::make($nodes)->toTree($node);
     $this->assertEquals(4, $tree1->count());
     $this->assertEquals(4, $tree2->count());
 }