Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. //ChildNode class
  2.  
  3. class ChildNode extends PNode
  4. {
  5. public $parent;
  6.  
  7. public function __construct($url, PNode $parent)
  8. {
  9. parent::__construct($url);
  10. $this->parent = $parent;
  11.  
  12. }
  13.  
  14.  
  15. public function getParent()
  16. {
  17. return $this->parent;
  18. }
  19.  
  20.  
  21. public function setParent($parent)
  22. {
  23. $this->parent = $parent;
  24. }
  25.  
  26.  
  27. }
  28.  
  29. //PNode Class
  30.  
  31. class PNode
  32. {
  33. public $url;
  34. public $dir;
  35. public $children;
  36. public $title;
  37.  
  38. public function __construct($url)
  39. {
  40. $this->url = $url;
  41. $this->children = array();
  42. $this->dir = parse_url($url, PHP_URL_PATH);
  43. $html = file_get_html($url);
  44. $raw = $html->find('title',0);
  45. $this->title = $raw->innertext;
  46. }
  47.  
  48.  
  49. public function getUrl()
  50. {
  51. return $this->url;
  52. }
  53.  
  54.  
  55. public function setUrl($url)
  56. {
  57. $this->url = $url;
  58. }
  59.  
  60. public function getChildren()
  61. {
  62. return $this->children;
  63. }
  64.  
  65. public function setChildren($children)
  66. {
  67. $this->children = $children;
  68. }
  69. public function addChild(ChildNode $childNode){
  70. $this->children[] = $childNode;
  71.  
  72. }
  73.  
  74.  
  75. public function getDir(){
  76.  
  77. return $this->dir;
  78. }
  79.  
  80. public function getTitle(){
  81. return $this->title;
  82. }
  83.  
  84. public function getParent(){
  85. return $this;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. }
  95.  
  96. //main .php file
  97.  
  98. //$testArr is an array of PNodes each PNode has an array of ChildNodes
  99. //and a ChildNode can also have an Array of ChildNodes
  100.  
  101. var_dump(toJson($testArr[0]->getChildren()));
  102.  
  103. function toJson($arr){
  104. $temp = array();
  105.  
  106. if($arr!=null){
  107.  
  108. foreach ($arr as $item){
  109.  
  110. $temp[] = ["url"=>$item->getUrl(),"Title"=>$item->getTitle(), "children"=>$item->getChildren()];
  111.  
  112. $temp = array_merge($temp, toJson($item->getChildren()));
  113.  
  114.  
  115. }
  116.  
  117.  
  118. }
  119. else{return $temp;}
  120.  
  121.  
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement