Created
November 5, 2021 00:26
-
-
Save ssddanbrown/9674c7a438135fc0c0d99913fe12729a to your computer and use it in GitHub Desktop.
Update Laravel Validation to be array-based
This file contains hidden or 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
<?php | |
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__)); | |
/** @var SplFileInfo $file */ | |
foreach ($iterator as $file) { | |
$filePath = $file->getRealPath(); | |
if ($file->getFilename() === 'update.php' || $file->isDir()) continue; | |
// Get controller file content and find validation rules | |
$controllerContent = file_get_contents($filePath); | |
$matches = []; | |
preg_match_all('/=>\s*([\'"].*?\|.*?[\'"])[\s,]*\n/', $controllerContent, $matches); | |
// Update rule blocks | |
foreach ($matches[1] as $validationString) { | |
$ruleList = array_map(function($rule) { | |
return "'" . $rule . "'"; | |
}, explode('|', trim($validationString, '\'"'))); | |
$newCode = '[' . (implode(', ', $ruleList)) . ']'; | |
$controllerContent = str_replace($validationString, $newCode, $controllerContent); | |
} | |
file_put_contents($filePath, $controllerContent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment