1
|
|
|
<?php |
2
|
|
|
namespace nochso\Omni; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* EOL detects, converts and returns information about line-endings. |
6
|
|
|
*/ |
7
|
|
|
final class EOL { |
8
|
|
|
/** |
9
|
|
|
* Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80. |
10
|
|
|
*/ |
11
|
|
|
const EOL_CR = "\r"; |
12
|
|
|
/** |
13
|
|
|
* Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS. |
14
|
|
|
*/ |
15
|
|
|
const EOL_LF = "\n"; |
16
|
|
|
/** |
17
|
|
|
* Carriage return/Line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS. |
18
|
|
|
*/ |
19
|
|
|
const EOL_CR_LF = "\r\n"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Note that EOL::detect depends on this order. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private static $eols = [ |
27
|
|
|
"\r\n" => [ |
28
|
|
|
'CR+LF', |
29
|
|
|
'Carriage return/line feed: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS', |
30
|
|
|
], |
31
|
|
|
"\n" => ['LF', 'Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS'], |
32
|
|
|
"\r" => [ |
33
|
|
|
'CR', |
34
|
|
|
'Carriage return: Mac OS <=v9, OS-9, Apple II, Commodore 8-bit, BBC Acorn, TRS-80', |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
private $eol; |
38
|
|
|
private $name; |
39
|
|
|
private $description; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $eol Line ending. See the EOL_* class constants. |
43
|
|
|
*/ |
44
|
|
|
public function __construct($eol) { |
45
|
|
|
$this->eol = $eol; |
46
|
|
|
if (!isset(self::$eols[$eol])) { |
47
|
|
|
throw new \DomainException(sprintf('Unknown line ending: %s', Strings::escapeControlChars($eol))); |
48
|
|
|
} |
49
|
|
|
$initEol = self::$eols[$eol]; |
50
|
|
|
$this->name = $initEol[0]; |
51
|
|
|
$this->description = $initEol[1]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* __toString casts to/returns the raw line ending string. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function __toString() { |
60
|
|
|
return $this->eol; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* getName of line ending, e.g. `LF`. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getName() { |
69
|
|
|
return $this->name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* getDescription of line ending, e.g. `Line feed: Unix, Unix-like, Multics, BeOS, Amiga, RISC OS`. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getDescription() { |
78
|
|
|
return $this->description; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Apply this EOL style to a string. |
83
|
|
|
* |
84
|
|
|
* @param string $input Input to be converted. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function apply($input) { |
89
|
|
|
return (string) Multiline::create($input)->setEol($this); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Detect the EOL style of a string and return an EOL representation. |
94
|
|
|
* |
95
|
|
|
* @param string $input Input string to be analyzed. |
96
|
|
|
* |
97
|
|
|
* @throws \RuntimeException Thrown on failure to detect any line ending. |
98
|
|
|
* |
99
|
|
|
* @return \nochso\Omni\EOL |
100
|
|
|
*/ |
101
|
|
|
public static function detect($input) { |
102
|
|
|
$maxEol = Strings::getMostFrequentNeedle($input, array_keys(self::$eols)); |
103
|
|
|
$missingEOL = $maxEol === null; |
104
|
|
|
if ($missingEOL) { |
105
|
|
|
throw new \RuntimeException('Could not detect end-of-line. There are no EOL chars in the input string.'); |
106
|
|
|
} |
107
|
|
|
return new self($maxEol); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* DetectDefault falls back to a default EOL style on failure. |
112
|
|
|
* |
113
|
|
|
* @param string $input Input string to be analyzed. |
114
|
|
|
* @param string $default Optional, defaults to "\n". The default line ending to use when $strict is false. See the `EOL::EOL_*` constants. |
115
|
|
|
* |
116
|
|
|
* @return \nochso\Omni\EOL |
117
|
|
|
*/ |
118
|
|
|
public static function detectDefault($input, $default = self::EOL_LF) { |
119
|
|
|
try { |
120
|
|
|
$eol = self::detect($input); |
121
|
|
|
} catch (\RuntimeException $e) { |
122
|
|
|
$eol = new self($default); |
123
|
|
|
} |
124
|
|
|
return $eol; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|