Skip to content

Instantly share code, notes, and snippets.

@raank
Created August 31, 2016 19:41
Show Gist options
  • Save raank/bdccb4513504f2ab7024513dd7414fab to your computer and use it in GitHub Desktop.
Save raank/bdccb4513504f2ab7024513dd7414fab to your computer and use it in GitHub Desktop.
Class tree for mount tree
<?php
namespace App\Helpers;
class Tree
{
public $total;
public $hasUp;
public $hasDown;
public $id;
public $name;
public $email;
public $username;
public $treeDefault;
public $network;
public function __construct( $id )
{
$this->total = 'total users';
$this->hasUp = 'get user up in array';
$this->hasDown = 'get user down in array';
$this->id = $id;
$this->name = $this->getUser('name');
$this->email = $this->getUser('email');
$this->username = $this->getUser('username');
$this->treeDefault = $this->getTreeJson( $id );
$this->network = $this->getNetwork( $id );
}
/**
* @param $field
* @return mixed
*/
public function getUser( $field )
{
$user = \App\Models\User::where( 'id', $this->id )->first();
return $user[ $field ];
}
/**
* @param null $id
* @return string
*/
public function getTreeJson( $id = null )
{
return json_encode( $this->getNetwork( $id ) );
}
/**
* Mount Tree Array
* @return array
*/
public function getNetwork( $id = null )
{
$hasId = (isset($id) ? $id : $this->id);
$network = \App\Models\Network::where( 'user_id', $hasId )->first();
$tree = json_decode( $network['users'] );
foreach( $tree as $key => $item ) {
$newTree[ $key ] = ($item->user != '' ? new Tree( $item->user ) : null);
}
return (isset($newTree) ? $newTree : null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment