Node.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Tree;
  7. use Magento\Framework\Data\Tree;
  8. use Magento\Framework\Data\Tree\Node\Collection;
  9. /**
  10. * Data tree node
  11. *
  12. * @api
  13. * @author Magento Core Team <core@magentocommerce.com>
  14. * @since 100.0.2
  15. */
  16. class Node extends \Magento\Framework\DataObject
  17. {
  18. /**
  19. * Parent node
  20. *
  21. * @var Node
  22. */
  23. protected $_parent;
  24. /**
  25. * Main tree object
  26. *
  27. * @var Tree
  28. */
  29. protected $_tree;
  30. /**
  31. * Child nodes
  32. *
  33. * @var Collection
  34. */
  35. protected $_childNodes;
  36. /**
  37. * Node ID field name
  38. *
  39. * @var string
  40. */
  41. protected $_idField;
  42. /**
  43. * Data tree node constructor
  44. *
  45. * @param array $data
  46. * @param string $idField
  47. * @param Tree $tree
  48. * @param Node $parent
  49. */
  50. public function __construct($data, $idField, $tree, $parent = null)
  51. {
  52. $this->setTree($tree);
  53. $this->setParent($parent);
  54. $this->setIdField($idField);
  55. $this->setData($data);
  56. $this->_childNodes = new Collection($this);
  57. }
  58. /**
  59. * Retrieve node id
  60. *
  61. * @return mixed
  62. */
  63. public function getId()
  64. {
  65. return $this->getData($this->getIdField());
  66. }
  67. /**
  68. * Set node id field name
  69. *
  70. * @param string $idField
  71. * @return $this
  72. */
  73. public function setIdField($idField)
  74. {
  75. $this->_idField = $idField;
  76. return $this;
  77. }
  78. /**
  79. * Retrieve node id field name
  80. *
  81. * @return string
  82. */
  83. public function getIdField()
  84. {
  85. return $this->_idField;
  86. }
  87. /**
  88. * Set node tree object
  89. *
  90. * @param Tree $tree
  91. * @return $this
  92. */
  93. public function setTree(Tree $tree)
  94. {
  95. $this->_tree = $tree;
  96. return $this;
  97. }
  98. /**
  99. * Retrieve node tree object
  100. *
  101. * @return Tree
  102. */
  103. public function getTree()
  104. {
  105. return $this->_tree;
  106. }
  107. /**
  108. * Set node parent
  109. *
  110. * @param Node $parent
  111. * @return $this
  112. */
  113. public function setParent($parent)
  114. {
  115. $this->_parent = $parent;
  116. return $this;
  117. }
  118. /**
  119. * Retrieve node parent
  120. *
  121. * @return Tree
  122. */
  123. public function getParent()
  124. {
  125. return $this->_parent;
  126. }
  127. /**
  128. * Check node children
  129. *
  130. * @return bool
  131. */
  132. public function hasChildren()
  133. {
  134. return $this->_childNodes->count() > 0;
  135. }
  136. /**
  137. * @param mixed $level
  138. * @return $this
  139. */
  140. public function setLevel($level)
  141. {
  142. $this->setData('level', $level);
  143. return $this;
  144. }
  145. /**
  146. * @param mixed $path
  147. * @return $this
  148. */
  149. public function setPathId($path)
  150. {
  151. $this->setData('path_id', $path);
  152. return $this;
  153. }
  154. /**
  155. * @param Node $node
  156. * @return void
  157. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  158. */
  159. public function isChildOf($node)
  160. {
  161. }
  162. /**
  163. * Load node children
  164. *
  165. * @param int $recursionLevel
  166. * @return \Magento\Framework\Data\Tree\Node
  167. */
  168. public function loadChildren($recursionLevel = 0)
  169. {
  170. $this->_tree->load($this, $recursionLevel);
  171. return $this;
  172. }
  173. /**
  174. * Retrieve node children collection
  175. *
  176. * @return Collection
  177. */
  178. public function getChildren()
  179. {
  180. return $this->_childNodes;
  181. }
  182. /**
  183. * @param array $nodes
  184. * @return array
  185. */
  186. public function getAllChildNodes(&$nodes = [])
  187. {
  188. foreach ($this->_childNodes as $node) {
  189. $nodes[$node->getId()] = $node;
  190. $node->getAllChildNodes($nodes);
  191. }
  192. return $nodes;
  193. }
  194. /**
  195. * @return mixed
  196. */
  197. public function getLastChild()
  198. {
  199. return $this->_childNodes->lastNode();
  200. }
  201. /**
  202. * Add child node
  203. *
  204. * @param Node $node
  205. * @return Node
  206. */
  207. public function addChild($node)
  208. {
  209. $this->_childNodes->add($node);
  210. return $this;
  211. }
  212. /**
  213. * @param Node $prevNode
  214. * @return $this
  215. */
  216. public function appendChild($prevNode = null)
  217. {
  218. $this->_tree->appendChild($this, $prevNode);
  219. return $this;
  220. }
  221. /**
  222. * @param Node $parentNode
  223. * @param Node $prevNode
  224. * @return $this
  225. */
  226. public function moveTo($parentNode, $prevNode = null)
  227. {
  228. $this->_tree->moveNodeTo($this, $parentNode, $prevNode);
  229. return $this;
  230. }
  231. /**
  232. * @param Node $parentNode
  233. * @param Node $prevNode
  234. * @return $this
  235. */
  236. public function copyTo($parentNode, $prevNode = null)
  237. {
  238. $this->_tree->copyNodeTo($this, $parentNode, $prevNode);
  239. return $this;
  240. }
  241. /**
  242. * @param Node $childNode
  243. * @return $this
  244. */
  245. public function removeChild($childNode)
  246. {
  247. $this->_childNodes->delete($childNode);
  248. return $this;
  249. }
  250. /**
  251. * @param array $prevNodes
  252. * @return array
  253. */
  254. public function getPath(&$prevNodes = [])
  255. {
  256. if ($this->_parent) {
  257. $prevNodes[] = $this;
  258. $this->_parent->getPath($prevNodes);
  259. }
  260. return $prevNodes;
  261. }
  262. /**
  263. * @return mixed
  264. */
  265. public function getIsActive()
  266. {
  267. return $this->_getData('is_active');
  268. }
  269. /**
  270. * @return mixed
  271. */
  272. public function getName()
  273. {
  274. return $this->_getData('name');
  275. }
  276. }