qsadfasdgfgads

Untitled

Apr 25th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace App\Services;
  5.  
  6.  
  7. use App\Category;
  8. use App\Power;
  9. use PhpParser\Node\Expr\AssignOp\Pow;
  10.  
  11. class Tree
  12. {
  13.     /**
  14.      * @var array
  15.      */
  16.     private $records;
  17.  
  18.     public function __construct(array $records)
  19.     {
  20.         $this->records = $records;
  21.     }
  22.  
  23.     public function build(int $parentId): array
  24.     {
  25.         $powers = Power::all();
  26.         $result = [];
  27.         foreach ($this->records as $record) {
  28.            if(empty($record['descendants'])){
  29.                 if(!isset($record['powers'])) {
  30.                     $record['powers'] = [];
  31.                 }
  32.  
  33.                 if( isset($record['products']) ){
  34.                     foreach ($record['products'] as $commodity){
  35.                         $power = $powers->where('id', $commodity['power_id'])->first();
  36.                         $record['powers'][] = $power['power'];
  37.                     }
  38.                 }
  39.  
  40.  
  41.            }
  42.             if ($record['parent_id'] === $parentId) {
  43.                 $record['children'] = $this->build($record['id']);
  44.                 $result[] = $record;
  45.             }
  46.         }
  47.         return $result;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment