|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Typography. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2017 Peter Putzer. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License along |
|
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20
|
|
|
* |
|
21
|
|
|
* *** |
|
22
|
|
|
* |
|
23
|
|
|
* @package mundschenk-at/php-typography |
|
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace PHP_Typography\Fixes\Node_Fixes; |
|
28
|
|
|
|
|
29
|
|
|
use PHP_Typography\Fixes\Node_Fix; |
|
30
|
|
|
use PHP_Typography\Settings; |
|
31
|
|
|
use PHP_Typography\Strings; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* All fixes that apply to textnodes should implement this interface. |
|
35
|
|
|
* |
|
36
|
|
|
* @author Peter Putzer <[email protected]> |
|
37
|
|
|
* |
|
38
|
|
|
* @since 5.0.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract class Abstract_Node_Fix implements Node_Fix { |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Is this fix compatible with feeds? |
|
44
|
|
|
* |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
private $feed_compatible; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Creates a new fix instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param bool $feed_compatible Optional. Default false. |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function __construct( $feed_compatible = false ) { |
|
55
|
1 |
|
$this->feed_compatible = $feed_compatible; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Apply the fix to a given textnode. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \DOMText $textnode Required. |
|
62
|
|
|
* @param Settings $settings Required. |
|
63
|
|
|
* @param bool $is_title Optional. Default false. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract public function apply( \DOMText $textnode, Settings $settings, $is_title = false ); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Determines whether the fix should be applied to (RSS) feeds. |
|
71
|
|
|
* |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function feed_compatible() { |
|
75
|
1 |
|
return $this->feed_compatible; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Remove adjacent characters from given string. |
|
80
|
|
|
* |
|
81
|
|
|
* @since 4.2.2 |
|
82
|
|
|
* @since 5.1.3 $prev_char and $next_char replaced with $prev_length and $next_length |
|
83
|
|
|
* to support multi-characters replacements. |
|
84
|
|
|
* @since 6.0.0 New required parameters for strlen() and substr() added. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $string The string. |
|
87
|
|
|
* @param callable $strlen A strlen()-type function. |
|
88
|
|
|
* @param callable $substr A substr()-type function. |
|
89
|
|
|
* @param int $prev_length Optional. Default 0. The number of characters to remove at the beginning. |
|
90
|
|
|
* @param int $next_length Optional. Default 0. The number of characters to remove at the end. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string The string without the characters from adjacent nodes. |
|
93
|
|
|
*/ |
|
94
|
3 |
|
protected static function remove_adjacent_characters( $string, callable $strlen, callable $substr, $prev_length = 0, $next_length = 0 ) { |
|
95
|
|
|
// Remove previous character. |
|
96
|
3 |
|
if ( $prev_length > 0 ) { |
|
97
|
3 |
|
$string = $substr( $string, $prev_length, $strlen( $string ) ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Remove next character. |
|
101
|
3 |
|
if ( $next_length > 0 ) { |
|
102
|
2 |
|
$string = $substr( $string, 0, $strlen( $string ) - $next_length ); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
3 |
|
return $string; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|