Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
AddEllipsis | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
__invoke | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | /** |
4 | * "Add ellipsis" view helper |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package View_Helpers |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\View\Helper\Root; |
31 | |
32 | use Laminas\View\Helper\AbstractHelper; |
33 | |
34 | use function strlen; |
35 | |
36 | /** |
37 | * "Add ellipsis" view helper |
38 | * |
39 | * @category VuFind |
40 | * @package View_Helpers |
41 | * @author Demian Katz <demian.katz@villanova.edu> |
42 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
43 | * @link https://vufind.org Main Site |
44 | */ |
45 | class AddEllipsis extends AbstractHelper |
46 | { |
47 | /** |
48 | * Adds "..." to the beginning and/or end of a highlighted phrase when |
49 | * incomplete text is detected. |
50 | * |
51 | * @param string $highlighted Highlighted, possibly abbreviated string |
52 | * @param mixed $fullString Full, non-highlighted text |
53 | * |
54 | * @return string Highlighted string with ellipsis added |
55 | */ |
56 | public function __invoke($highlighted, $fullString) |
57 | { |
58 | // Remove highlighting markers from the string so we can perform a clean |
59 | // comparison: |
60 | $dehighlighted = str_replace( |
61 | ['{{{{START_HILITE}}}}', '{{{{END_HILITE}}}}'], |
62 | '', |
63 | $highlighted |
64 | ); |
65 | |
66 | // If the dehighlighted string is shorter than the full string, we need |
67 | // to figure out where things changed: |
68 | if (strlen($dehighlighted) < strlen($fullString)) { |
69 | // If we can splice the highlighted text into the unhighlighted text, |
70 | // let's do so! |
71 | $pos = strpos($fullString, $dehighlighted); |
72 | if ($pos !== false) { |
73 | // Attach the highlighted snippet to the unhighlighted preceding text |
74 | $title = substr($fullString, 0, $pos) . $highlighted; |
75 | // If the overall title is relatively short, attach the rest; |
76 | // otherwise, unless we already have the full string, add ellipses. |
77 | if (strlen($fullString) < 160) { |
78 | $title .= substr($fullString, $pos + strlen($dehighlighted)); |
79 | } elseif ($pos + strlen($dehighlighted) < strlen($fullString)) { |
80 | $title = trim($title) . '...'; |
81 | } |
82 | return $title; |
83 | } |
84 | |
85 | // If the first five characters don't match chances are something was cut |
86 | // from the front: |
87 | if (strncmp($dehighlighted, $fullString, 5) !== 0) { |
88 | $highlighted = '...' . $highlighted; |
89 | } |
90 | |
91 | // If the last five characters don't match, chances are something was cut |
92 | // from the end: |
93 | if (substr($dehighlighted, -5) != substr($fullString, -5)) { |
94 | $highlighted .= '...'; |
95 | } |
96 | } |
97 | |
98 | // Send back our augmented string: |
99 | return $highlighted; |
100 | } |
101 | } |