Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <colinodell@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
 
14
namespace League\CommonMark\Util;
15
 
991 lars 16
use League\CommonMark\Exception\IOException;
17
 
148 lars 18
/**
19
 * Reads in a CommonMark spec document and extracts the input/output examples for testing against them
20
 */
21
final class SpecReader
22
{
23
    private function __construct()
24
    {
25
    }
26
 
27
    /**
28
     * @return iterable<string, array{input: string, output: string, type: string, section: string, number: int}>
29
     */
30
    public static function read(string $data): iterable
31
    {
32
        // Normalize newlines for platform independence
33
        $data = \preg_replace('/\r\n?/', "\n", $data);
34
        \assert($data !== null);
35
        $data = \preg_replace('/<!-- END TESTS -->.*$/', '', $data);
36
        \assert($data !== null);
37
        \preg_match_all('/^`{32} (example ?\w*)\n([\s\S]*?)^\.\n([\s\S]*?)^`{32}$|^#{1,6} *(.*)$/m', $data, $matches, PREG_SET_ORDER);
38
 
39
        $currentSection = 'Example';
40
        $exampleNumber  = 0;
41
 
42
        foreach ($matches as $match) {
43
            if (isset($match[4])) {
44
                $currentSection = $match[4];
45
                continue;
46
            }
47
 
48
            yield \trim($currentSection . ' #' . $exampleNumber) => [
49
                'input'   => \str_replace('→', "\t", $match[2]),
50
                'output'  => \str_replace('→', "\t", $match[3]),
51
                'type'    => $match[1],
52
                'section' => $currentSection,
53
                'number'  => $exampleNumber++,
54
            ];
55
        }
56
    }
57
 
58
    /**
59
     * @return iterable<string, array{input: string, output: string, type: string, section: string, number: int}>
60
     *
991 lars 61
     * @throws IOException if the file cannot be loaded
148 lars 62
     */
63
    public static function readFile(string $filename): iterable
64
    {
65
        if (($data = \file_get_contents($filename)) === false) {
991 lars 66
            throw new IOException(\sprintf('Failed to load spec from %s', $filename));
148 lars 67
        }
68
 
69
        return self::read($data);
70
    }
71
}