-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
463 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
class Parser | ||
{ | ||
const PARSERS = [ | ||
'links' => 'parseLinks', | ||
'timecodes' => 'parseTimeCodes', | ||
]; | ||
|
||
public static function applyParsers(string $string, array $parsers): string|null { | ||
|
||
$result = htmlspecialchars($string); | ||
|
||
foreach ($parsers as $parser) { | ||
|
||
if (!in_array($parser, array_keys(self::PARSERS))) { | ||
throw new \Exception('Invalid parser :'. $parser); | ||
} | ||
|
||
$method = self::PARSERS[$parser]; | ||
|
||
$result = self::$method($result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
private static function parseLinks(string $string): string|null { | ||
|
||
$regex = '/(https?:\/\/)?([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/\S*)?)/m'; | ||
|
||
return preg_replace($regex, '<a class="text-decoration-none" href="//${2}" target="_blank" title="$0" rel="external nofollow">$0</a>', $string); | ||
} | ||
|
||
private static function parseTimeCodes(string $string): string|null | ||
{ | ||
$regex = '/(\d{1,2}:){1,2}\d{2}/m'; | ||
|
||
return preg_replace_callback($regex, function ($matches) { | ||
|
||
$times = array_reverse(explode(':', $matches[0])); | ||
|
||
$timecode = array_reduce(array_keys($times), function($carry, $index) use ($times) { | ||
return $carry + $times[$index] * pow(60, $index); | ||
} , 0); | ||
|
||
return "<button onclick='time($timecode)' class='btn btn-link btn-sm p-0 text-decoration-none' data-timecode='$timecode'>{$matches[0]}</button>"; | ||
|
||
}, $string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.