ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilRisReader.php
Go to the documentation of this file.
1<?php
2
25{
26 public const RIS_EOL = "\r\n";
27 public const LINE_REGEX = '/^(([A-Z1-9]{2})\s+-(.*))|(.*)$/';
28
29 protected array $data = [];
30
31 public function __construct($options = [])
32 {
33 }
34
48 public function parseString(string $string): void
49 {
50 $contents = explode(self::RIS_EOL, $string);
51 $this->parseArray($contents);
52 }
53
57 protected function parseArray(array $lines): void
58 {
59 $recordset = [];
60
61 // Do any cleaning and normalizing.
62 $this->cleanData($lines);
63
64 $record = [];
65 $lastTag = null;
66 foreach ($lines as $line) {
67 $line = trim((string) $line);
68 $matches = [];
69
70 preg_match(self::LINE_REGEX, $line, $matches);
71 if (!empty($matches[3])) {
72 $lastTag = $matches[2];
73 $record[$matches[2]][] = trim($matches[3]);
74 } // End record and prep a new one.
75 elseif (!empty($matches[2]) && $matches[2] === 'ER') {
76 $lastTag = null;
77 $recordset[] = $record;
78 $record = [];
79 } elseif (!empty($matches[4])) {
80 // Append to the last one.
81 // We skip leading info (like BOMs).
82 if (!empty($lastTag)) {
83 $lastEntry = count($record[$lastTag]) - 1;
84 // We trim because some encoders add tabs or multiple spaces.
85 // Standard is silent on how this should be handled.
86 $record[$lastTag][$lastEntry] .= ' ' . trim($matches[4]);
87 }
88 }
89 }
90 if ($record !== []) {
91 $recordset[] = $record;
92 }
93
94 $this->data = $recordset;
95 }
96
97 public function getRecords(): array
98 {
99 return $this->data;
100 }
101
108 protected function cleanData(array &$lines): void
109 {
110 if ($lines === []) {
111 return;
112 }
113
114 // Currently, we only need to strip a BOM if it exists.
115 // Thanks to Derik Badman (http://madinkbeard.com/) for finding the
116 // bug and suggesting this fix:
117 // http://blog.philipp-michels.de/?p=32
118 $first = $lines[0];
119 if (substr((string) $first, 0, 3) === pack('CCC', 0xef, 0xbb, 0xbf)) {
120 $lines[0] = substr((string) $first, 3);
121 }
122 }
123
124}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
cleanData(array &$lines)
Clean up the data before processing.
__construct($options=[])
parseArray(array $lines)
Take an array of lines and parse them into an RIS record.
parseString(string $string)
Parse a string of RIS data.