|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the eluceo/iCal package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Markus Poerschke <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Eluceo\iCal; |
|
13
|
|
|
|
|
14
|
|
|
class ParameterBag |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The params. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $params; |
|
22
|
|
|
|
|
23
|
81 |
|
public function __construct($params = []) |
|
24
|
|
|
{ |
|
25
|
81 |
|
$this->params = $params; |
|
26
|
81 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param mixed $value |
|
31
|
|
|
*/ |
|
32
|
19 |
|
public function setParam($name, $value) |
|
33
|
|
|
{ |
|
34
|
19 |
|
$this->params[$name] = $value; |
|
35
|
19 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param $name |
|
39
|
|
|
* |
|
40
|
|
|
* @return array|mixed |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function getParam($name) |
|
43
|
|
|
{ |
|
44
|
2 |
|
if (isset($this->params[$name])) { |
|
45
|
1 |
|
return $this->params[$name]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Checks if there are any params. |
|
53
|
|
|
*/ |
|
54
|
34 |
|
public function hasParams(): bool |
|
55
|
|
|
{ |
|
56
|
34 |
|
return count($this->params) > 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
21 |
|
public function toString(): string |
|
60
|
|
|
{ |
|
61
|
21 |
|
$line = ''; |
|
62
|
21 |
|
foreach ($this->params as $param => $paramValues) { |
|
63
|
21 |
|
if (!is_array($paramValues)) { |
|
64
|
21 |
|
$paramValues = [$paramValues]; |
|
65
|
|
|
} |
|
66
|
21 |
|
foreach ($paramValues as $k => $v) { |
|
67
|
21 |
|
$paramValues[$k] = $this->escapeParamValue($v); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
21 |
|
if ('' != $line) { |
|
71
|
11 |
|
$line .= ';'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
21 |
|
$line .= $param . '=' . implode(',', $paramValues); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
21 |
|
return $line; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns an escaped string for a param value. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $value |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
21 |
|
private function escapeParamValue($value) |
|
88
|
|
|
{ |
|
89
|
21 |
|
$count = 0; |
|
90
|
21 |
|
$value = str_replace('\\', '\\\\', $value); |
|
91
|
21 |
|
$value = str_replace('"', '\"', $value, $count); |
|
92
|
21 |
|
$value = str_replace("\n", '\\n', $value); |
|
93
|
21 |
|
if (false !== strpos($value, ';') || false !== strpos($value, ',') || false !== strpos($value, ':') || $count) { |
|
|
|
|
|
|
94
|
6 |
|
$value = '"' . $value . '"'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
21 |
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function __toString() |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->toString(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: