SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/TreenodeRegexMatcher.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 specific key containing a specific value
  22. **/
  23. class TreenodeRegexMatcher implements TreenodeMatcher
  24. {
  25. private $key;
  26. private $regex;
  27. private $flags;
  28.  
  29.  
  30. /**
  31.   * Constructor
  32.   *
  33.   * @param string $key The key to search
  34.   * @param mixed $regex The regex to search with. Should not contain leading or trailing slashes.
  35.   * @param string $flags The regex flags to use. Defaults to 'i' (case-insensitive).
  36.   **/
  37. public function __construct($key, $regex, $flags = 'i')
  38. {
  39. $this->key = $key;
  40. $this->regex = $regex;
  41. $this->flags = $flags;
  42. }
  43.  
  44.  
  45. /**
  46.   * Does the match
  47.   *
  48.   * @param TreeNode $node The treenode to do matching against
  49.   * @return True if the node matches, false otherwise
  50.   **/
  51. public function match($node)
  52. {
  53. $search = '/' . str_replace('/', '\/', $this->regex) . '/' . $this->flags;
  54.  
  55. if (preg_match($search, $node[$this->key])) return true;
  56. return false;
  57. }
  58.  
  59. /**
  60.   * Returns true if the children of the specified node should be searched, false otherwise.
  61.   *
  62.   * @param TreeNode $node The treenode which is about to be descended into
  63.   * @return True descending should proceed, false otherwise
  64.   **/
  65. public function descend($node)
  66. {
  67. return true;
  68. }
  69.  
  70. /**
  71.   * Called after children have been processed
  72.   *
  73.   * @param TreeNode $node The treenode which has just ascended.
  74.   **/
  75. public function ascend ($node) {}
  76. }
  77.  
  78.  
  79.