ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
KVForm.php
Go to the documentation of this file.
1<?php
2
29 static function toArray($kvs, $strict=false)
30 {
31 $lines = explode("\n", $kvs);
32
33 $last = array_pop($lines);
34 if ($last !== '') {
35 array_push($lines, $last);
36 if ($strict) {
37 return false;
38 }
39 }
40
41 $values = array();
42
43 for ($lineno = 0; $lineno < count($lines); $lineno++) {
44 $line = $lines[$lineno];
45 $kv = explode(':', $line, 2);
46 if (count($kv) != 2) {
47 if ($strict) {
48 return false;
49 }
50 continue;
51 }
52
53 $key = $kv[0];
54 $tkey = trim($key);
55 if ($tkey != $key) {
56 if ($strict) {
57 return false;
58 }
59 }
60
61 $value = $kv[1];
62 $tval = trim($value);
63 if ($tval != $value) {
64 if ($strict) {
65 return false;
66 }
67 }
68
69 $values[$tkey] = $tval;
70 }
71
72 return $values;
73 }
74
81 static function fromArray($values)
82 {
83 if ($values === null) {
84 return null;
85 }
86
87 ksort($values);
88
89 $serialized = '';
90 foreach ($values as $key => $value) {
91 if (is_array($value)) {
92 list($key, $value) = array($value[0], $value[1]);
93 }
94
95 if (strpos($key, ':') !== false) {
96 return null;
97 }
98
99 if (strpos($key, "\n") !== false) {
100 return null;
101 }
102
103 if (strpos($value, "\n") !== false) {
104 return null;
105 }
106 $serialized .= "$key:$value\n";
107 }
108 return $serialized;
109 }
110}
111
Container for key-value/comma-newline OpenID format and parsing.
Definition: KVForm.php:21
static toArray($kvs, $strict=false)
Convert an OpenID colon/newline separated string into an associative array.
Definition: KVForm.php:29
static fromArray($values)
Convert an array into an OpenID colon/newline separated string.
Definition: KVForm.php:81