Completed
Pull Request — master (#74)
by Stefan
02:52
created

ParameterBag::escapeParamValue()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 8.8571
cc 5
eloc 8
nc 2
nop 1
crap 5
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
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