SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/TreenodePageAttrMatcher.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 matching a specific value
  22. **/
  23. class TreenodePageAttrMatcher implements TreenodeMatcher
  24. {
  25. private $page_ids;
  26.  
  27.  
  28. /**
  29.   * Constructor
  30.   *
  31.   * @param string $key The attr name to search.
  32.   * @param mixed $value The attr value to search for.
  33.   * Can be null to match all pages which contain the attribute, irrespective of the value.
  34.   **/
  35. public function __construct($attr_name, $attr_value = null)
  36. {
  37. $this->page_ids = Page::pagesWithAttr($attr_name, $attr_value);
  38. }
  39.  
  40.  
  41. /**
  42.   * Does the match
  43.   *
  44.   * @param TreeNode $node The treenode to do matching against
  45.   * @return True if the node matches, false otherwise
  46.   **/
  47. public function match($node)
  48. {
  49. if (in_array($node['id'], $this->page_ids)) return true;
  50. return false;
  51. }
  52.  
  53. /**
  54.   * Returns true if the children of the specified node should be searched, false otherwise.
  55.   *
  56.   * @param TreeNode $node The treenode which is about to be descended into
  57.   * @return True descending should proceed, false otherwise
  58.   **/
  59. public function descend($node)
  60. {
  61. return true;
  62. }
  63.  
  64. /**
  65.   * Called after children have been processed
  66.   *
  67.   * @param TreeNode $node The treenode which has just ascended.
  68.   **/
  69. public function ascend ($node) {}
  70. }
  71.  
  72.  
  73.