1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Bigwhoop\Trumpet\Config\Params; |
5
|
|
|
|
6
|
|
|
use Bigwhoop\Trumpet\Config\ConfigException; |
7
|
|
|
use Bigwhoop\Trumpet\Config\Presentation; |
8
|
|
|
use Bigwhoop\Trumpet\Config\Slides; |
9
|
|
|
|
10
|
|
|
final class SlidesParam implements Param |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function parse($value, Presentation $presentation) |
16
|
|
|
{ |
17
|
|
|
if (!is_string($value)) { |
18
|
|
|
throw new ConfigException('Slides must be a string.'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$text = $this->normalizeNewLines($value); |
22
|
|
|
|
23
|
|
|
$lines = explode("\n", $text); |
24
|
|
|
|
25
|
|
|
if (empty($lines)) { |
26
|
|
|
throw new ConfigException('Slides must not be empty.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$slides = new Slides(); |
30
|
|
|
|
31
|
|
|
foreach ($lines as $line) { |
32
|
|
|
if ($this->isHeader1($line)) { |
33
|
|
|
$slides->addContentToNewSlide($line); |
34
|
|
|
$slides->addBlankSlide(); |
35
|
|
|
} elseif ($this->isHeader2($line)) { |
36
|
|
|
$slides->addContentToNewSlide($line); |
37
|
|
|
} else { |
38
|
|
|
$slides->addContentToCurrentSlide($line); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$slides->trim(); |
43
|
|
|
|
44
|
|
|
$presentation->slides = $slides; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $line |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
private function isHeader1($line) |
53
|
|
|
{ |
54
|
|
|
return substr($line, 0, 2) === '# '; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $line |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
private function isHeader2($line) |
63
|
|
|
{ |
64
|
|
|
return substr($line, 0, 3) === '## '; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $text |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
private function normalizeNewLines($text) |
73
|
|
|
{ |
74
|
|
|
$text = str_replace("\r", '', $text); |
75
|
|
|
|
76
|
|
|
return $text; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|