41 return self::POTENTIAL_DELIMETERS[0];
51 $this->counts = array_fill_keys(self::POTENTIAL_DELIMETERS, []);
52 $delimiterKeys = array_flip(self::POTENTIAL_DELIMETERS);
55 $this->numberLines = 0;
56 while (($line = $this->
getNextLine()) !==
false && (++$this->numberLines < 1000)) {
63 $splitString = str_split($line, 1);
64 if (is_array($splitString)) {
65 $distribution = array_count_values($splitString);
66 $countLine = array_intersect_key($distribution, $delimiterKeys);
68 foreach (self::POTENTIAL_DELIMETERS as
$delimiter) {
74 public function infer(): ?string
78 $meanSquareDeviations = [];
79 $middleIdx = floor(($this->numberLines - 1) / 2);
81 foreach (self::POTENTIAL_DELIMETERS as
$delimiter) {
85 $median = ($this->numberLines % 2)
87 : ($series[$middleIdx] + $series[$middleIdx + 1]) / 2;
93 $meanSquareDeviations[
$delimiter] = array_reduce(
95 function ($sum, $value) use ($median) {
96 return $sum + ($value - $median) ** 2;
104 foreach (self::POTENTIAL_DELIMETERS as $delimiter) {
105 if (!isset($meanSquareDeviations[$delimiter])) {
109 if ($meanSquareDeviations[$delimiter] < $min) {
126 $enclosure = ($this->escapeCharacter ===
'' ?
'' 127 : (
'(?<!' . preg_quote($this->escapeCharacter,
'/') .
')'))
128 . preg_quote($this->enclosure,
'/');
132 $newLine = fgets($this->fileHandle);
135 if ($newLine ===
false) {
140 $line = $line . $newLine;
147 }
while (preg_match(
'/(' .
$enclosure .
')/', $line ??
'') > 0);
149 return $line ??
false;
__construct($fileHandle, string $escapeCharacter, string $enclosure)
countPotentialDelimiters()
const POTENTIAL_DELIMETERS
countDelimiterValues(string $line, array $delimiterKeys)
getNextLine()
Get the next full line from the file.