生成树 Tree [ 2.0 版本 ]
<?php
namespace core\common\utils;
use JsonSerializable;
use yii\base\BaseObject;
/**
*
- @property int|null $id 节点Id
- @property int|null $pid 父节点Id
- @property int|null $sort 排序
- @property array|null $children 子节点(子树)
- @property array $data
@property string|null $dataType
*/
class Node extends BaseObject implements JsonSerializable {
private $_id;
private $_pid;
private $_sort;
private $_children = [];
private $_data;
private $_dataType;public function getId(){
return $this->_id;
}
public function setId($value){$this->_id = $value;
}
public function getPid(){return $this->_pid;
}
public function setPid($value){$this->_pid = $value;
}
public function getSort(){return $this->_sort;
}
public function setSort($value){$this->_sort = $value;
}
public function getChildren()
{return $this->_children;
}
public function setChildren($value){if (is_array($value)) $this->_children = $value; else{ if ( !empty($value) ){ $this->_children[] = $value; }else{ $this->_children = []; } }
}
public function getData()
{return $this->_data;
}
public function setData($value)
{$this->_data = $value;
}
public function getDataType()
{return $this->_dataType;
}
public function setDataType($value)
{$this->_dataType = $value;
}
// if (method_exists($this, $getter))
// if (property_exists($this, $getter))
// canGetProperty() 或 canSetProperty()
public function __get($name){if ( isset($this->_data[$name]) ) return $this->_data[$name]; //$name=root 调用 getRoot return parent::__get($name); // TODO: Change the autogenerated stub
}
public function __set($name, $value)
{if ( isset($this->_data[$name]) ) $this->_data[$name] = $value; else parent::__set($name, $value); // TODO: Change the autogenerated stub
}
// 实现的抽象类方法,指定需要被序列化JSON的数据
public function jsonSerialize() {$data = []; /* foreach ($this as $key=>$val){ if ($val !== null) { $data[$key] = $val; } }*/ $data['id'] = $this->_id; $data['pid'] = $this->_pid; $data['data'] = $this->_data; $data['children'] = $this->_children; return $data;
}
}
class Tree extends BaseObject{
private $_tree = [];
private $_tree_index = [];
private $_tree_dataType = null;
private $_id_tag = null;
private $_pid_tag = null;
public function __construct($id_tag= null,$pid_tag= null,$config = []){
$this->_id_tag = $id_tag ?? 'id';
$this->_pid_tag = $pid_tag ?? 'pid';
parent::__construct($config);
}
public function create($list){
if ( empty($this->tree_dataType) )
$this->_tree_dataType = get_class($list[0]);
if ($this->_tree_dataType == get_class($list[0])){
foreach ($list as $key => $value){
$node = new Node();
$node->id = $value[$this->_id_tag];
$node->pid = $value[$this->_pid_tag];
$node->sort = $value['sort'];
$node->data = $value->attributes;
$node->dataType = $this->_tree_dataType;
$node->children = $this->getAllChildren($node->id);
$this->_tree_index[$node->id] = $node;
$p_node = $this->getFatherNode($node->id);
if ( !empty( $p_node ) ){
$p_node->children = $node;
}else if ( empty( $p_node ) && empty($node->pid) ){
// 没有父节
$this->_tree[] = $node;
}
unset($list[$key]);
}
}else{
throw new Exception("数据类型不一致");
}
$this->sort($this->_tree);
return $this->_tree;
}
public function getFindRoot($id):?Node{
if ( !empty( $id ) ){
$node = self::getFatherNode($id);
if ( !empty($node->pid) ){
return self::getFindRoot($node->id);
}
else{
return $node;
}
}
return null;
}
public function getNode($id):?Node{
$node = $this->_tree_index[$id];
if ( !empty($node))
return $node;
return null;
}
public function getFatherNode($id):?Node{
if ( !empty( $id ) ){
$node = self::getNode($id);
if ( isset($node) ){
$pid = $node->pid;
if ( !empty( $pid ) )
return self::getNode($pid);
}
}
return null;
}
public function getAllChildren($id){
$temp = [];
foreach ($this->_tree_index as $key => $value){
if ( $value->pid == $id ){
$temp[] = $value;
}
}
return $temp;
}
public function sort(&$tree){
foreach ($tree as $key => $item){
if ( !empty( $item->children )){
$tm = $item->children;
self::sort($tm);
$item->children = $tm;
}
}
return Sort::property_sort($tree,Sort::SORT_ASC,'sort');
}
}
使用
$result = $goods->category->findAll();
$tree = new Tree('classId','classPId');
$_Json->data = $tree->create($result['data']);
// $result['data'] 是一个 array|ActiveRecord[]
villers
最后登录:2020-10-16
在线时长:2小时2分
- 粉丝0
- 金钱55
- 威望0
- 积分75
共 0 条评论