FOSSology  4.4.0
Open Source License Compliance by Open Source Software
HighlightState.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\View;
10 
11 
14 
16 {
17  const PLACEHOLDER = " # ";
18 
20  private $elementStack;
22  private $highlightRenderer;
24  private $anchorDrawn;
26  private $insertBacklink;
27 
28  public function __construct(HighlightRenderer $highlightRenderer, $insertBacklink = false)
29  {
30  $this->highlightRenderer = $highlightRenderer;
31  $this->insertBacklink = $insertBacklink;
32  $this->elementStack = array();
33  }
34 
38  public function push(SplitPosition $splitPosition)
39  {
40  $this->elementStack[] = $splitPosition;
41  }
42 
46  public function pop()
47  {
48  return array_pop($this->elementStack);
49  }
50 
54  public function getElementStack()
55  {
56  return $this->elementStack;
57  }
58 
62  public function processSplitEntries($entries)
63  {
64  foreach ($entries as $entry) {
65  switch ($entry->getAction()) {
66  case SplitPosition::START:
67  $this->push($entry);
68  $this->checkForAnchor($entry);
69  break;
70  case SplitPosition::END:
71  $this->pop();
72  break;
73  }
74  }
75  }
76 
81  public function insertElements($entries, PagedResult $result)
82  {
83  foreach ($entries as $entry) {
84  switch ($entry->getAction()) {
85  case SplitPosition::START:
86  $this->push($entry);
87  $result->appendMetaText($this->startSpan($entry));
88  break;
89  case SplitPosition::ATOM:
90  $result->appendMetaText($this->startSpan($entry));
91  $result->appendMetaText(
92  self::PLACEHOLDER . $this->highlightRenderer->createSpanEnd($entry));
93  break;
94 
95  case SplitPosition::END:
96  $this->pop();
97  $result->appendMetaText(
98  $this->highlightRenderer->createSpanEnd($entry));
99  break;
100  }
101  }
102  }
103 
107  public function closeOpenElements(PagedResult $result)
108  {
109  foreach ($this->elementStack as $splitPosition) {
110  $result->appendMetaText(
111  $this->highlightRenderer->createSpanEnd($splitPosition));
112  }
113  }
114 
118  public function openExistingElements(PagedResult $result)
119  {
120  foreach ($this->elementStack as $entry) {
121  $result->appendMetaText($this->highlightRenderer->createSpanStart($entry));
122  }
123  }
124 
129  protected function startSpan(SplitPosition $entry)
130  {
131  $anchorText = $this->checkForAnchor($entry)
132  ? "<a id=\"highlight\"" . ($this->insertBacklink ? " href=\"#top\">&nbsp;&#8593;&nbsp;" : ">") . "</a>"
133  : "";
134 
135  return $anchorText . $this->highlightRenderer->createSpanStart($entry);
136  }
137 
142  protected function checkForAnchor(SplitPosition $entry)
143  {
144  $shouldShowAnchor = !$this->anchorDrawn && $entry->getHighlight()->getType() != Highlight::KEYWORD;
145  if ($shouldShowAnchor) {
146  $this->anchorDrawn = true;
147  }
148  return $shouldShowAnchor;
149  }
150 }
openExistingElements(PagedResult $result)
checkForAnchor(SplitPosition $entry)
closeOpenElements(PagedResult $result)
push(SplitPosition $splitPosition)
insertElements($entries, PagedResult $result)