BodytextParser.php 4.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 3/18/16
 * Time: 5:43 PM
 */

namespace qfq;


class BodytextParser {

    /**
     * @param $bodytext
     * @return mixed
     */
    public function process($bodytext) {
        $bodytext = $this->stripAndRemoveComment($bodytext);
        $bodytext = $this->joinLine($bodytext);
        $bodytext = $this->unNest($bodytext);
        $bodytext = $this->stripAndRemoveComment($bodytext);

        return $bodytext;

    }

    /**
     * Trim all lines, remove all empty lines and  all lines which start with '#'
     * @param $bodytext
     * @return string
     */

    private function stripAndRemoveComment($bodytext) {
        $data = array();

        $src = explode(PHP_EOL, $bodytext);
        foreach ($src as $row) {
            $row = trim($row);
            if ($row === '' || $row[0] === '#') {
                continue;
            }
            $data[] = $row;
        }

        return implode(PHP_EOL, $data);
    }

//PREG_SPLIT_DELIM_CAPTURE

    /**
     * Join lines, which do not begin with '<level>.<keyword>[ ]='
     *
     * @param array $bodytextArray
     * @return string
     */
    private function joinLine($bodytext) {
        $data = array();
        $bodytextArray = explode(PHP_EOL, $bodytext);

        $full = '';
        foreach ($bodytextArray as $row) {
            // Valid 'new line' starts indicators: form, <level>, <level.sublevel>, <level>.<keyword>, {, <level> {, }
64
            if ((1 === preg_match('/^\s*(\d*(\.)?)*\s*(head|althead|tail|sql|rbeg|rend|renr|rsep|fbeg|fend|fsep|form|debugShowBodyText|r) *=/', $row))
65
66
67
68
69
70
71
72
73
74
75
76
                || (1 === preg_match('/^\s*(\d*(\.)?)*\s*({|})\s*/', $row))
                || (1 === preg_match('/^\s*(\d+(\.)?)+/', $row))
            ) {

                // if there is already something: save this
                if ($full !== '')
                    $data[] = $full;

                // start new line
                $full = $row;

            } else {
Carsten  Rose's avatar
Carsten Rose committed
77

78
79
80
81
82
83
84
85
86
87
88
89
                // continue row: concat
                $full .= ' ' . $row;
            }
        }

        // Save last line
        if ($full !== '')
            $data[] = $full;

        return implode(PHP_EOL, $data);
    }

Carsten  Rose's avatar
Carsten Rose committed
90
91
92
    /**
     * @param $bodytext
     * @return mixed|string
93
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
94
     */
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    private function unNest($bodytext) {
        // Replace '\{' | '\}' by internal token. All remaining '}' | '{' means: 'nested'
        $bodytext = str_replace('\{', '#&[_#', $bodytext);
        $bodytext = str_replace('\}', '#&]_#', $bodytext);
        $bodytext = str_replace('{{', '#&[[_#', $bodytext);
        $bodytext = str_replace('}}', '#&]]_#', $bodytext);

        $result = $bodytext;
        $posFirstClose = strpos($result, '}');

        while ($posFirstClose !== false) {
            $posMatchOpen = strrpos(substr($result, 0, $posFirstClose), '{');

            if ($posMatchOpen === false) {
109
                throw new \qfq\UserFormException("Missing open delimiter: $result", ERROR_MISSING_OPEN_DELIMITER);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            }

            $pre = substr($result, 0, $posMatchOpen);
            if ($pre === false)
                $pre = '';

            $post = substr($result, $posFirstClose + 1);
            if ($post === false)
                $post = '';

            // trim also removes '\n'
            $match = trim(substr($result, $posMatchOpen + 1, $posFirstClose - $posMatchOpen - 1));

            // "10.sql = SELECT...\n20 {\n
            $levelStartPos = strrpos(trim($pre), PHP_EOL);
            $levelStartPos = ($levelStartPos === false) ? 0 : $levelStartPos + 1;  // Skip PHP_EOL

            $level = trim(substr($pre, $levelStartPos));
//            if($level==='') {
//                $pre=
//            }
            // remove 'level' from last line
            $pre = substr($pre, 0, $levelStartPos);

            // Split nested content in single rows
            $lines = explode(PHP_EOL, $match);
            foreach ($lines as $line) {
                $pre .= $level . '.' . $line . PHP_EOL;
            }

            $result = $pre . $post;
            $posFirstClose = strpos($result, '}');
        }

        $result = str_replace('#&[_#', '{', $result);
        $result = str_replace('#&]_#', '}', $result);
        $result = str_replace('#&[[_#', '{{', $result);
        $result = str_replace('#&]]_#', '}}', $result);


        return $result;
    }

}