mirror of
https://github.com/Respect/Validation.git
synced 2026-03-16 15:25:45 +01:00
This commit ensures that if validator A has a direct link to validator B, validator B will have a direct link to validator A too.
83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Dev\Markdown\Linters;
|
|
|
|
use Respect\Dev\Markdown\Content;
|
|
use Respect\Dev\Markdown\File;
|
|
use Respect\Dev\Markdown\Linter;
|
|
|
|
use function array_keys;
|
|
use function array_unique;
|
|
use function basename;
|
|
use function dirname;
|
|
use function file_get_contents;
|
|
use function in_array;
|
|
use function preg_match_all;
|
|
use function sort;
|
|
use function sprintf;
|
|
use function str_contains;
|
|
use function str_ends_with;
|
|
|
|
final readonly class ValidatorRelatedLinter implements Linter
|
|
{
|
|
public function lint(File $file): File
|
|
{
|
|
if (!str_contains($file->filename, '/validators/') || str_ends_with($file->filename, '/validators/index.md')) {
|
|
return $file;
|
|
}
|
|
|
|
$validator = basename($file->filename, '.md');
|
|
$relatedValidators = $this->getRelatedValidators($validator);
|
|
|
|
if ($relatedValidators === []) {
|
|
return $file;
|
|
}
|
|
|
|
$content = new Content();
|
|
$content->paragraph('See also:');
|
|
$content->emptyLine();
|
|
foreach ($relatedValidators as $relatedValidator) {
|
|
$content->anchorListItem($relatedValidator, $relatedValidator . '.md');
|
|
}
|
|
|
|
$content->emptyLine();
|
|
|
|
return $file->withContent($file->content->withSection($content));
|
|
}
|
|
|
|
/** @return array<string> */
|
|
private function getRelatedValidators(string $validator): array
|
|
{
|
|
$docsDirectory = dirname(__DIR__, 3) . '/docs';
|
|
$filename = sprintf('%s/validators/%s.md', $docsDirectory, $validator);
|
|
$content = file_get_contents($filename);
|
|
if ($content === false) {
|
|
return [];
|
|
}
|
|
|
|
$relatedValidators = [];
|
|
|
|
preg_match_all('/\[([^\]]+)\]\(([^\)]+\.md)\)/', $content, $matches);
|
|
foreach (array_keys($matches[0]) as $key) {
|
|
$related = $matches[1][$key];
|
|
$document = $matches[2][$key];
|
|
if (str_contains($document, '/') || in_array($related, $relatedValidators)) {
|
|
continue;
|
|
}
|
|
|
|
$relatedValidators[] = $related;
|
|
}
|
|
|
|
$relatedValidators = array_unique($relatedValidators);
|
|
sort($relatedValidators);
|
|
|
|
return $relatedValidators;
|
|
}
|
|
}
|