|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/template package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Template\Utils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Text Utilities class |
|
14
|
|
|
* |
|
15
|
|
|
* @package Slick\Template\Utils |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class Text |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Truncates the value string with the provided length |
|
22
|
|
|
* |
|
23
|
|
|
* If the string length is lower the the desired truncate length |
|
24
|
|
|
* the value is returned intact. |
|
25
|
|
|
* |
|
26
|
|
|
* If the string is bigger then the provided length then it will |
|
27
|
|
|
* be cut and the a terminator will be added to the end of the string. |
|
28
|
|
|
* |
|
29
|
|
|
* If you specify $preserve arg as true the world will be preserved |
|
30
|
|
|
* when cutting the string and the cut will occur on the next available |
|
31
|
|
|
* space character counting from the provided length. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $value |
|
34
|
|
|
* @param int $length |
|
35
|
|
|
* @param string $terminator |
|
36
|
|
|
* @param bool $preserve |
|
37
|
|
|
* |
|
38
|
|
|
* @return string The truncate string |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function truncate( |
|
41
|
|
|
$value, $length = 80, $terminator = "\n", $preserve = false |
|
42
|
|
|
) { |
|
43
|
|
|
$length = $preserve |
|
44
|
|
|
? static::preserveBreakpoint($value, $length) |
|
|
|
|
|
|
45
|
|
|
: $length; |
|
46
|
|
|
if (mb_strlen($value) > $length) { |
|
47
|
|
|
$value = rtrim(mb_substr($value, 0, $length)). |
|
48
|
|
|
$terminator; |
|
49
|
|
|
} |
|
50
|
|
|
return $value; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Wraps a string to a given number of characters using a string |
|
55
|
|
|
* break character. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $value The input string. |
|
58
|
|
|
* @param int $length The number of characters at which the string |
|
59
|
|
|
* will be wrapped. |
|
60
|
|
|
* @param string $break The line is broken using the optional |
|
61
|
|
|
* break parameter. |
|
62
|
|
|
* @param bool $cut If the cut is set to TRUE, the string is always |
|
63
|
|
|
* wrapped at or before the specified width. So if |
|
64
|
|
|
* you have a word that is larger than the given |
|
65
|
|
|
* width, it is broken apart. When FALSE the function |
|
66
|
|
|
* does not split the word even if the width is |
|
67
|
|
|
* smaller than the word width. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string Returns the given string wrapped at the specified length. |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function wordwrap( |
|
72
|
|
|
$value, $length = 75, $break = "\n", $cut = false |
|
73
|
|
|
) { |
|
74
|
|
|
return wordwrap($value, $length, $break, $cut); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Check truncate length to avoid split a word |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $value |
|
81
|
|
|
* @param int $length |
|
82
|
|
|
* |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
|
|
private static function preserveBreakpoint($value, $length) |
|
86
|
|
|
{ |
|
87
|
|
|
if (strlen($value) <= $length) { |
|
88
|
|
|
return $length; |
|
89
|
|
|
} |
|
90
|
|
|
$breakpoint = mb_strpos($value, ' ', $length); |
|
91
|
|
|
$length = $breakpoint; |
|
92
|
|
|
if (false === $breakpoint) { |
|
93
|
|
|
$length = mb_strlen($value); |
|
94
|
|
|
} |
|
95
|
|
|
return $length; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: