1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Markdown; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Text Helpers. |
19
|
|
|
*/ |
20
|
|
|
class TextHelper extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* HTML escaping. |
24
|
|
|
* |
25
|
|
|
* @param string $value Value to escape |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function e($value) |
30
|
|
|
{ |
31
|
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Markdown transformation. |
36
|
|
|
* |
37
|
|
|
* @param string $text |
38
|
|
|
* @param bool $isPublicLink |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function markdown($text, $isPublicLink = false) |
43
|
|
|
{ |
44
|
|
|
$parser = new Markdown($this->container, $isPublicLink); |
45
|
|
|
$parser->setMarkupEscaped(MARKDOWN_ESCAPE_HTML); |
46
|
|
|
|
47
|
|
|
return $parser->text($text); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Escape Markdown text that need to be stored in HTML attribute. |
52
|
|
|
* |
53
|
|
|
* @param string $text |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function markdownAttribute($text) |
58
|
|
|
{ |
59
|
|
|
return htmlentities($this->markdown($text), ENT_QUOTES, 'UTF-8'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Format a file size. |
64
|
|
|
* |
65
|
|
|
* @param int $size Size in bytes |
66
|
|
|
* @param int $precision Precision |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function bytes($size, $precision = 2) |
71
|
|
|
{ |
72
|
|
|
$base = log($size) / log(1024); |
73
|
|
|
$suffixes = ['', 'k', 'M', 'G', 'T']; |
74
|
|
|
|
75
|
|
|
return round(pow(1024, $base - floor($base)), $precision).$suffixes[(int) floor($base)]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the number of bytes from PHP size. |
80
|
|
|
* |
81
|
|
|
* @param int $val PHP size (example: 2M) |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function phpToBytes($val) |
86
|
|
|
{ |
87
|
|
|
$val = trim($val); |
88
|
|
|
$last = strtolower($val[strlen($val) - 1]); |
89
|
|
|
|
90
|
|
|
switch ($last) { |
91
|
|
|
case 'g': |
|
|
|
|
92
|
|
|
$val *= 1024; |
93
|
|
|
case 'm': |
|
|
|
|
94
|
|
|
$val *= 1024; |
95
|
|
|
case 'k': |
96
|
|
|
$val *= 1024; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $val; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return true if needle is contained in the haystack. |
104
|
|
|
* |
105
|
|
|
* @param string $haystack Haystack |
106
|
|
|
* @param string $needle Needle |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function contains($haystack, $needle) |
111
|
|
|
{ |
112
|
|
|
return strpos($haystack, $needle) !== false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return a value from a dictionary. |
117
|
|
|
* |
118
|
|
|
* @param mixed $id Key |
119
|
|
|
* @param array $listing Dictionary |
120
|
|
|
* @param string $default_value Value displayed when the key doesn't exists |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function in($id, array $listing, $default_value = '?') |
125
|
|
|
{ |
126
|
|
|
if (isset($listing[$id])) { |
127
|
|
|
return $this->helper->text->e($listing[$id]); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $default_value; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|