ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ExamplesYamlParser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use Symfony\Component\Yaml;
25
27{
28 public const PARSER_STATE_END = 5;
29
30 protected function getYamlEntriesFromString(string $content): array
31 {
32 $parser_state = self::PARSER_STATE_OUTSIDE;
33 $entry = "";
34
35 foreach (preg_split("/((\r?\n)|(\r\n?))/", $content) as $line) {
36
37 if ($parser_state === self::PARSER_STATE_OUTSIDE) {
38 if (preg_match('/---/', $line)) {
39 $entry = "";
40 $parser_state = self::PARSER_STATE_ENTRY;
41 }
42
43 } elseif ($parser_state === self::PARSER_STATE_ENTRY) {
44 if (!preg_match('/(\*$)|(---)/', $line)) {
45 $entry .= $this->purifyYamlLine($line) . "\n";
46 }
47 if (preg_match('/---/', $line)) {
48 $parser_state = self::PARSER_STATE_END;
49 }
50 }
51 }
52 if ($parser_state !== self::PARSER_STATE_END) {
53 return [];
54 }
55
56 $entry = $this->getPHPArrayFromYamlArray([$entry]);
57 return array_shift($entry);
58 }
59}