@@ -74,68 +74,68 @@ |
||
| 74 | 74 | foreach ($lines as $lineNumber => $line) { |
| 75 | 75 | |
| 76 | 76 | switch ($state) { |
| 77 | - case 'order': |
|
| 78 | - if (!preg_match(self::PATTERN_ORDER, $line)) { |
|
| 79 | - throw new \Exception($this->filename.' is not a proper .srt file. (Expected subtitle order index at line '.$lineNumber.')'); |
|
| 80 | - } |
|
| 81 | - $subtitleIndex = intval($line); |
|
| 82 | - if ($strict && $subtitleOrder !== $subtitleIndex) { |
|
| 83 | - throw new \Exception($this->filename.' is not a proper .srt file. (Invalid subtitle order index: '.$line.' at line '.$lineNumber.')'); |
|
| 84 | - } |
|
| 85 | - $state = 'time'; |
|
| 86 | - break; |
|
| 87 | - |
|
| 88 | - case 'time': |
|
| 89 | - $timeline = explode(' --> ', $line); |
|
| 90 | - if (count($timeline) !== 2) { |
|
| 91 | - throw new \Exception($this->filename." is not a proper .srt file. (Invalid timestamp delimiter at line ".$lineNumber.")"); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $subtitleTimeStart = trim($timeline[0]); |
|
| 95 | - $subtitleTimeEnd = trim($timeline[1]); |
|
| 96 | - if (!preg_match(self::PATTERN_TIMESTAMP, $subtitleTimeStart)) { |
|
| 97 | - throw new \Exception($this->filename.' is not a proper .srt file. (Invalid start timestamp format '.$subtitleTimeStart.' at line '.$lineNumber.')'); |
|
| 98 | - } |
|
| 99 | - if (!preg_match(self::PATTERN_TIMESTAMP, $subtitleTimeEnd)) { |
|
| 100 | - throw new \Exception($this->filename.' is not a proper .srt file. (Invalid end timestamp format '.$subtitleTimeEnd.' at line '.$lineNumber.')'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $subtitleTimeStart = $this->cleanUpTimecode($subtitleTimeStart); |
|
| 104 | - $subtitleTimeEnd = $this->cleanUpTimecode($subtitleTimeEnd); |
|
| 105 | - |
|
| 106 | - if ($subtitleTimeLast && |
|
| 107 | - $strict && // Allow overlapping timecodes when not in "strict mode" |
|
| 108 | - !$this->validateTimelines($subtitleTimeLast, $subtitleTimeStart, true) |
|
| 109 | - ) { |
|
| 110 | - throw new \Exception($this->filename.' is not a proper .srt file. (Starting time invalid: '.$subtitleTimeStart.' at line '.$lineNumber.')'); |
|
| 111 | - } |
|
| 112 | - if (!$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd, !$strict)) { |
|
| 113 | - throw new \Exception($this->filename.' is not a proper .srt file. (Ending time invalid: '.$subtitleTimeEnd.' at line '.$lineNumber.')'); |
|
| 114 | - } |
|
| 115 | - $subtitleText = array(); |
|
| 116 | - $state = 'text'; |
|
| 117 | - break; |
|
| 118 | - |
|
| 119 | - case 'text': |
|
| 120 | - $subtitleText[] = $line; |
|
| 121 | - if ($lineNumber === count($lines) - 1 || ($line === '' && $lines[$lineNumber+1] !== '')) { |
|
| 122 | - $state = 'end'; |
|
| 123 | - // Fall through... |
|
| 124 | - } else { |
|
| 125 | - break; |
|
| 126 | - } |
|
| 127 | - // Fall through... |
|
| 128 | - |
|
| 129 | - case 'end': |
|
| 130 | - $subtitleTimeLast = $subtitleTimeEnd; |
|
| 131 | - $subtitleOrder++; |
|
| 132 | - |
|
| 133 | - $cue = new SubripCue($subtitleTimeStart, $subtitleTimeEnd, implode($this->lineEnding, $subtitleText)); |
|
| 134 | - $cue->setLineEnding($this->lineEnding); |
|
| 135 | - $this->addCue($cue); |
|
| 136 | - |
|
| 137 | - $state = 'order'; |
|
| 138 | - break; |
|
| 77 | + case 'order': |
|
| 78 | + if (!preg_match(self::PATTERN_ORDER, $line)) { |
|
| 79 | + throw new \Exception($this->filename.' is not a proper .srt file. (Expected subtitle order index at line '.$lineNumber.')'); |
|
| 80 | + } |
|
| 81 | + $subtitleIndex = intval($line); |
|
| 82 | + if ($strict && $subtitleOrder !== $subtitleIndex) { |
|
| 83 | + throw new \Exception($this->filename.' is not a proper .srt file. (Invalid subtitle order index: '.$line.' at line '.$lineNumber.')'); |
|
| 84 | + } |
|
| 85 | + $state = 'time'; |
|
| 86 | + break; |
|
| 87 | + |
|
| 88 | + case 'time': |
|
| 89 | + $timeline = explode(' --> ', $line); |
|
| 90 | + if (count($timeline) !== 2) { |
|
| 91 | + throw new \Exception($this->filename." is not a proper .srt file. (Invalid timestamp delimiter at line ".$lineNumber.")"); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $subtitleTimeStart = trim($timeline[0]); |
|
| 95 | + $subtitleTimeEnd = trim($timeline[1]); |
|
| 96 | + if (!preg_match(self::PATTERN_TIMESTAMP, $subtitleTimeStart)) { |
|
| 97 | + throw new \Exception($this->filename.' is not a proper .srt file. (Invalid start timestamp format '.$subtitleTimeStart.' at line '.$lineNumber.')'); |
|
| 98 | + } |
|
| 99 | + if (!preg_match(self::PATTERN_TIMESTAMP, $subtitleTimeEnd)) { |
|
| 100 | + throw new \Exception($this->filename.' is not a proper .srt file. (Invalid end timestamp format '.$subtitleTimeEnd.' at line '.$lineNumber.')'); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $subtitleTimeStart = $this->cleanUpTimecode($subtitleTimeStart); |
|
| 104 | + $subtitleTimeEnd = $this->cleanUpTimecode($subtitleTimeEnd); |
|
| 105 | + |
|
| 106 | + if ($subtitleTimeLast && |
|
| 107 | + $strict && // Allow overlapping timecodes when not in "strict mode" |
|
| 108 | + !$this->validateTimelines($subtitleTimeLast, $subtitleTimeStart, true) |
|
| 109 | + ) { |
|
| 110 | + throw new \Exception($this->filename.' is not a proper .srt file. (Starting time invalid: '.$subtitleTimeStart.' at line '.$lineNumber.')'); |
|
| 111 | + } |
|
| 112 | + if (!$this->validateTimelines($subtitleTimeStart, $subtitleTimeEnd, !$strict)) { |
|
| 113 | + throw new \Exception($this->filename.' is not a proper .srt file. (Ending time invalid: '.$subtitleTimeEnd.' at line '.$lineNumber.')'); |
|
| 114 | + } |
|
| 115 | + $subtitleText = array(); |
|
| 116 | + $state = 'text'; |
|
| 117 | + break; |
|
| 118 | + |
|
| 119 | + case 'text': |
|
| 120 | + $subtitleText[] = $line; |
|
| 121 | + if ($lineNumber === count($lines) - 1 || ($line === '' && $lines[$lineNumber+1] !== '')) { |
|
| 122 | + $state = 'end'; |
|
| 123 | + // Fall through... |
|
| 124 | + } else { |
|
| 125 | + break; |
|
| 126 | + } |
|
| 127 | + // Fall through... |
|
| 128 | + |
|
| 129 | + case 'end': |
|
| 130 | + $subtitleTimeLast = $subtitleTimeEnd; |
|
| 131 | + $subtitleOrder++; |
|
| 132 | + |
|
| 133 | + $cue = new SubripCue($subtitleTimeStart, $subtitleTimeEnd, implode($this->lineEnding, $subtitleText)); |
|
| 134 | + $cue->setLineEnding($this->lineEnding); |
|
| 135 | + $this->addCue($cue); |
|
| 136 | + |
|
| 137 | + $state = 'order'; |
|
| 138 | + break; |
|
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |