1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see AppUtils\ConvertHelper_EOL} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage ConvertHelper |
7
|
|
|
* @see AppUtils\ConvertHelper_EOL |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Container class for an end of line (EOL) character. |
16
|
|
|
* Used as result when detecting EOL characters in a |
17
|
|
|
* string or file. |
18
|
|
|
* |
19
|
|
|
* @package Application Utils |
20
|
|
|
* @subpackage ConvertHelper |
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @see ConvertHelper::detectEOLCharacter() |
24
|
|
|
*/ |
25
|
|
|
class ConvertHelper_EOL |
26
|
|
|
{ |
27
|
|
|
const TYPE_CRLF = 'CR+LF'; |
28
|
|
|
const TYPE_LFCR = 'LF+CR'; |
29
|
|
|
const TYPE_LF = 'LF'; |
30
|
|
|
const TYPE_CR = 'CR'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $char; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $type; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $description; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array<int,array<string,string>>|NULL |
49
|
|
|
*/ |
50
|
|
|
protected static $eolChars = null; |
51
|
|
|
|
52
|
|
|
public function __construct(string $char, string $type, string $description) |
53
|
|
|
{ |
54
|
|
|
$this->char = $char; |
55
|
|
|
$this->type = $type; |
56
|
|
|
$this->description = $description; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The actual EOL character. |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getCharacter() : string |
64
|
|
|
{ |
65
|
|
|
return $this->char; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* A more detailed, human readable description of the character. |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getDescription() : string |
73
|
|
|
{ |
74
|
|
|
return $this->description; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* The EOL character type, e.g. "CR+LF", "CR"... |
79
|
|
|
* @return string |
80
|
|
|
* |
81
|
|
|
* @see ConvertHelper_EOL::TYPE_CR |
82
|
|
|
* @see ConvertHelper_EOL::TYPE_CRLF |
83
|
|
|
* @see ConvertHelper_EOL::TYPE_LF |
84
|
|
|
* @see ConvertHelper_EOL::TYPE_LFCR |
85
|
|
|
*/ |
86
|
|
|
public function getType() : string |
87
|
|
|
{ |
88
|
|
|
return $this->type; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isCRLF() : bool |
92
|
|
|
{ |
93
|
|
|
return $this->isType(self::TYPE_CRLF); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isCR() : bool |
97
|
|
|
{ |
98
|
|
|
return $this->isType(self::TYPE_CR); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isLF() : bool |
102
|
|
|
{ |
103
|
|
|
return $this->isType(self::TYPE_LF); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isLFCR() : bool |
107
|
|
|
{ |
108
|
|
|
return $this->isType(self::TYPE_LFCR); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function isType(string $type) : bool |
112
|
|
|
{ |
113
|
|
|
return $this->type === $type; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Detects the most used end-of-line character in the subject string. |
118
|
|
|
* |
119
|
|
|
* @param string $subjectString The string to check. |
120
|
|
|
* @return NULL|ConvertHelper_EOL The detected EOL instance, or NULL if none has been detected. |
121
|
|
|
*/ |
122
|
|
|
public static function detect(string $subjectString) : ?ConvertHelper_EOL |
123
|
|
|
{ |
124
|
|
|
if(empty($subjectString)) { |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$max = 0; |
129
|
|
|
$results = array(); |
130
|
|
|
$chars = self::getEOLChars(); |
131
|
|
|
|
132
|
|
|
foreach($chars as $def) |
133
|
|
|
{ |
134
|
|
|
$amount = substr_count($subjectString, $def['char']); |
135
|
|
|
|
136
|
|
|
if($amount > $max) |
137
|
|
|
{ |
138
|
|
|
$max = $amount; |
139
|
|
|
$results[] = $def; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if(empty($results)) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return new ConvertHelper_EOL( |
148
|
|
|
$results[0]['char'], |
149
|
|
|
$results[0]['type'], |
150
|
|
|
$results[0]['description'] |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return array<int,array<string,string>> |
156
|
|
|
*/ |
157
|
|
|
public static function getEOLChars() : array |
158
|
|
|
{ |
159
|
|
|
if(isset(self::$eolChars)) { |
160
|
|
|
return self::$eolChars; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$cr = chr((int)hexdec('0d')); |
164
|
|
|
$lf = chr((int)hexdec('0a')); |
165
|
|
|
|
166
|
|
|
self::$eolChars = array( |
167
|
|
|
array( |
168
|
|
|
'char' => $cr.$lf, |
169
|
|
|
'type' => ConvertHelper_EOL::TYPE_CRLF, |
170
|
|
|
'description' => t('Carriage return followed by a line feed'), |
171
|
|
|
), |
172
|
|
|
array( |
173
|
|
|
'char' => $lf.$cr, |
174
|
|
|
'type' => ConvertHelper_EOL::TYPE_LFCR, |
175
|
|
|
'description' => t('Line feed followed by a carriage return'), |
176
|
|
|
), |
177
|
|
|
array( |
178
|
|
|
'char' => $lf, |
179
|
|
|
'type' => ConvertHelper_EOL::TYPE_LF, |
180
|
|
|
'description' => t('Line feed'), |
181
|
|
|
), |
182
|
|
|
array( |
183
|
|
|
'char' => $cr, |
184
|
|
|
'type' => ConvertHelper_EOL::TYPE_CR, |
185
|
|
|
'description' => t('Carriage Return'), |
186
|
|
|
), |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
return self::$eolChars; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|