SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/TreenodePathMatcher.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. /**
  17. * @package Tree
  18. **/
  19.  
  20. /**
  21. * Matches a node which has a specified path.
  22. * Path can be specified with either slash (/) or backslash (\)
  23. **/
  24. class TreenodePathMatcher implements TreenodeMatcher
  25. {
  26. private $check_path;
  27. private $curr_depth;
  28.  
  29. /**
  30.   * Constructor
  31.   *
  32.   * @param string $path The path to search
  33.   **/
  34. public function __construct($path)
  35. {
  36. $path = strtolower($path);
  37.  
  38. $this->check_path = preg_split('![/\\\\]!', $path);
  39. $this->curr_depth = 0;
  40. }
  41.  
  42.  
  43. /**
  44.   * Returns true if the children of the specified node should be searched, false otherwise.
  45.   *
  46.   * @param TreeNode $node The treenode which is about to be descended into
  47.   * @return True descending should proceed, false otherwise
  48.   **/
  49. public function descend($node)
  50. {
  51. if ($node->isRoot()) {
  52. $this->curr_depth = 0;
  53. return true;
  54. }
  55.  
  56. if ($this->check_path[$this->curr_depth] == strtolower($node->getUrlName())) {
  57. $this->curr_depth++;
  58. return true;
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. /**
  65.   * Does the match
  66.   *
  67.   * @param TreeNode $node The treenode to do matching against
  68.   * @return True if the node matches, false otherwise
  69.   **/
  70. public function match($node)
  71. {
  72. if ($node->isRoot()) {
  73. $this->curr_depth = 0;
  74. return false;
  75. }
  76.  
  77. if ($this->curr_depth + 1 != count($this->check_path)) return false;
  78.  
  79. if ($this->check_path[$this->curr_depth] == strtolower($node->getUrlName())) {
  80. return true;
  81. }
  82.  
  83. return false;
  84. }
  85.  
  86. /**
  87.   * Called after children have been processed
  88.   *
  89.   * @param TreeNode $node The treenode which has just ascended.
  90.   **/
  91. public function ascend ($node) {
  92. $this->curr_depth--;
  93. }
  94. }
  95.  
  96.  
  97.