1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
3
|
|
|
* |
4
|
|
|
* This file is part of Dembelo. |
5
|
|
|
* |
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License 3 for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
namespace AdminBundle\Service\TwineImport; |
20
|
|
|
|
21
|
|
|
use DembeloMain\Document\Textnode; |
22
|
|
|
use DembeloMain\Document\TextnodeHitch; |
23
|
|
|
use DembeloMain\Model\Repository\TextNodeRepositoryInterface; |
24
|
|
|
use Exception; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class HitchParser |
28
|
|
|
*/ |
29
|
|
|
class HitchParser |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var TextNodeRepositoryInterface |
33
|
|
|
*/ |
34
|
|
|
private $textnodeRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Textnode[] |
38
|
|
|
*/ |
39
|
|
|
private $nodeNameMapping; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* HitchParser constructor. |
43
|
|
|
* @param TextNodeRepositoryInterface $textNodeRepository |
44
|
|
|
*/ |
45
|
15 |
|
public function __construct(TextNodeRepositoryInterface $textNodeRepository) |
46
|
|
|
{ |
47
|
15 |
|
$this->textnodeRepository = $textNodeRepository; |
48
|
15 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Textnode[] $nodeNameMapping |
52
|
|
|
*/ |
53
|
6 |
|
public function setNodeNameMapping(array $nodeNameMapping): void |
54
|
|
|
{ |
55
|
6 |
|
$this->nodeNameMapping = $nodeNameMapping; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $content |
60
|
|
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return TextnodeHitch |
63
|
|
|
* |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
4 |
|
public function parseDoubleArrowRight(string $content, string $name): TextnodeHitch |
67
|
|
|
{ |
68
|
4 |
|
list($description, $textnodeId) = explode('-->', $content, 2); |
69
|
|
|
|
70
|
4 |
|
if (strlen($description) <= 0 || strlen($textnodeId) <= 0) { |
71
|
2 |
|
throw new Exception(sprintf("The Twine archive file contains a '%s' with the invalid element '[[%s-->%s]]'.", $name, $description, $textnodeId)); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
$externalTextnode = $this->textnodeRepository->find($textnodeId); |
75
|
|
|
|
76
|
2 |
|
if (null === $externalTextnode) { |
77
|
1 |
|
throw new Exception(sprintf("There is a textnode which references the external Dembelo Textnode '%s', but a Dembelo Textnode with such an Id doesn't exist.", $textnodeId)); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
$hitch = new TextnodeHitch(); |
81
|
1 |
|
$hitch->setDescription($description); |
82
|
1 |
|
$hitch->setTargetTextnode($externalTextnode); |
83
|
1 |
|
$hitch->setStatus(TextnodeHitch::STATUS_ACTIVE); |
84
|
|
|
|
85
|
1 |
|
return $hitch; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $content |
90
|
|
|
* @param string $name |
91
|
|
|
* |
92
|
|
|
* @return TextnodeHitch |
93
|
|
|
* |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
4 |
|
public function parseSingleArrowRight(string $content, string $name): TextnodeHitch |
97
|
|
|
{ |
98
|
4 |
|
list($description, $nodeName) = explode('->', $content, 2); |
99
|
|
|
|
100
|
4 |
|
if (\strlen($description) <= 0 || \strlen($nodeName) <= 0) { |
101
|
2 |
|
throw new Exception(sprintf("The Twine archive file contains a '%s' with the invalid element '[[%s->%s]]'.", $name, $description, $nodeName)); |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if (array_key_exists($nodeName, $this->nodeNameMapping) !== true) { |
105
|
1 |
|
throw new Exception(sprintf("There is a textnode which references another textnode named '%s', but this textnode doesn't exist within the same story.", $nodeName)); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$hitch = new TextnodeHitch(); |
109
|
1 |
|
$hitch->setDescription($description); |
110
|
1 |
|
$hitch->setTargetTextnode($this->nodeNameMapping[$nodeName]); |
111
|
1 |
|
$hitch->setStatus(TextnodeHitch::STATUS_ACTIVE); |
112
|
|
|
|
113
|
1 |
|
return $hitch; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $content |
118
|
|
|
* @param string $name |
119
|
|
|
* |
120
|
|
|
* @return TextnodeHitch |
121
|
|
|
* |
122
|
|
|
* @throws Exception |
123
|
|
|
*/ |
124
|
4 |
|
public function parseSingleArrowLeft(string $content, string $name): TextnodeHitch |
125
|
|
|
{ |
126
|
4 |
|
list($nodeName, $description) = explode('<-', $content, 2); |
127
|
|
|
|
128
|
4 |
|
if (\strlen($nodeName) <= 0 || \strlen($description) <= 0) { |
129
|
2 |
|
throw new Exception(sprintf("The Twine archive file contains a '%s' with the invalid element '[[%s<-%s]]'.", $name, $nodeName, $description)); |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
if (array_key_exists($nodeName, $this->nodeNameMapping) !== true) { |
133
|
1 |
|
throw new Exception(sprintf("There is a textnode in the Twine archive file which references another textnode named '%s', but this textnode doesn't exist within the same story.", $nodeName)); |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$hitch = new TextnodeHitch(); |
137
|
1 |
|
$hitch->setDescription($description); |
138
|
1 |
|
$hitch->setTargetTextnode($this->nodeNameMapping[$nodeName]); |
139
|
1 |
|
$hitch->setStatus(TextnodeHitch::STATUS_ACTIVE); |
140
|
|
|
|
141
|
1 |
|
return $hitch; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $content |
146
|
|
|
* @param string $name |
147
|
|
|
* |
148
|
|
|
* @return TextnodeHitch |
149
|
|
|
* |
150
|
|
|
* @throws Exception |
151
|
|
|
*/ |
152
|
3 |
|
public function parseSimpleHitch(string $content, string $name): TextnodeHitch |
153
|
|
|
{ |
154
|
3 |
|
if (strlen($content) <= 0) { |
155
|
1 |
|
throw new Exception(sprintf("The Twine archive file contains a '%s' with the invalid element '[[%s]]'.", $name, $content)); |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
if (array_key_exists($content, $this->nodeNameMapping) !== true) { |
159
|
1 |
|
throw new Exception(sprintf("There is a textnode in the Twine archive file which references another textnode named '%s', but this textnode doesn't exist within the same story.", $content)); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
$hitch = new TextnodeHitch(); |
163
|
1 |
|
$hitch->setDescription($content); |
164
|
1 |
|
$hitch->setTargetTextnode($this->nodeNameMapping[$content]); |
165
|
1 |
|
$hitch->setStatus(TextnodeHitch::STATUS_ACTIVE); |
166
|
|
|
|
167
|
1 |
|
return $hitch; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|