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
|
11 |
|
public function __construct($params = array()) |
24
|
|
|
{ |
25
|
11 |
|
$this->params = $params; |
26
|
11 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $name |
30
|
|
|
* @param mixed $value |
31
|
|
|
*/ |
32
|
1 |
|
public function setParam($name, $value) |
33
|
|
|
{ |
34
|
1 |
|
$this->params[$name] = $value; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $name |
39
|
|
|
*/ |
40
|
|
|
public function getParam($name) |
41
|
|
|
{ |
42
|
|
|
if (array_key_exists($name, $this->params)) { |
43
|
|
|
return $this->params[$name]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checks if there are any params. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
8 |
|
public function hasParams() |
53
|
|
|
{ |
54
|
8 |
|
return count($this->params) > 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
4 |
|
public function toString() |
61
|
|
|
{ |
62
|
4 |
|
$line = ''; |
63
|
4 |
|
foreach ($this->params as $param => $paramValues) { |
64
|
4 |
|
if (!is_array($paramValues)) { |
65
|
4 |
|
$paramValues = array($paramValues); |
66
|
4 |
|
} |
67
|
4 |
|
foreach ($paramValues as $k => $v) { |
68
|
4 |
|
$paramValues[$k] = $this->escapeParamValue($v); |
69
|
4 |
|
} |
70
|
|
|
|
71
|
4 |
|
if ('' != $line) { |
72
|
1 |
|
$line .= ';'; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
4 |
|
$line .= $param . '=' . implode(',', $paramValues); |
76
|
4 |
|
} |
77
|
|
|
|
78
|
4 |
|
return $line; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns an escaped string for a param value. |
83
|
|
|
* |
84
|
|
|
* @param string $value |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
5 |
|
public function escapeParamValue($value) |
89
|
|
|
{ |
90
|
5 |
|
$count = 0; |
91
|
5 |
|
$value = str_replace('\\', '\\\\', $value); |
92
|
5 |
|
$value = str_replace('"', '\"', $value, $count); |
93
|
5 |
|
$value = str_replace("\n", '\\n', $value); |
94
|
5 |
|
if (false !== strpos($value, ';') || false !== strpos($value, ',') || false !== strpos($value, ':') || $count) { |
|
|
|
|
95
|
2 |
|
$value = '"' . $value . '"'; |
96
|
2 |
|
} |
97
|
|
|
|
98
|
5 |
|
return $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function __toString() |
105
|
|
|
{ |
106
|
|
|
return $this->toString(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: