Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- use App\Category;
- use App\Power;
- use PhpParser\Node\Expr\AssignOp\Pow;
- class Tree
- {
- /**
- * @var array
- */
- private $records;
- public function __construct(array $records)
- {
- $this->records = $records;
- }
- public function build(int $parentId): array
- {
- $powers = Power::all();
- $result = [];
- foreach ($this->records as $record) {
- if(empty($record['descendants'])){
- if(!isset($record['powers'])) {
- $record['powers'] = [];
- }
- if( isset($record['products']) ){
- foreach ($record['products'] as $commodity){
- $power = $powers->where('id', $commodity['power_id'])->first();
- $record['powers'][] = $power['power'];
- }
- }
- }
- if ($record['parent_id'] === $parentId) {
- $record['children'] = $this->build($record['id']);
- $result[] = $record;
- }
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment