Node.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Protocol\Node;
  36. use Hoa\Consistency;
  37. use Hoa\Protocol;
  38. /**
  39. * Class \Hoa\Protocol\Node\Node.
  40. *
  41. * Abstract class for all `hoa://`'s nodes.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Node implements \ArrayAccess, \IteratorAggregate
  47. {
  48. /**
  49. * Node's name.
  50. *
  51. * @var string
  52. */
  53. protected $_name = null;
  54. /**
  55. * Path for the `reach` method.
  56. *
  57. * @var string
  58. */
  59. protected $_reach = null;
  60. /**
  61. * Children of the node.
  62. *
  63. * @var array
  64. */
  65. private $_children = [];
  66. /**
  67. * Construct a protocol's node.
  68. * If it is not a data object (i.e. if it does not extend this class to
  69. * overload the `$_name` attribute), we can set the `$_name` attribute
  70. * dynamically. This is useful to create a node on-the-fly.
  71. *
  72. * @param string $name Node's name.
  73. * @param string $reach Path for the `reach` method.
  74. * @param array $children Node's children.
  75. */
  76. public function __construct($name = null, $reach = null, array $children = [])
  77. {
  78. if (null !== $name) {
  79. $this->_name = $name;
  80. }
  81. if (null !== $reach) {
  82. $this->_reach = $reach;
  83. }
  84. foreach ($children as $child) {
  85. $this[] = $child;
  86. }
  87. return;
  88. }
  89. /**
  90. * Add a node.
  91. *
  92. * @param string $name Node's name. If null, will be
  93. * set to name of `$node`.
  94. * @param \Hoa\Protocol\Protocol $node Node to add.
  95. * @return \Hoa\Protocol\Protocol
  96. * @throws \Hoa\Protocol\Exception
  97. */
  98. public function offsetSet($name, $node)
  99. {
  100. if (!($node instanceof self)) {
  101. throw new Protocol\Exception(
  102. 'Protocol node must extend %s.',
  103. 0,
  104. __CLASS__
  105. );
  106. }
  107. if (empty($name)) {
  108. $name = $node->getName();
  109. }
  110. if (empty($name)) {
  111. throw new Protocol\Exception(
  112. 'Cannot add a node to the `hoa://` protocol without a name.',
  113. 1
  114. );
  115. }
  116. $this->_children[$name] = $node;
  117. return;
  118. }
  119. /**
  120. * Get a specific node.
  121. *
  122. * @param string $name Node's name.
  123. * @return \Hoa\Protocol\Protocol
  124. * @throws \Hoa\Protocol\Exception
  125. */
  126. public function offsetGet($name)
  127. {
  128. if (!isset($this[$name])) {
  129. throw new Protocol\Exception(
  130. 'Node %s does not exist.',
  131. 2,
  132. $name
  133. );
  134. }
  135. return $this->_children[$name];
  136. }
  137. /**
  138. * Check if a node exists.
  139. *
  140. * @param string $name Node's name.
  141. * @return bool
  142. */
  143. public function offsetExists($name)
  144. {
  145. return true === array_key_exists($name, $this->_children);
  146. }
  147. /**
  148. * Remove a node.
  149. *
  150. * @param string $name Node's name to remove.
  151. * @return void
  152. */
  153. public function offsetUnset($name)
  154. {
  155. unset($this->_children[$name]);
  156. return;
  157. }
  158. /**
  159. * Resolve a path, i.e. iterate the nodes tree and reach the queue of
  160. * the path.
  161. *
  162. * @param string $path Path to resolve.
  163. * @param array &$accumulator Combination of all possibles paths.
  164. * @param string $id ID.
  165. * @return mixed
  166. */
  167. protected function _resolve($path, &$accumulator, $id = null)
  168. {
  169. if (substr($path, 0, 6) == 'hoa://') {
  170. $path = substr($path, 6);
  171. }
  172. if (empty($path)) {
  173. return null;
  174. }
  175. if (null === $accumulator) {
  176. $accumulator = [];
  177. $posId = strpos($path, '#');
  178. if (false !== $posId) {
  179. $id = substr($path, $posId + 1);
  180. $path = substr($path, 0, $posId);
  181. } else {
  182. $id = null;
  183. }
  184. }
  185. $path = trim($path, '/');
  186. $pos = strpos($path, '/');
  187. if (false !== $pos) {
  188. $next = substr($path, 0, $pos);
  189. } else {
  190. $next = $path;
  191. }
  192. if (isset($this[$next])) {
  193. if (false === $pos) {
  194. if (null === $id) {
  195. $this->_resolveChoice($this[$next]->reach(), $accumulator);
  196. return true;
  197. }
  198. $accumulator = null;
  199. return $this[$next]->reachId($id);
  200. }
  201. $tnext = $this[$next];
  202. $this->_resolveChoice($tnext->reach(), $accumulator);
  203. return $tnext->_resolve(substr($path, $pos + 1), $accumulator, $id);
  204. }
  205. $this->_resolveChoice($this->reach($path), $accumulator);
  206. return true;
  207. }
  208. /**
  209. * Resolve choices, i.e. a reach value has a “;”.
  210. *
  211. * @param string $reach Reach value.
  212. * @param array &$accumulator Combination of all possibles paths.
  213. * @return void
  214. */
  215. protected function _resolveChoice($reach, array &$accumulator)
  216. {
  217. if (empty($accumulator)) {
  218. $accumulator = explode(RS, $reach);
  219. return;
  220. }
  221. if (false === strpos($reach, RS)) {
  222. if (false !== $pos = strrpos($reach, "\r")) {
  223. $reach = substr($reach, $pos + 1);
  224. foreach ($accumulator as &$entry) {
  225. $entry = null;
  226. }
  227. }
  228. foreach ($accumulator as &$entry) {
  229. $entry .= $reach;
  230. }
  231. return;
  232. }
  233. $choices = explode(RS, $reach);
  234. $ref = $accumulator;
  235. $accumulator = [];
  236. foreach ($choices as $choice) {
  237. if (false !== $pos = strrpos($choice, "\r")) {
  238. $choice = substr($choice, $pos + 1);
  239. foreach ($ref as $entry) {
  240. $accumulator[] = $choice;
  241. }
  242. } else {
  243. foreach ($ref as $entry) {
  244. $accumulator[] = $entry . $choice;
  245. }
  246. }
  247. }
  248. unset($ref);
  249. return;
  250. }
  251. /**
  252. * Queue of the node.
  253. * Generic one. Must be overrided in children classes.
  254. *
  255. * @param string $queue Queue of the node (generally a filename,
  256. * with probably a query).
  257. * @return mixed
  258. */
  259. public function reach($queue = null)
  260. {
  261. return empty($queue) ? $this->_reach : $queue;
  262. }
  263. /**
  264. * ID of the component.
  265. * Generic one. Should be overrided in children classes.
  266. *
  267. * @param string $id ID of the component.
  268. * @return mixed
  269. * @throws \Hoa\Protocol\Exception
  270. */
  271. public function reachId($id)
  272. {
  273. throw new Protocol\Exception(
  274. 'The node %s has no ID support (tried to reach #%s).',
  275. 4,
  276. [$this->getName(), $id]
  277. );
  278. }
  279. /**
  280. * Set a new reach value.
  281. *
  282. * @param string $reach Reach value.
  283. * @return string
  284. */
  285. public function setReach($reach)
  286. {
  287. $old = $this->_reach;
  288. $this->_reach = $reach;
  289. return $old;
  290. }
  291. /**
  292. * Get node's name.
  293. *
  294. * @return string
  295. */
  296. public function getName()
  297. {
  298. return $this->_name;
  299. }
  300. /**
  301. * Get reach's root.
  302. *
  303. * @return string
  304. */
  305. protected function getReach()
  306. {
  307. return $this->_reach;
  308. }
  309. /**
  310. * Get an iterator.
  311. *
  312. * @return \ArrayIterator
  313. */
  314. public function getIterator()
  315. {
  316. return new \ArrayIterator($this->_children);
  317. }
  318. /**
  319. * Get root the protocol.
  320. *
  321. * @return \Hoa\Protocol\Protocol
  322. */
  323. public static function getRoot()
  324. {
  325. return Protocol::getInstance();
  326. }
  327. /**
  328. * Print a tree of component.
  329. *
  330. * @return string
  331. */
  332. public function __toString()
  333. {
  334. static $i = 0;
  335. $out = str_repeat(' ', $i) . $this->getName() . "\n";
  336. foreach ($this as $node) {
  337. ++$i;
  338. $out .= $node;
  339. --$i;
  340. }
  341. return $out;
  342. }
  343. }
  344. /**
  345. * Flex entity.
  346. */
  347. Consistency::flexEntity('Hoa\Protocol\Node\Node');