PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/downloader/lib/Mage/Connect/Structures/Node.php

https://bitbucket.org/acidel/buykoala
PHP | 257 lines | 180 code | 8 blank | 69 comment | 2 complexity | 111ff5bc7e98ec96fc9f98cc74891468 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Connect
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Connect_Structures_Node
  27. {
  28. protected $_data = null;
  29. protected $_metadata = array();
  30. protected $_arcs = array();
  31. protected $_graph = null;
  32. /**
  33. * Node graph getter
  34. *
  35. * @return Mage_Connect_Structures_Graph
  36. */
  37. public function &getGraph()
  38. {
  39. return $this->_graph;
  40. }
  41. /**
  42. *
  43. * Node graph setter.
  44. * This method should not be called directly.
  45. * Use Graph::addNode instead.
  46. *
  47. * @param $graph
  48. */
  49. public function setGraph(&$graph)
  50. {
  51. $this->_graph =& $graph;
  52. }
  53. /**
  54. *
  55. * Node data getter.
  56. *
  57. * Each graph node can contain a reference to one variable. This is the getter for that reference.
  58. *
  59. * @return mixed Data stored in node
  60. * @access public
  61. */
  62. public function &getData()
  63. {
  64. return $this->_data;
  65. }
  66. /**
  67. * Node data setter
  68. *
  69. * Each graph node can contain a reference to one variable. This is the setter for that reference.
  70. *
  71. * @return mixed Data to store in node
  72. */
  73. public function setData($data)
  74. {
  75. $this->_data =& $data;
  76. }
  77. /**
  78. *
  79. * Test for existence of metadata under a given key.
  80. *
  81. * @param string Key to test
  82. * @return boolean
  83. * @access public
  84. */
  85. public function metadataKeyExists($key)
  86. {
  87. return array_key_exists($key, $this->_metadata);
  88. }
  89. /**
  90. *
  91. * Get node metadata
  92. *
  93. * @param string $key
  94. * @param boolean $nullIfNonexistent (defaults to false).
  95. * @return mixed
  96. */
  97. public function & getMetadata($key, $nullIfNonexistent = false)
  98. {
  99. if (array_key_exists($key, $this->_metadata)) {
  100. return $this->_metadata[$key];
  101. } elseif ($nullIfNonexistent) {
  102. $a = null;
  103. return $a;
  104. } else {
  105. throw new Exception(__METHOD__." : requested key doesn't exist: {$key}");
  106. }
  107. }
  108. /**
  109. *
  110. * Delete metadata by key
  111. *
  112. * @param string Key
  113. */
  114. public function unsetMetadata($key)
  115. {
  116. if (array_key_exists($key, $this->_metadata)) {
  117. unset($this->_metadata[$key]);
  118. }
  119. }
  120. /**
  121. *
  122. * Node metadata setter
  123. *
  124. * Each graph node can contain multiple 'metadata' entries, each stored under a different key, as in an
  125. * associative array or in a dictionary. This method stores data under the given key. If the key already exists,
  126. * previously stored data is discarded.
  127. *
  128. * @param string $key
  129. * @param mixed $data
  130. */
  131. public function setMetadata($key, $data)
  132. {
  133. $this->_metadata[$key] =& $data;
  134. }
  135. protected function _connectTo(&$destinationNode)
  136. {
  137. $this->_arcs[] =& $destinationNode;
  138. }
  139. /**
  140. * Connect this node to another one.
  141. * If the graph is not directed, the reverse arc, connecting $destinationNode to $this is also created.
  142. * @param Structures_Graph Node to connect to
  143. */
  144. public function connectTo(&$destinationNode)
  145. {
  146. $class = get_class($this);
  147. if(!$destinationNode instanceof $class) {
  148. throw new Exception(__METHOD__." : argument should be instance of {$class}");
  149. }
  150. // Nodes must already be in graphs to be connected
  151. if ($this->_graph == null) {
  152. throw new Exception(__METHOD__." : tried to connect to null graph");
  153. }
  154. if ($destinationNode->getGraph() == null) {
  155. throw new Exception(__METHOD__." : tried to connect to node that is not connected to any graph");
  156. }
  157. // Connect here
  158. $this->_connectTo($destinationNode);
  159. // If graph is undirected, connect back
  160. if (!$this->_graph->isDirected()) {
  161. $destinationNode->_connectTo($this);
  162. }
  163. }
  164. /**
  165. * Return nodes connected to this one.
  166. * @return array
  167. */
  168. public function getNeighbours()
  169. {
  170. return $this->_arcs;
  171. }
  172. /**
  173. * Test wether this node has an arc to the target node
  174. * Returns true if the two nodes are connected
  175. * @return boolean
  176. */
  177. public function connectsTo(&$target)
  178. {
  179. $arcKeys = array_keys($this->_arcs);
  180. foreach($arcKeys as $key) {
  181. $arc =& $this->_arcs[$key];
  182. if ($target === $arc) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. /**
  189. * Calculate the in degree of the node.
  190. *
  191. * The indegree for a node is the number of arcs
  192. * entering the node.
  193. *
  194. * For non directed graphs:
  195. * always outdegree = indegree.
  196. *
  197. * @return int
  198. */
  199. public function inDegree()
  200. {
  201. $result = 0;
  202. if ($this->_graph == null) {
  203. return $result;
  204. }
  205. if (!$this->_graph->isDirected()) {
  206. return $this->outDegree();
  207. }
  208. $graphNodes =& $this->_graph->getNodes();
  209. foreach (array_keys($graphNodes) as $key) {
  210. if ($graphNodes[$key]->connectsTo($this)) {
  211. $result++;
  212. }
  213. }
  214. return $result;
  215. }
  216. /**
  217. * Calculate the out degree of the node.
  218. *
  219. * The outdegree for a node is the number of arcs exiting the node.
  220. * For non directed graphs:
  221. * always outdegree = indegree.
  222. *
  223. * @return int
  224. */
  225. public function outDegree()
  226. {
  227. if ($this->_graph == null) {
  228. return 0;
  229. }
  230. return count($this->_arcs);
  231. }
  232. }