Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 49 |
App\Models\CategoryCollection | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 49 |
fromArrayToCategory | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 10 |
|||
getChildren | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 15 |
|||
buildMenu | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 5 |
|||
prepareMenu | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 19 |
<?php | |
namespace App\Models; | |
class CategoryCollection { | |
/** | |
* | |
* @param array $array | |
* @return \App\Models\Node | |
*/ | |
public function fromArrayToCategory($array) { | |
$value = $array[0]; | |
$node = new Node(); | |
$node->setId($value['id']); | |
$node->setName($value['name']); | |
$node->setLft($value['lft']); | |
$node->setRgt($value['rgt']); | |
$node->setLevel($value['level']); | |
$node->setChildren($this->getChildren($array, $node->getLevel(), $node->getLft(), $node->getRgt())); | |
return $node; | |
} | |
/** | |
* | |
* @param array $array | |
* @param string $level (number as string) | |
* @param string $lft (number as string) | |
* @param string $rgt (number as string) | |
* @return \App\Models\Node | |
*/ | |
private function getChildren($array, $level, $lft, $rgt) { | |
$nodeArray = []; | |
foreach ($array as $value) { | |
if ($level + 1 == $value['level'] && $lft < $value['lft'] && $rgt > $value['rgt']) { | |
$child = new Node(); | |
$child->setId($value['id']); | |
$child->setName($value['name']); | |
$child->setLft($value['lft']); | |
$child->setRgt($value['rgt']); | |
$child->setLevel($value['level']); | |
$child->setChildren($this->getChildren($array, $child->getLevel(), $child->getLft(), $child->getRgt())); | |
$nodeArray[] = $child; | |
} | |
} | |
return $nodeArray; | |
} | |
public function buildMenu() { | |
$nestedSet = new \App\Models\NestedSet(); | |
$nestedSet->setTable('category'); | |
$array = $nestedSet->getAllNodes(); | |
return $this->fromArrayToCategory($array); | |
} | |
/** | |
* | |
* @param \App\Models\Node $node | |
* @return string html-list | |
*/ | |
public function prepareMenu($node) { | |
$output = ''; | |
if (empty($node->getChildren())) { | |
$output .= '<li><a href="#">' . $node->getName() . '</a></li>'; | |
} else { | |
if ($node->getLevel() == 0) { | |
} else { | |
$output .= '<li class="category"><a href="#">' . $node->getName() . '</a>' | |
. '<ul class="level-1">'; | |
} | |
foreach ($node->getChildren() as $childNode) { | |
$output .= $this->prepareMenu($childNode); | |
} | |
if ($node->getLevel() == 0) { | |
//$output .= '</li>'; | |
} else { | |
$output .= '</ul></li>'; | |
} | |
} | |
return $output; | |
} | |
} |